java 将外部库放入 JAR?

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

Put external library to the JAR?

java

提问by user

I have added some external libraries to my java project (in netbeans).
Is it possible to put the external jar library to the java archive (and not to put them into a separate (for example) "lib" directory)?

我在我的 java 项目中添加了一些外部库(在 netbeans 中)。
是否可以将外部 jar 库放入 java 存档(而不是将它们放入单独的(例如)“lib”目录中)?

采纳答案by abyx

Of course you can. There are a few open source projects that can be downloaded with a "bundle jar" that contains all the dependencies.
You need to extract all the jars and then jar them again to one file.
An example of doing this with Ant can be seen here.

当然可以。有一些开源项目可以使用包含所有依赖项的“捆绑 jar”下载。
您需要提取所有 jar,然后将它们再次 jar 到一个文件中。
可以在此处查看使用 Ant 执行此操作的示例。

回答by Pascal Thivent

Yes, this is possible and the resulting jar is actually called an "uberjar" or "megajar". Basically, to create an "uberjar", you'll need to extract the content of the external library (using jar -x) and to repack it in your own jar (with jar -c). This can be achieved by hand, or with a build tool like Ant (and the optional support of a project like One-JAR) or Maven which has built-in support for this through the maven-assembly-plugin (or the maven-shade-plugin)

是的,这是可能的,生成的 jar 实际上称为“uberjar”或“megajar”。基本上,要创建“uberjar”,您需要提取外部库的内容(使用jar -x)并将其重新打包到您自己的 jar 中(使用jar -c)。这可以通过手工实现,或者使用像 Ant(以及像One-JAR这样的项目的可选支持)或 Maven 这样的构建工具来实现,它通过 maven-assembly-plugin(或 maven-shade -插入)

回答by Robert Christie

The JarJarproject allows you to package depend jars into a single jar file for distribution.

JarJar项目可以让你依靠打包成瓶分发一个JAR文件。

The project provides an ant task to do the packaging and as netbeans projects use Ant for their build process, you should be able to integrate it into your build fairly easily.

该项目提供了一个 ant 任务来进行打包,因为 netbeans 项目在其构建过程中使用 Ant,您应该能够相当容易地将它集成到您​​的构建中。

回答by stacker

If it is about packing jars into jars this is always done for web apps in war or ear files. If your app is standalone you can unzip the jars and pack them alltogether in one resulting jar using the Ant jar and unjar tasks: http://ant.apache.org/manual/Tasks/unzip.htmlBut I would not recommend to do this.

如果是关于将 jars 打包成 jars,这总是用于 war 或 ear 文件中的 web 应用程序。如果您的应用程序是独立的,您可以使用 Ant jar 和 unjar 任务将 jar 解压缩并将它们全部打包到一个生成的 jar 中:http://ant.apache.org/manual/Tasks/unzip.html 但我不建议这样做这。

回答by gpampara

If you are using Maven, you can consider the Maven shade plugin, or the assembly plugin.

如果你使用 Maven,你可以考虑Maven shade plugin,或者 assembly plugin。

回答by Raekye

There's an easy way to do it with the built in ant scripts.

使用内置的 ant 脚本有一种简单的方法可以做到这一点。

There was another SO question pointing to this article: http://mavistechchannel.wordpress.com/2010/08/17/how-to-build-a-single-jar-file-with-external-libs/

还有另一个指向这篇文章的问题:http: //mavistechchannel.wordpress.com/2010/08/17/how-to-build-a-single-jar-file-with-external-libs/

I added it, but was such a noob I didn't know how to use it. This articlehad a great visual tutorial. Basically, after modifying your ant script, go to files tab (in explorer on left) => right click on the xml => run tasks => other tasks => package for store (or whatever you named the script)

我添加了它,但它是如此的菜鸟我不知道如何使用它。这篇文章有一个很棒的视觉教程。基本上,修改您的 ant 脚本后,转到文件选项卡(在左侧的资源管理器中)=> 右键单击​​ xml => 运行任务 => 其他任务 => 存储包(或您命名脚本的任何内容)