Java 将一个版本合并到一个 JAR 文件中的最简单方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/81260/
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-11 08:03:35  来源:igfitidea点击:

Easiest way to merge a release into one JAR file

javajarmergerestructuredtextuberjar

提问by tovare

Is there a tool or script which easily merges a bunch of JARfiles into one JAR file? A bonus would be to easily set the main-file manifest and make it executable.

是否有一种工具或脚本可以轻松地将一堆JAR文件合并为一个 JAR 文件?一个好处是可以轻松设置主文件清单并使其可执行。

The concrete case is a Java restructured text tool. I would like to run it with something like:

具体案例是一个Java重组文本工具。我想用类似的东西运行它:

java -jar rst.jar

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.

据我所知,它没有依赖项,这表明它不应该是一个简单的单文件工具,但下载的 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

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.

到目前为止,我只尝试过 AutoJar 和 ProGuard,这两个都相当容易上手。JAR 文件中的常量池似乎存在一些问题。

Apparently jrst is slightly broken, so I'll make a go of fixing it. The Mavenpom.xmlfile was apparently broken too, so I'll have to fix that before fixing jrst ... I feel like a bug-magnet :-)

显然 jrst 有点坏了,所以我会努力修复它。在Maven的pom.xml文件显然是打破过,所以我必须解决这个问题JRST固定之前......我觉得自己像一个错误磁铁:-)



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.

更新:我从来没有想过修复这个应用程序,但我查看了Eclipse的“Runnable JAR 导出向导”,它基于一个胖 JAR。我发现这很容易用于部署我自己的代码。

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

其他一些很好的建议可能更适合在非 Eclipse 环境中构建, oss 可能应该使用Ant进行很好的构建。(Maven,到目前为止只是让我感到痛苦,但其他人喜欢它。)

采纳答案by Stephen Denne

Eclipse 3.4 JDT's Runnable JAR export wizard.

Eclipse 3.4 JDT 的 Runnable JAR 导出向导。

In Eclipse 3.5, this has been extended. Now you can chose how you want to treat your referenced JAR files.

在 Eclipse 3.5 中,这已得到扩展。现在,您可以选择如何处理引用的 JAR 文件。

回答by larsivi

If you are a Mavenuser, typically the assembly plugin do what you want, or potentially the shade plugin, and in some cases a combination.

如果您是Maven用户,通常程序集插件可以满足您的需求,或者可能是 shade 插件,在某些情况下还可以组合使用。

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

使用程序集插件,您可以在项目中放置一个带有任何必要设置的清单文件,尽管默认值通常很好。然后构建完成

mvn assembly:assembly

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

或者,如果您有更多特殊的事情要处理,请选择其他目标之一。要包含的所有 JAR 文件都由 Maven 的依赖项解析器获取。如果您使用 shade 插件,它通常是安装目标的一部分,并且在我现在正在做的一个特定项目中

mvn install
mvn assembly:single

The assembly:singlegoal is to work around lifetime issues, in this case in a Springapplication.

assembly:single目标是解决问题的寿命,在这种情况下,在一个春天的应用。

回答by PaulF

Sounds like Apache Antis what you're looking for.

听起来您正在寻找Apache Ant

回答by PaulF

There is a tool called autojarwhich will scan your bytecode and compile a .jar file with the classes it finds, including referenced (imported) classes.

有一个名为autojar的工具,它将扫描您的字节码并使用它找到的类编译一个 .jar 文件,包括引用(导入)类。

Doesn't always work with something like Spring, though, where you specify the classnames in configuration and it gets loaded by the framework.

但是,并不总是与 Spring 之类的东西一起使用,您在配置中指定类名并由框架加载它。

回答by Ashley Mercer

Having tried a few different solutions, I found One-JARthe easiest to work with, and have managed to make do exactly that: produce a single, executable JAR which contains everything I need.

