java - ant 使用多个 jar 文件名替换 token

标签 java ant jar token

我有一个 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/

相关文章:

java - 从 JPA 注释生成 DDL

java - 如何在java中创建grep命令作为linux grep命令

java - 带有Gradle的Hibernate工具,jdbc错误

java - Ant中的echo目标描述

java - Ant 找不到 Javac。无论我做什么,它总是声称 JAVA_HOME 是 "C:\Program Files\Java\jre6"

objective-c - JObjC.jar 是做什么用的?

spring-boot - 强制 Spring Boot 在 web.xml 中使用 servlet 映射

java - 自定义 Java PMD 规则 : Can't find the class CustomRule

java - ICal4j 中的递归规则

java - 移植库时首先要做的事情