任何人都可以帮助我如何在构建应用程序时替换 jnlp 中的字符串。这是我遵循的步骤。
创建一个jnlp文件,字符串内容如
<jnlp spec="1.0+" codebase="%$%test.url$" href="test.jnlp">
- 我们的应用程序依赖于环境,在生成 war 时,我会将命令行参数传递给 ant build。因此,基于此命令行参数,我们确实在属性文件中维护了一个不同的 URL。
- 在构建应用程序时,我无法替换 jnlp 中依赖于环境的 URL。
有人可以对此提出建议吗?
最佳答案
要将参数传递给 ant 以选择环境,您可以从 ant 开始,例如 ant -Denv=prod
或 ant -Denv=test
并根据 env
在 ant build.xml 中做出决定属性(property)。
您可以根据所选环境设置其他属性,如下所示:
<target name="compile" depends="init" description="compile the source">
<!-- settings for production environment -->
<condition property="scalacparams"
value="-optimise -Yinline -Ydead-code -Ywarn-dead-code -g:none -Xdisable-assertions">
<equals arg1="${env}" arg2="prod"/>
</condition>
<condition property="javacparams" value="-g:none -Xlint">
<equals arg1="${env}" arg2="prod"/>
</condition>
<!-- settings for test environment -->
<condition property="scalacparams" value="-g:vars">
<equals arg1="${env}" arg2="test"/>
</condition>
<condition property="javacparams" value="-g -Xlint">
<equals arg1="${env}" arg2="test"/>
</condition>
<!-- abort if no environment chosen -->
<fail message="Use -Denv=prod or -Denv=test">
<condition>
<not>
<isset property="scalacparams"/>
</not>
</condition>
</fail>
<!-- actual compilation done here ->
</target>
你也可以使用 <if>
仅针对特定环境执行特定操作:
<if> <!-- proguard only for production release -->
<equals arg1="${env}" arg2="prod" />
<then>
<!-- run proguard here -->
</then>
</if>
最后,为了根据环境将字符串插入到文件中,首先在检查选择了哪个环境(如上)后设置一个属性,然后:
<copy file="template.jnlp" tofile="appname.jnlp">
<filterset begintoken="$" endtoken="$">
<filter token="compiledwith" value="${scalacparams}"/>
<!--- more filter rules here -->
</filterset>
</copy>
假设 template.jnlp 是一个占位符被 $ 包围的文件。在示例中,template.jnlp 中的 $compiledwith$ 将替换为之前设置的 scala 编译器参数,并将结果写入 appname.jnlp。
关于java - 使用 ant build 生成 war 时替换 jnlp 中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12836825/