怎么用ant解压多个JAR文件并重建成一个JAR文件? [英] How do you use ant to unjar multiple JAR files and rebuild them into one JAR file?

查看:32
本文介绍了怎么用ant解压多个JAR文件并重建成一个JAR文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解压多个 JAR 文件,然后使用 ant 构建脚本重建为一个 JAR.这可能吗?

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

推荐答案

是的,ant 可以做到.jar 文件基本上是带有特殊清单文件的 zip.所以要解压,我们需要解压罐子.Ant 包括一个解压缩任务.

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.

解压/解压项目中的所有 jar 文件:

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>

显然你需要先声明 ${build.dir} 和 ${lib.dir}.<include name="**/*.jar"/> 行告诉 ant 包含所有以 jar 扩展名结尾的文件,您可以调整该包含以满足您的需要.

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.

要将所有内容打包到 jar 中,请使用 jar 任务:

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.

这篇关于怎么用ant解压多个JAR文件并重建成一个JAR文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