Ant任务来过滤zip文件和JAR文件清单(Ant task to filter JARs for

2019-10-16 15:44发布

我在那里,我创造了几个JAR文件的zip文件和清单的Ant文件。 无论是拉链和清单引用相同的库,但方式略有不同。 如果可能的话,我想引用结合的文件,而不是明确地写他们两次,并希望能在这两个任务引用同步。 下面是我目前在做什么的例子。

<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某种我能在这两个任务参考。 需要注意的是清单中不包含任何文件夹/路径。 清单只包含在所提到的目录中发现的JAR文件zip任务。

Answer 1:

你是对的。 你可以用一个共同完成这个fileset由双方共享的zipcreateManifest任务。 对于zip任务,将文件复制到一个临时位置,然后压缩起来。

对于createManifest任务,使用字符替换,从路径删除文件夹。 字符替换策略在讨论“ 在Ant属性替换字符 ”。 如果您有蚂蚁的Contrib ,您可以使用简化以下字符替换算法PropertyRegex Ant任务 。

<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包含以下项目:

  • 免费/ dom4j的-1.6.1.jar
  • 免费/ GSON-2.1.jar
  • 免费/番石榴11.0.2.jar


文章来源: Ant task to filter JARs for zip file and manifest
标签: java ant jar