我有一个 ant 任务,它创建一个 webstart jnlp 文件。
它替换了模板文件中的 @title@ 等标记:
<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" codebase="@codebase@">
<information>
<title>@title@</title>
</information>
<resources>
@jars@
</resources>
<application-desc main-class="@mainclass@"/>
</jnlp>
问题是我的 lib/目录中有很多 jars: Log4J.jar、xpp.jar、resources.jar ... 和 1 个 jar token 。
如何用 jar 文件名替换 @jars@ token ? 这样输出就变成:
<resources>
<jar href="log4J.jar"/>
<jar href="xpp.jar"/>
<jar href="resources.jar"/>
</resources>
这是我的 Ant 项目的一部分:
<target name="webstart" description="Deploy as jnlp webstart">
<copy file="template.jnlp" tofile="test.jnlp">
<filterchain>
<replacetokens>
<token key="codebase" value="myCodebase" />
<token key="title" value="myTitle" />
<token key="jars" value="jar href="xxx.jar" />
</replacetokens>
</filterchain>
</copy>
</target>
<project/>
最佳答案
我设法使用 ant-contrib 实现了这一点(感谢 Chad Nouis 在属性中提供 CDATA 的提示):
<!-- Tricky part is XML content here CDATA Elements are needed, this is the first part-->
<property name="pre"><![CDATA[
<jar href="]]></property>
<!-- Tricky part is XML content here CDATA Elements are needed, this is the last part-->
<property name="after"><![CDATA["/>]]></property>
<!-- this will be our temp file-->
<delete file="temp.txt" failonerror="false"/>
<!-- for task from ant-contrib-->
<for param="file">
<fileset dir="dist" includes="*.jar"/>
<sequential>
<!-- write it into a file, using var/properties did not work-->
<echo file="temp.txt" append="true">${pre}@{file}${after}</echo>
</sequential>
</for>
<!-- load file content in property-->
<loadfile property="xml.data" srcfile="temp.txt"/>
<!-- finish-->
<copy file="template.jnlp" tofile="test.jnlp" overwrite="true">
<filterchain>
<replacetokens>
<token key="codebase" value="myCodebase" />
<token key="title" value="myTitle" />
<token key="jars" value="${xml.data}" />
</replacetokens>
</filterchain>
</copy>
链接:
关于java - ant 使用多个 jar 文件名替换 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9797770/