将版本合并到一个 JAR 文件的最简单方法
是否有一个工具或脚本可以轻松地将一堆 JAR 文件合并到一个 JAR 中文件? 一个好处是可以轻松设置主文件清单并使其可执行。
具体案例是一个Java 重构文本工具。 我想用类似的东西来运行它:
java -jar rst.jar
据我所知,它没有依赖项,这表明它不应该是一个简单的单文件工具,但下载的 ZIP 文件包含很多库。
0 11-30-07 10:01 jrst-0.8.1/
922 11-30-07 09:53 jrst-0.8.1/jrst.bat
898 11-30-07 09:53 jrst-0.8.1/jrst.sh
2675 11-30-07 09:42 jrst-0.8.1/readmeEN.txt
108821 11-30-07 09:59 jrst-0.8.1/jrst-0.8.1.jar
2675 11-30-07 09:42 jrst-0.8.1/readme.txt
0 11-30-07 10:01 jrst-0.8.1/lib/
81508 11-30-07 09:49 jrst-0.8.1/lib/batik-util-1.6-1.jar
2450757 11-30-07 09:49 jrst-0.8.1/lib/icu4j-2.6.1.jar
559366 11-30-07 09:49 jrst-0.8.1/lib/commons-collections-3.1.jar
83613 11-30-07 09:49 jrst-0.8.1/lib/commons-io-1.3.1.jar
207723 11-30-07 09:49 jrst-0.8.1/lib/commons-lang-2.1.jar
52915 11-30-07 09:49 jrst-0.8.1/lib/commons-logging-1.1.jar
260172 11-30-07 09:49 jrst-0.8.1/lib/commons-primitives-1.0.jar
313898 11-30-07 09:49 jrst-0.8.1/lib/dom4j-1.6.1.jar
1994150 11-30-07 09:49 jrst-0.8.1/lib/fop-0.93-jdk15.jar
55147 11-30-07 09:49 jrst-0.8.1/lib/activation-1.0.2.jar
355030 11-30-07 09:49 jrst-0.8.1/lib/mail-1.3.3.jar
77977 11-30-07 09:49 jrst-0.8.1/lib/servlet-api-2.3.jar
226915 11-30-07 09:49 jrst-0.8.1/lib/jaxen-1.1.1.jar
153253 11-30-07 09:49 jrst-0.8.1/lib/jdom-1.0.jar
50789 11-30-07 09:49 jrst-0.8.1/lib/jewelcli-0.41.jar
324952 11-30-07 09:49 jrst-0.8.1/lib/looks-1.2.2.jar
121070 11-30-07 09:49 jrst-0.8.1/lib/junit-3.8.1.jar
358085 11-30-07 09:49 jrst-0.8.1/lib/log4j-1.2.12.jar
72150 11-30-07 09:49 jrst-0.8.1/lib/logkit-1.0.1.jar
342897 11-30-07 09:49 jrst-0.8.1/lib/lutinwidget-0.9.jar
2160934 11-30-07 09:49 jrst-0.8.1/lib/docbook-xsl-nwalsh-1.71.1.jar
301249 11-30-07 09:49 jrst-0.8.1/lib/xmlgraphics-commons-1.1.jar
68610 11-30-07 09:49 jrst-0.8.1/lib/sdoc-0.5.0-beta.jar
3149655 11-30-07 09:49 jrst-0.8.1/lib/xalan-2.6.0.jar
1010675 11-30-07 09:49 jrst-0.8.1/lib/xercesImpl-2.6.2.jar
194205 11-30-07 09:49 jrst-0.8.1/lib/xml-apis-1.3.02.jar
78440 11-30-07 09:49 jrst-0.8.1/lib/xmlParserAPIs-2.0.2.jar
86249 11-30-07 09:49 jrst-0.8.1/lib/xmlunit-1.1.jar
108874 11-30-07 09:49 jrst-0.8.1/lib/xom-1.0.jar
63966 11-30-07 09:49 jrst-0.8.1/lib/avalon-framework-4.1.3.jar
138228 11-30-07 09:49 jrst-0.8.1/lib/batik-gui-util-1.6-1.jar
216394 11-30-07 09:49 jrst-0.8.1/lib/l2fprod-common-0.1.jar
121689 11-30-07 09:49 jrst-0.8.1/lib/lutinutil-0.26.jar
76687 11-30-07 09:49 jrst-0.8.1/lib/batik-ext-1.6-1.jar
124724 11-30-07 09:49 jrst-0.8.1/lib/xmlParserAPIs-2.6.2.jar
正如您所看到的,最好不需要手动执行此操作。
到目前为止,我只尝试过 AutoJar 和 ProGuard,它们都相当容易运行。 JAR 文件中的常量池似乎存在一些问题。
显然 jrst 有点坏了,所以我会尽力修复它。 Maven pom.xml
文件显然也被破坏了,所以我'在修复 jrst 之前,我必须先解决这个问题...我感觉自己就像一个 bug 磁铁 :-)
更新:我从来没有抽出时间来修复这个应用程序,但我签出了 Eclipse 的“可运行 JAR 导出向导”基于 fat JAR。 我发现这非常容易用于部署我自己的代码。
其他一些优秀的建议可能更适合在非 Eclipse 环境中构建,oss 可能应该使用 蚂蚁。 (Maven,到目前为止刚刚给我带来了痛苦,但其他人喜欢它。)
Is there a tool or script which easily merges a bunch of JAR files into one JAR file? A bonus would be to easily set the main-file manifest and make it executable.
The concrete case is a Java restructured text tool. I would like to run it with something like:
java -jar rst.jar
As far as I can tell, it has no dependencies which indicates that it shouldn't be an easy single-file tool, but the downloaded ZIP file contains a lot of libraries.
0 11-30-07 10:01 jrst-0.8.1/
922 11-30-07 09:53 jrst-0.8.1/jrst.bat
898 11-30-07 09:53 jrst-0.8.1/jrst.sh
2675 11-30-07 09:42 jrst-0.8.1/readmeEN.txt
108821 11-30-07 09:59 jrst-0.8.1/jrst-0.8.1.jar
2675 11-30-07 09:42 jrst-0.8.1/readme.txt
0 11-30-07 10:01 jrst-0.8.1/lib/
81508 11-30-07 09:49 jrst-0.8.1/lib/batik-util-1.6-1.jar
2450757 11-30-07 09:49 jrst-0.8.1/lib/icu4j-2.6.1.jar
559366 11-30-07 09:49 jrst-0.8.1/lib/commons-collections-3.1.jar
83613 11-30-07 09:49 jrst-0.8.1/lib/commons-io-1.3.1.jar
207723 11-30-07 09:49 jrst-0.8.1/lib/commons-lang-2.1.jar
52915 11-30-07 09:49 jrst-0.8.1/lib/commons-logging-1.1.jar
260172 11-30-07 09:49 jrst-0.8.1/lib/commons-primitives-1.0.jar
313898 11-30-07 09:49 jrst-0.8.1/lib/dom4j-1.6.1.jar
1994150 11-30-07 09:49 jrst-0.8.1/lib/fop-0.93-jdk15.jar
55147 11-30-07 09:49 jrst-0.8.1/lib/activation-1.0.2.jar
355030 11-30-07 09:49 jrst-0.8.1/lib/mail-1.3.3.jar
77977 11-30-07 09:49 jrst-0.8.1/lib/servlet-api-2.3.jar
226915 11-30-07 09:49 jrst-0.8.1/lib/jaxen-1.1.1.jar
153253 11-30-07 09:49 jrst-0.8.1/lib/jdom-1.0.jar
50789 11-30-07 09:49 jrst-0.8.1/lib/jewelcli-0.41.jar
324952 11-30-07 09:49 jrst-0.8.1/lib/looks-1.2.2.jar
121070 11-30-07 09:49 jrst-0.8.1/lib/junit-3.8.1.jar
358085 11-30-07 09:49 jrst-0.8.1/lib/log4j-1.2.12.jar
72150 11-30-07 09:49 jrst-0.8.1/lib/logkit-1.0.1.jar
342897 11-30-07 09:49 jrst-0.8.1/lib/lutinwidget-0.9.jar
2160934 11-30-07 09:49 jrst-0.8.1/lib/docbook-xsl-nwalsh-1.71.1.jar
301249 11-30-07 09:49 jrst-0.8.1/lib/xmlgraphics-commons-1.1.jar
68610 11-30-07 09:49 jrst-0.8.1/lib/sdoc-0.5.0-beta.jar
3149655 11-30-07 09:49 jrst-0.8.1/lib/xalan-2.6.0.jar
1010675 11-30-07 09:49 jrst-0.8.1/lib/xercesImpl-2.6.2.jar
194205 11-30-07 09:49 jrst-0.8.1/lib/xml-apis-1.3.02.jar
78440 11-30-07 09:49 jrst-0.8.1/lib/xmlParserAPIs-2.0.2.jar
86249 11-30-07 09:49 jrst-0.8.1/lib/xmlunit-1.1.jar
108874 11-30-07 09:49 jrst-0.8.1/lib/xom-1.0.jar
63966 11-30-07 09:49 jrst-0.8.1/lib/avalon-framework-4.1.3.jar
138228 11-30-07 09:49 jrst-0.8.1/lib/batik-gui-util-1.6-1.jar
216394 11-30-07 09:49 jrst-0.8.1/lib/l2fprod-common-0.1.jar
121689 11-30-07 09:49 jrst-0.8.1/lib/lutinutil-0.26.jar
76687 11-30-07 09:49 jrst-0.8.1/lib/batik-ext-1.6-1.jar
124724 11-30-07 09:49 jrst-0.8.1/lib/xmlParserAPIs-2.6.2.jar
As you can see, it is somewhat desirable to not need to do this manually.
So far I've only tried AutoJar and ProGuard, both of which were fairly easy to get running. It appears that there's some issue with the constant pool in the JAR files.
Apparently jrst is slightly broken, so I'll make a go of fixing it. The Maven pom.xml
file was apparently broken too, so I'll have to fix that before fixing jrst ... I feel like a bug-magnet :-)
Update: I never got around to fixing this application, but I checked out Eclipse's "Runnable JAR export wizard" which is based on a fat JAR. I found this very easy to use for deploying my own code.
Some of the other excellent suggestions might be better for builds in a non-Eclipse environment, oss probably should make a nice build using Ant. (Maven, so far has just given me pain, but others love it.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
听起来 Apache Ant 就是您正在寻找的东西。
Sounds like Apache Ant is what you're looking for.
您应该使用 Maven 着色插件来做到这一点。 我经常使用maven来构建独立的jar文件,它是如此强大
查看更多:
http://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html
You should use maven shading plugin to do that. I often use maven to build standalone jar file and it's so powerful
See more:
http://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html
我认为你需要的工具是JarSplice:http://ninjacave.com/jarsplice
它不需要需要 Ant 或 Maven,有自己的 GUI,使用起来很简单,并且完全按照您的要求执行 --> 它将多个 jar 文件的内容合并为一个文件(请注意,它仍然需要添加自己的类加载器)。
I think that the tool you need here is JarSplice: http://ninjacave.com/jarsplice
It does not require Ant or Maven, has its own GUI, it is straightforward to use and do exactly what you asked --> It Merges the content of several jar files into a single one (please note it still need to add its own classloader).
或者使用Maven程序集插件(mvn assembly:程序集)
Or using the Maven assembly plugin (mvn assembly:assembly)
有一个名为 autojar 的工具,它将扫描您的字节码并使用其中的类编译 .jar 文件查找,包括引用(导入)的类。
不过,并不总是适用于像 Spring 这样的东西,您在配置中指定类名并由框架加载。
There is a tool called autojar which will scan your bytecode and compile a .jar file with the classes it finds, including referenced (imported) classes.
Doesn't always work with something like Spring, though, where you specify the classnames in configuration and it gets loaded by the framework.
您可以使用 JarJar ,它将使用包阴影来确保您的 JAR 文件不会发生冲突和其他人。
You can use JarJar which will use package shadowing to make sure your JAR file doesn't conflict with others.
One-JAR 0.97 刚刚在 http://one-jar.sourceforge.net 发布,并且它已扩展为对框架的支持,例如 Spring 和 Guice,这可能会给其他方法带来麻烦。 它还处理类加载器反转——其中一些 JAR 文件位于 One-JAR 外部(例如,JDBC 驱动程序可能不捆绑发货)。
One-JAR 是命令行,带有 Ant 和 Maven 2 个插件。 只需使用“jar”工具即可轻松构建。
我还可以推荐 Ference Hechler 所写的 Eclipse Jar Exporter(可运行):他在提出一种包装一组 JAR 文件的简单方法方面做得非常出色。 他和我一起开发了 One-JAR,但 Jar Exporter 基于不同的代码库。
One-JAR 0.97 has just been released at http://one-jar.sourceforge.net, and it has been extended wih support for frameworks such as Spring and Guice, which may present trouble to other approaches. It also handles classloader-inversion -- where some JAR files are external to the One-JAR (for example, JDBC drivers which may not be shipped bundled).
One-JAR is command-line, with Ant and Maven 2 plugins. It's also simple to build just using the "jar" tool.
I can also recommend the Eclipse Jar Exporter (Runnable) on which Ference Hechler wrote: he did a great job in coming up with a simple approach to wrapping a set of JAR files. He and I worked on One-JAR, but the Jar Exporter is based on a different codebase.
(基于安德里安的):
(based on Andrian's):
ProGuard 不仅可以将您的 JAR 文件打包成一个,还可以优化、清理或混淆类文件,使得生成的 JAR 文件比之前所有 JAR 文件的总和小得多。
我实际上用 JRST 工具尝试了 ProGuard,正如您所报告的那样。 我试图追踪这个问题,发现它与一个错误有关jrst 引用的 ICU4J 库。 问题是,使用的 ICU 版本现在已经过时了。 因此,我将
icu.jar
文件替换为 ICU4J 3.2 版。 现在,ProGuard 发现了许多与 JRST 库不一致的其他错误/警告。我的猜测是 ProGuard 按预期工作,但 jrst 的库不一致。 我不知道除了与开发人员交谈之外,您是否还能做更多的事情,因为他们应该检查和更新项目的依赖项。
There is ProGuard which does not only pack your JAR files into one, but it can also optimize, cleanup or obfuscate your class files, making the resulting JAR file much smaller than the sum of all JAR files before.
I actually tried ProGuard with the JRST tool, and it is as you reported. I tried to track the problem down and found it to relate to a bug in the ICU4J library referenced by jrst. The problem is, that the used ICU version is far outdated right now. So I replaced the
icu.jar
file with ICU4J version 3.2. Now ProGuard finds a bunch of other errors/warnings about incosistencies with the libraries of JRST.My guess is that ProGuard works as expected, but the libraries of jrst are just not consistent. I don't know if you can do much more than talk with its developers since they should check and update the dependencies of the project.
如果您是 Maven 用户,通常程序集插件会执行您想要的操作,或者可能会执行您想要的操作插件,在某些情况下是组合。
使用程序集插件,您可以在项目中放置一个带有任何必要设置的清单文件,尽管默认设置通常相当不错。 然后完成构建,
或者如果您有更特殊的事情要处理,则使用其他目标之一。 所有要包含的 JAR 文件均由 Maven 的依赖关系解析器获取。 如果您使用阴影插件,它通常是安装目标的一部分,在我现在正在做的一个特定项目中,我所做的
assembly:single 目标是解决生命周期问题,在本例中在 Spring 应用程序中。
If you are a Maven user, typically the assembly plugin do what you want, or potentially the shade plugin, and in some cases a combination.
With the assembly plugin you put a manifest file in your project with any necessary settings, although the defaults are usually quite good. Building is then done with
Or if you have more special things to deal with, one of the other goals. All JAR files to include, are picked up by Maven's dependency resolver. If you use the shade plugin, it is typically part of the install goal, and in one particular project I'm doing now I do
The
assembly:single
goal is to work around lifetime issues, in this case in a Spring application.尝试了几种不同的解决方案后,我发现 One-JAR 最容易使用,并且已经成功make 正是这样做的:生成一个包含我需要的所有内容的可执行 JAR。
One-JAR 使用可以导航嵌套资源的自定义类加载器。 查看下载中的 .bat 文件,它看起来像 jrst-0.8.1.jar 中的 org.codelutin.jrst.JRST 是主类,因此您的清单应该如下所示:
真正酷的是 One- JAR 将为您处理命令行参数的传递。 类路径由自定义类加载器处理,假设您需要的所有资源都捆绑到单个 JAR 中。
使用 One-JAR 最简单的方法是使用 ant; 有一个自定义的“one-jar”ant 任务,其工作原理如下(假设您的清单名为“rst.mf”):
Having tried a few different solutions, I found One-JAR the easiest to work with, and have managed to make do exactly that: produce a single, executable JAR which contains everything I need.
One-JAR uses a custom class-loader which can navigate nested resources. Look at the .bat file in the download, it looks like org.codelutin.jrst.JRST in the jrst-0.8.1.jar is the main class, so your manifest should look like this:
The really cool thing is that One-JAR will handle passing on command-line arguments for you. The classpath is handled by the custom class loader, assuming all the resources you need are bundled into the single JAR.
The easiest way to use One-JAR is with ant; there's a custom "one-jar" ant task which works as follows (assuming your manifest is called "rst.mf"):
Eclipse 3.4 JDT 的 Runnable JAR 导出向导。
在 Eclipse 3.5 中,这一点得到了扩展。 现在您可以选择如何处理引用的 JAR 文件。
Eclipse 3.4 JDT's Runnable JAR export wizard.
In Eclipse 3.5, this has been extended. Now you can chose how you want to treat your referenced JAR files.
Ant 的 zipfileset 可以完成这项工作
Ant's
zipfileset
does the job