1

我有一个 Ant 文件,我正在其中创建一个 zip 文件和几个 JAR 文件的清单。zip 和清单都引用相同的库,但方式略有不同。如果可能的话,我想合并对文件的引用,而不是明确地写两次,并希望两个任务中的引用同步。下面是我目前正在做的一个例子。

<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 文件。

4

1 回答 1

1

你说的对。您可以使用和任务fileset共享的共同点来完成此操作。对于该任务,将文件复制到临时位置,然后将其压缩。zipcreateManifestzip

对于该createManifest任务,使用字符替换从路径中删除文件夹。字符替换策略在“替换 Ant 属性中的字符”中讨论。如果你有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} = &quot;${jars.property}&quot;" />
        <echo message="$${mfClasspath} = &quot;${mfClasspath}&quot;" />
    </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
于 2012-05-04T01:56:30.200 回答