Ant task to filter JARs for zip file and manifest

2019-08-07 14:33发布

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>

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.

标签: java ant jar
1条回答
再贱就再见
2楼-- · 2019-08-07 15:15

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.

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} = &quot;${jars.property}&quot;" />
        <echo message="$${mfClasspath} = &quot;${mfClasspath}&quot;" />
    </target>
</project>

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

Also, example.zip contained the following entries:

  • lib/dom4j-1.6.1.jar
  • lib/gson-2.1.jar
  • lib/guava-11.0.2.jar
查看更多
登录 后发表回答