Java 从 jar 中删除文件的 ant 任务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2521231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:46:08  来源:igfitidea点击:

ant task to remove files from a jar

antjavajarpackages

提问by bguiz

How to write an ant task that removes files from a previously compiled JAR?

如何编写从先前编译的 JAR 中删除文件的 ant 任务?

Let's say the files in my JAR are:

假设我的 JAR 中的文件是:

aaa/bbb/ccc/Class1
aaa/bbb/ccc/Class2
aaa/bbb/def/Class3
aaa/bbb/def/Class4

... and I want a version of this JAR file without the aaa.bbb.defpackage, and I need to strip it out using ant, such that I end up with a JAR that contains:

...我想要一个没有aaa.bbb.def包的这个 JAR 文件的版本,我需要使用 ant 将它剥离,这样我最终得到一个包含以下内容的 JAR:

aaa/bbb/ccc/Class1
aaa/bbb/ccc/Class2

Thanks!

谢谢!

采纳答案by mipadi

Have you tried using the zipfilesettask?

您是否尝试过使用该zipfileset任务?

<jar destfile="stripped.jar">
    <zipfileset src="full.jar" excludes="files/to/exclude/**/*.file"/>
</jar>


For example:

例如:

<property name="library.dir" value="dist"/>
<property name="library.file" value="YourJavaArchive.jar"/>
<property name="library.path" value="${library.dir}/${library.file}" />
<property name="library.path.new" value="${library.dir}/new-${library.file}"/>

<target name="purge-superfluous">
    <echo>Removing superfluous files from Java archive.</echo>

    <jar destfile="${library.path.new}">
        <zipfileset src="${library.path}" excludes="**/ComicSans.ttf"/>
    </jar>

    <delete file="${library.path}" />
    <move file="${library.path.new}" tofile="${library.path}" />
</target>

回答by navr

I am not sure if there a direct solution for your requirement. I would recommend to explode the jar to some temp directory and then remove unwanted class files. Finally create a new jar with required class files.

我不确定是否有针对您的要求的直接解决方案。我建议将 jar 分解到某个临时目录,然后删除不需要的类文件。最后用所需的类文件创建一个新的 jar。

Reference links:

参考链接:

http://ant.apache.org/manual/Tasks/unzip.html

http://ant.apache.org/manual/Tasks/unzip.html

http://ant.apache.org/manual/Tasks/delete.html

http://ant.apache.org/manual/Tasks/delete.html

http://ant.apache.org/manual/Tasks/jar.html

http://ant.apache.org/manual/Tasks/jar.html

回答by David

You have to unjar and rejar.

你必须解压和重压。

<unzip src="myjar.jar" dest="/classes/">
<jar destfile="newjar.jar"
    basedir="/classes/"
    includes="**/*"
    excludes="**/def/*"
/>    

回答by Michael Besteck

If a jar-file capable archiver program, like e.g. "zip" on Linux, is available, the task can be done by

如果可以使用支持 jar 文件的归档程序,例如 Linux 上的“zip”,则该任务可以通过

<exec executable="zip">            
<arg value="-d"/>            
<arg value="myJarCopyToStrip.jar"/>            
<arg value="aaa/bbb/def/*>            
<arg value="aaa/bbb/def>
</exec>

Subtree deletion depends on the capabilities of the used archiver.
The "os" attribute of the Ant "exec" task allows to use different archivers on different OS's.

子树删除取决于所用归档程序的功能。
Ant“exec”任务的“os”属性允许在不同的操作系统上使用不同的归档器。

回答by Jeremy Bunn

I came here looking to use ant as a workaround some short comings in gradle unzip.

我来这里是想使用 ant 作为解决 gradle unzip 中的一些缺点的解决方法。

On the off chance someone else is in the same boat.

万一有其他人在同一条船上。

Here is an example:

下面是一个例子:

    task antUnzip() << {

            ant.jar(destfile : "stripped.jar") {
                zipfileset (src : "full.jar", excludes : "files/to/exclude/**/*.file") {
                }
            }
}

回答by Marc Magon

The answers didn't quite add up for me -

答案对我来说并不完全 -

<zip destfile="tmp.jar">
        <zipfileset src="src.jar">
                <exclude name="**/*.class" />
        </zipfileset>
</zip>
<move file="tmp.jar" tofile="src.jar" /> 

This uses a single pass and doesn't add too much time to the build

这使用单次传递并且不会为构建增加太多时间

source : http://ant.1045680.n5.nabble.com/Remove-entru-from-ZIP-file-using-ANT-td1353728.html

来源:http: //ant.1045680.n5.nabble.com/Remove-entru-from-ZIP-file-using-ANT-td1353728.html