尝试了几种不同的解决方案后,我发现One-JAR最容易使用,并且设法做到了:生成一个包含我需要的所有内容的单个可执行 JAR。

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:

One-JAR 使用可以导航嵌套资源的自定义类加载器。查看下载中的.bat文件,jrst-0.8.1.jar中的org.codelutin.jrst.JRST是主类,所以你的manifest应该是这样的:

Main-Class: com.simontuffs.onejar.Boot
One-Jar-Main-Class: org.codelutin.jrst.JRST

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.

真正酷的是 One-JAR 将为您处理传递命令行参数。类路径由自定义类加载器处理,假设您需要的所有资源都捆绑到单个 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"):

使用 One-JAR 的最简单方法是使用 ant;有一个自定义的“one-jar”ant 任务,其工作方式如下(假设您的清单名为“rst.mf”):

<target name="jar-rst">
    <one-jar destfile="rst.jar" manifest="rst.mf">
        <main jar="jrst-0.8.1.jar" />
        <lib>
            <fileset dir="${pathToJars}">
                <include name="batik-util-1.6-1.jar" />
                <include name="icu4j-2.6.1.jar" />
                <include name="commons-collections-3.1.jar" />
                <!-- Snip -->
            </fileset>
        </lib>
    </one-jar>
</target>

回答by jrudolph

There is ProGuardwhich 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.

ProGuard的不只是收拾你的JAR文件合并为一个,但它也可以优化,清理或混淆您的类文件,使所产生的JAR文件不是所有的JAR文件之前总和小得多。

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 bugin the ICU4J library referenced by jrst. The problem is, that the used ICU version is far outdated right now. So I replaced the icu.jarfile with ICU4J version 3.2. Now ProGuard finds a bunch of other errors/warnings about incosistencies with the libraries of JRST.

我实际上用 JRST 工具尝试了 ProGuard,正如你所报告的那样。我试图追踪这个问题,发现它与jrst 引用的 ICU4J 库中的一个错误有关。问题是,使用的 ICU 版本现在已经过时了。所以我icu.jar用ICU4J 3.2版替换了该文件。现在 ProGuard 发现了一堆关于与 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.

我的猜测是 ProGuard 按预期工作,但 jrst 的库并不一致。我不知道您是否可以做更多的事情而不是与开发人员交谈,因为他们应该检查和更新项目的依赖项。

回答by Philip Helger

Or using the Maven assembly plugin (mvn assembly:assembly)

或者使用 Maven 程序集插件 (mvn assembly:assembly)

回答by Steve g

You can use JarJarwhich will use package shadowing to make sure your JAR file doesn't conflict with others.

您可以使用JarJar,它将使用包阴影来确保您的 JAR 文件不会与其他文件冲突。

回答by akuhn

Ant's zipfilesetdoes the job

蚂蚁的zipfileset工作

<jar id="files" jarfile="all.jar">
    <zipfileset src="first.jar" includes="**/*.java **/*.class"/>
    <zipfileset src="second.jar" includes="**/*.java **/*.class"/>
</jar>

回答by simontuffs

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 Springand 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, JDBCdrivers which may not be shipped bundled).

One-JAR 0.97 刚刚在http://one-jar.sourceforge.net发布,并且已经扩展了对SpringGuice等框架的支持,这可能会给其他方法带来麻烦。它还处理类加载器反转——其中一些 JAR 文件位于 One-JAR 的外部(例如,可能未捆绑交付的JDBC驱动程序)。

One-JAR is command-line, with Antand Maven2 plugins. It's also simple to build just using the "jar" tool.

One-JAR 是命令行,带有AntMaven2 插件。使用“jar”工具构建也很简单。

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.

我还可以推荐 Ference Hechler 所写的 Eclipse Jar Exporter (Runnable):他在提出包装一组 JAR 文件的简单方法方面做得非常出色。他和我在 One-JAR 上工作,但 Jar 导出器基于不同的代码库。