我有一个 Ant 文件,我正在其中创建一个 zip 文件和多个 JAR 文件的 list 。 zip 和 list 都引用相同的库,但方式略有不同。如果可能的话,我想合并对文件的引用,而不是明确地将它们写入两次并希望两个任务中的引用同步。以下是我目前正在做的事情的示例。
<target name="zip" depends="default">
<zip destfile="${dist.dir}/${project.name}_v${project.version}.zip">
<zipfileset prefix="lib" dir="lib/Dom4J" includes="*.jar"/>
<zipfileset prefix="lib" dir="lib/GSON" includes="*.jar"/>
<zipfileset prefix="lib" dir="lib/Guava" includes="*.jar"/>
<!-- ... A bunch more (Note I don't want everything
in the lib directory, just certain subfolders
within the lib directory which are explicitly
listed here like GSON. -->
</zip>
</target>
<target name="createManifest">
<!-- Hard code the classpath by hand and hope
they sync up with the zip task -->
<property name="mfClasspath"
value="dom4j-1.6.1.jar gson-2.1.jar guava-11.0.2.jar" />
<!-- Code to use the mfClasspath when creating the manifest
omitted for brevity -->
</target>
我理想中希望拥有一个可以在这两个任务中引用的某种文件集
。请注意, list 不包含任何文件夹/路径。该 list 仅包含在 zip
任务中提到的目录中找到的 JAR 文件。
最佳答案
你是对的。您可以使用 zip
和 createManifest
任务共享的通用fileset
来完成此操作。对于 zip
任务,将文件复制到临时位置,然后将其压缩。
对于 createManifest
任务,使用字符替换从路径中删除文件夹。字符替换策略在“Replacing characters in Ant property”中讨论。如果您有Ant-Contrib ,您可以使用 PropertyRegex Ant task 来简化下面的字符替换算法.
<project default="all">
<fileset id="jars" dir=".">
<include name="lib/Dom4J/dom4j-1.6.1.jar" />
<include name="lib/GSON/gson-2.1.jar" />
<include name="lib/Guava/guava-11.0.2.jar" />
</fileset>
<target name="zip">
<copy todir="tmp.dir" flatten="true">
<fileset refid="jars" />
</copy>
<zip destfile="example.zip">
<zipfileset dir="tmp.dir" prefix="lib" />
</zip>
<delete dir="tmp.dir" />
</target>
<target name="createManifest">
<property name="jars.property" refid="jars" />
<echo message="${jars.property}" file="some.tmp.file" />
<loadfile property="mfClasspath" srcFile="some.tmp.file">
<filterchain>
<tokenfilter>
<replaceregex pattern="(?:[^;/]+/)+?([^;/]+\.jar)"
replace="\1" flags="g" />
<replacestring from=";" to=" " />
</tokenfilter>
</filterchain>
</loadfile>
<delete file="some.tmp.file" />
</target>
<target name="all" depends="zip, createManifest">
<echo message="$${jars.property} = "${jars.property}"" />
<echo message="$${mfClasspath} = "${mfClasspath}"" />
</target>
</project>
当我执行上述 Ant 构建文件时,控制台输出以下内容:
Buildfile: /workspace/StackOverflow/build.xml
zip:
[zip] Building zip: /workspace/StackOverflow/example.zip
[delete] Deleting directory /workspace/StackOverflow/tmp.dir
createManifest:
[delete] Deleting: /workspace/StackOverflow/some.tmp.file
all:
[echo] ${jars.property} = "lib/Dom4J/dom4j-1.6.1.jar;lib/GSON/gson-2.1.jar;lib/Guava/guava-11.0.2.jar"
[echo] ${mfClasspath} = "dom4j-1.6.1.jar gson-2.1.jar guava-11.0.2.jar"
BUILD SUCCESSFUL
Total time: 675 milliseconds
此外,example.zip 包含以下条目:
- lib/dom4j-1.6.1.jar
- lib/gson-2.1.jar
- lib/guava-11.0.2.jar
关于java - Ant 任务过滤 JAR 中的 zip 文件和 list ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10397717/