Ant任务来过滤zip文件

Ant任务来过滤zip文件

本文介绍了Ant任务来过滤zip文件,JAR文件清单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在哪里,我创建了几个JAR文件的zip文件和清单Ant文件。无论是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>

我想最好想有一个文件集一些,我可以在这两个任务引用。需要注意的是清单不包含任何文件夹/路径。清单只包含在拉链任务中提到的目录中发现的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.

推荐答案

您是对的。你可以用一个共同完成这个文件集由两个拉链 createManifest 任务。对于拉链任务,将文件复制到一个临时位置,然后压缩他们。

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属性 ,则可以通过使用的。

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>

当我执行上面的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 的包含以下项目:


  • 的lib / dom4j的-1.6.1.jar

  • 的lib / GSON-2.1.jar

  • 的lib /番石榴11.0.2.jar

  • lib/dom4j-1.6.1.jar
  • lib/gson-2.1.jar
  • lib/guava-11.0.2.jar

这篇关于Ant任务来过滤zip文件,JAR文件清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 19:01