用于过滤 zip 文件和清单的 JAR 的 Ant 任务 [英] Ant task to filter JARs for zip file and manifest
问题描述
我有一个 Ant 文件,我正在其中为多个 JAR 文件创建一个 zip 文件和一个清单.zip 和清单都引用了相同的库,但方式略有不同.如果可能,我想合并对文件的引用,而不是将它们显式写入两次并希望两个任务中的引用同步.下面是我目前正在做的一个例子.
I have an Ant file where I am creating a zip file and a manifest for several JAR files. Both the zip and the manifest reference the same libraries, but in slightly different ways. If possible I would like to combine the references to the files instead of explicitly writing them twice and hoping the references in both tasks sync up. Below is an example of what I am currently doing.
<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>
我最希望拥有的是某种类型的 fileset
,我可以在这两个任务中引用它.请注意,清单不包含任何文件夹/路径.清单仅包含在 zip
任务中提到的目录中找到的 JAR 文件.
What I would ideally like to have is a fileset
of some sort that I could reference in both tasks. Note that the manifest does not contain any folders/paths. The manifest only contains the JAR files found within the directories mentioned in the zip
task.
推荐答案
你说得对.您可以使用由 zip
和 createManifest
任务共享的公共 fileset
来完成此操作.对于 zip
任务,将文件复制到一个临时位置,然后将它们压缩.
You are right. You can accomplish this with a common fileset
shared by both the zip
and createManifest
tasks. For the zip
task, copy the files to a temporary location and then zip them up.
对于 createManifest
任务,使用字符替换从路径中删除文件夹.字符替换策略在替换 Ant 属性中的字符"中讨论.如果您有 Ant-Contrib,您可以使用
For the createManifest
task, use character replacement to remove the folders from the paths. Character-replacement strategies are discussed in "Replacing characters in Ant property." If you have Ant-Contrib, you can simplify the character-replacement algorithm below by using the 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 构建文件时,控制台输出以下内容:
When I executed the above Ant buildfile, the following was output to the console:
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 包含以下条目:
Also, example.zip contained the following entries:
- lib/dom4j-1.6.1.jar
- lib/gson-2.1.jar
- lib/guava-11.0.2.jar
这篇关于用于过滤 zip 文件和清单的 JAR 的 Ant 任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!