如何使用ant取消对多个JAR文件的归档,并将它们重建为一个JAR文件?

如何使用ant取消对多个JAR文件的归档,并将它们重建为一个JAR文件?,ant,jar,unjar,Ant,Jar,Unjar,我想取消对多个JAR文件的归档,然后使用ant构建脚本将其重新构建到一个JAR中。这可能吗?是的,使用ant是可能的。jar文件基本上是一个带有特殊清单文件的zip文件。所以为了安贾,我们需要解开罐子的拉链。Ant包含一个任务 要解压缩/取消归档项目中的所有jar文件,请执行以下操作: <target name="unjar_dependencies" depends="clean"> <unzip dest="${build.dir}"> <

我想取消对多个JAR文件的归档,然后使用ant构建脚本将其重新构建到一个JAR中。这可能吗?

是的,使用ant是可能的。jar文件基本上是一个带有特殊清单文件的zip文件。所以为了安贾,我们需要解开罐子的拉链。Ant包含一个任务

要解压缩/取消归档项目中的所有jar文件,请执行以下操作:

<target name="unjar_dependencies" depends="clean">
    <unzip dest="${build.dir}">
        <fileset dir="${lib.dir}">
            <include name="**/*.jar" />
        </fileset>    
    </unzip>
</target>

显然,您需要首先声明${build.dir}和${lib.dir}。行
告诉ant包含所有以jar扩展名结尾的文件,您可以调整包含以满足您的需要

要将所有内容打包到一个罐子中,请使用以下任务:


在本例中,我们包括不同的文件集。在一个文件集中,我们包含所有编译的类。在另一个文件集中,我们包含了这个特定项目所依赖的两个配置文件。

是的

你有两种可能:

  • Espen回答:
一个可能的解决方案创建了一个 jar文件中的所有jar文件 给定目录:

<target name="dependencies.jar">
    <jar destfile="WebContent/dependencies.jar">
        <zipgroupfileset dir="lib/default/" includes="*.jar" 
              excludes="*.properties" />
    </jar>
</target>

如果您不需要排除某些jar中的内容(例如,某些属性配置文件可能会覆盖您的jar,等等),那么这非常有用。这里的excludes属性从dir属性中过滤出文件

  • 使用zipfileset
另一种解决方案是使用zipfileset标记,此时excludes属性将从要合并的jar中过滤出内容

<jar destfile="your_final_jar.jar" filesetmanifest="mergewithoutmain">
    <manifest>
        <attribute name="Main-Class" value="main.class"/>
        <attribute name="Class-Path" value="."/>
    </manifest>
    <zipfileset 
       excludes="META-INF/*.SF"
       src="/path/to/first/jar/to/include.jar"/>
</jar>

  • 当然,您可以将这两个标记(zipfileset和zipgroupfileset)组合在同一个jar标记中,以充分利用这两个标记
  • 是的,这是可能的

    一种可能的解决方案是从给定目录中的所有jar文件创建一个jar文件:

    <target name="dependencies.jar">
        <jar destfile="WebContent/dependencies.jar">
            <zipgroupfileset dir="lib/default/" includes="*.jar" 
                  excludes="*.properties" />
        </jar>
    </target>
    

    还有一个专门用于重新包装jar的项目,名为。你可以用它把多个罐子重新包装成一个罐子。根据需要,您甚至可以重命名类以防止版本冲突

    从他们的:

    在本例中,我们包含jaxen.jar中的类,并添加一条规则,将任何以“org.jaxen”开头的类名改为以“org.example.jaxen”开头(在我们想象的世界中,我们控制example.org域):

    
    
    <target name="dependencies.jar">
        <jar destfile="WebContent/dependencies.jar">
            <zipgroupfileset dir="lib/default/" includes="*.jar" 
                  excludes="*.properties" />
        </jar>
    </target>
    
    <target name="jar" depends="compile">
        <taskdef name="jarjar" classname="com.tonicsystems.jarjar.JarJarTask"
            classpath="lib/jarjar.jar"/>
        <jarjar jarfile="dist/example.jar">
            <fileset dir="build/main"/>
            <zipfileset src="lib/jaxen.jar"/>
            <rule pattern="org.jaxen.**" result="org.example.@1"/>
        </jarjar>
    </target>