How do you use ant to unjar multiple JAR files and rebuild them into one JAR file?

好久不见. 提交于 2019-11-30 00:38:13

问题


I would like to unjar multiple JAR files and then rebuild into one JAR using an ant build script. Is this possible?


回答1:


Yes, it's possible with ant. A jar file is basically a zip with a special manifest file. So to unjar, we need to unzip the jars. Ant includes an unzip task.

To unzip/unjar all the jar files in your project:

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

Obviously you need to declare ${build.dir} and ${lib.dir} first. The line <include name="**/*.jar" /> tells ant to include all files that end up with the jar extension, you can tweak that include to suit your needs.

To pack everything into a jar, you use the jar task:

<target name="make_jar" depends="compile, unjar_dependencies">
    <jar basedir="${build.dir}"
         destfile="${dist.dir}/${project_name}.jar">
        <manifest>
            <attribute name="Main-Class" value="${mainclass}" />
        </manifest>
        <fileset dir="${build.dir}">
            <include name="**/*.class" />
        </fileset>
        <fileset dir="${src.dir}">
            <include name="applicationContext.xml" />
            <include name="log4j.properties" />
        </fileset>
    </jar>
</target>

In this example, we include different filesets. In one fileset we are including all compiled classes. In another fileset we include two config files that this particular project depends upon.




回答2:


Yes it is !

You have two possibilities :

  • Espen answer :

One possible solution that creates one jar file from all the jar files in a given directory:

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

This is useful if you don't need to exclude content that are in some jars (like for example some properties configuration file that might override yours, etc). Here the excludes properties is filtering out files from the dir property.

  • Use zipfileset

The other solution is to use the zipfileset tag where the excludes property this time will filter out content from the jar to be merged.

<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>
  • Of course you can combine the two tags (zipfileset and zipgroupfileset) inside the same jar tag to get the best of the two.



回答3:


Yes, it's possible.

One possible solution that creates one jar file from all the jar files in a given directory:

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



回答4:


There is also a project devoted to repackage jars called JarJar. You can use it to repackage mutiple Jars into one. Depending on your requirements, you can even rename classes to prevent version conflicts.

From their getting started page:

In this example we include classes from jaxen.jar and add a rule that changes any class name starting with "org.jaxen" to start with "org.example.jaxen" instead (in our imaginary world we control the example.org domain):

<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>


来源:https://stackoverflow.com/questions/2823187/how-do-you-use-ant-to-unjar-multiple-jar-files-and-rebuild-them-into-one-jar-fil

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!