【问题标题】:Replacing a string in jnlp when generating war using ant build使用 ant build 生成战争时替换 jnlp 中的字符串
【发布时间】:2012-10-11 09:52:21
【问题描述】:
任何人都可以帮助我在构建应用程序时如何替换 jnlp 中的字符串。这是我遵循的步骤。
-
创建一个包含字符串内容的 jnlp 文件,例如
<jnlp spec="1.0+" codebase="%$%test.url$" href="test.jnlp">
- 而且我们的应用程序依赖于环境,在生成 war 时,我会将命令行参数传递给 ant build。所以基于这个命令行参数,我们确实在属性文件中维护了一个不同的 URL。
- 在构建应用程序时,我无法替换 jnlp 中依赖于环境的 URL。
任何人都可以就此提出建议吗?
【问题讨论】:
标签:
java
ant
jnlp
java-web-start
replace
【解决方案1】:
要将参数传递给 ant 以选择环境,您可以使用例如 ant -Denv=prod 或 ant -Denv=test 启动 ant,然后根据 env 属性在 ant build.xml 中做出决定。
您可以根据所选环境设置其他属性,如下所示:
<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。