【问题标题】:Ant task to filter JARs for zip file and manifest为 zip 文件和清单过滤 JAR 的 Ant 任务
【发布时间】:2012-05-01 12:36:59
【问题描述】:

我有一个 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 文件。

【问题讨论】:

    标签: java ant jar


    【解决方案1】:

    你是对的。您可以通过zipcreateManifest 任务共享的公共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} = &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

    【讨论】:

    • 太好了,非常感谢!!!我不得不对正则表达式进行 1 次小改动(因为我的清单使用了反斜杠),但它就像一个魅力!
    • &lt;replaceregex pattern="(?:[^;\\]+\\)+?([^;\\]+\.jar)" replace="\1" flags="g" /&gt;
    猜你喜欢
    • 2014-01-14
    • 1970-01-01
    • 2014-05-03
    • 1970-01-01
    • 1970-01-01
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多