我有一个Java应用程序,我将其打包到JAR中。我创建了一个Ant脚本,因为我还需要为JAR添加资源(图标等)。
现在,我在我的项目中使用了库(Apache HttpClient和JSON库)。我也将其内容复制到JAR中,因为这是最简单的方法。
我的构建文件:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="create_run_jar"
name="Create Runnable Jar">
<target name="create_run_jar">
<jar destfile="C:/Users/Pietu1998/Documents/Java/Whatever.jar"
filesetmanifest="mergewithoutmain">
<manifest>
<attribute name="Main-Class"
value="net.pietu1998.whatever" />
<attribute name="Class-Path" value="." />
</manifest>
<fileset dir="C:/Users/Pietu1998/Documents/Java/Whatever/bin"
includes="*.class" />
<fileset dir="C:/Users/Pietu1998/Documents/Java/Whatever/res" />
<zipfileset excludes="META-INF/**"
src="C:/Users/Pietu1998/Documents/Java/Whatever/lib/commons-codec-1.6.jar" />
<zipfileset excludes="META-INF/**"
src="C:/Users/Pietu1998/Documents/Java/Whatever/lib/commons-logging-1.1.3.jar" />
<!-- More (like 5) JARs -->
</jar>
</target>
</project>
然而,库(JAR)有自己的META-INF
文件夹,它们中有很多东西; LICENSE
,CONDITIONS
等文件以及名为maven
的Maven文件夹。
该项目仅供个人使用,所以我只想摆脱不必要的东西。我已经尝试了一些方法来排除所有META-INF
内容,但总会留下一些东西。
excludes="META-INF"
留下了一切。excludes="META-INF/**"
离开maven
文件夹。**/excludes="META-INF/**"
,见上文。我相信我可以使用很多include-exludes或patternset
s,但这会导致很多重复。
有没有办法(不是针对此特定情况)排除文件夹(此处为META-INF
)及其所有内容(包括子目录),最好不要重复(对于很多图书馆)?
答案 0 :(得分:0)
使用excludes="META-INF/**""
对我有用(Ant 1.9.3,Windows 7,JDK 1.7.0_51)
下载原始commons-logging-1.1.3.jar from here后的一些测试:
<project>
<zipfileset excludes="META-INF/**" src="c:/area51/commons-logging-1.1.3.jar" id="foobar"/>
<!-- for output line by line -->
<pathconvert property="foo" refid="foobar">
<map from="c:/area51/commons-logging-1.1.3.jar:" to="${line.separator}"/>
</pathconvert>
<echo>${foo}</echo>
</project>
输出只包含org / apache / commons / logging / ..下的类文件:
[echo] commons-logging-1.1.3.jar
[echo] org/apache/commons/logging/Log.class;
[echo] org/apache/commons/logging/LogConfigurationException.class;
[echo] org/apache/commons/logging/LogFactory$1.class;
[echo] org/apache/commons/logging/LogFactory$2.class;
[echo] org/apache/commons/logging/LogFactory$3.class;
[echo] org/apache/commons/logging/LogFactory$4.class;
[echo] org/apache/commons/logging/LogFactory$5.class;
[echo] org/apache/commons/logging/LogFactory$6.class;
[echo] org/apache/commons/logging/LogFactory.class;
[echo] org/apache/commons/logging/LogSource.class;
[echo] org/apache/commons/logging/impl/AvalonLogger.class;
[echo] org/apache/commons/logging/impl/Jdk13LumberjackLogger.class;
[echo] org/apache/commons/logging/impl/Jdk14Logger.class;
[echo] org/apache/commons/logging/impl/Log4JLogger.class;
[echo] org/apache/commons/logging/impl/LogFactoryImpl$1.class;
[echo] org/apache/commons/logging/impl/LogFactoryImpl$2.class;
[echo] org/apache/commons/logging/impl/LogFactoryImpl$3.class;
[echo] org/apache/commons/logging/impl/LogFactoryImpl.class;
[echo] org/apache/commons/logging/impl/LogKitLogger.class;
[echo] org/apache/commons/logging/impl/NoOpLog.class;
[echo] org/apache/commons/logging/impl/ServletContextCleaner.class;
[echo] org/apache/commons/logging/impl/SimpleLog$1.class;
[echo] org/apache/commons/logging/impl/SimpleLog.class;
[echo] org/apache/commons/logging/impl/WeakHashtable$1.class;
[echo] org/apache/commons/logging/impl/WeakHashtable$Entry.class;
[echo] org/apache/commons/logging/impl/WeakHashtable$Referenced.class;
[echo] org/apache/commons/logging/impl/WeakHashtable$WeakKey.class;
[echo] org/apache/commons/logging/impl/WeakHashtable.class
也许您忘记将jar任务的更新属性设置为true:
<jar destfile=".." update="true" ..>
覆盖一些已经存在的同名jar文件!
答案 1 :(得分:0)
我找到了一种与星星一起做的方法。
<zipfileset excludes="META-INF/**/*" src="C:\library.jar" />
这会排除META-INF
文件夹及其所有内容。
*
表示它会排除文件夹中的所有文件,foo/**/bar
表示在bar
内的任何级别排除了foo
。因此,foo/**/*
会排除foo
内每个级别的每个文件。