eclipse 打包具有大量依赖项的 Java 应用程序的最佳方法是什么?

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

Whats best way to package a Java Application with lots of dependencies?

javaeclipsedeployment

提问by Omar Kooheji

I'm writing a java app using eclipse which references a few external jars and requires some config files to be user accessable.

我正在使用 Eclipse 编写一个 Java 应用程序,该应用程序引用了一些外部 jar,并且需要一些配置文件可供用户访问。

  1. What is the best way to package it up for deployment?

  2. My understanding is that you cant put Jars inside another jar file, is this correct?

  3. Can I keep my config files out of the jars and still reference them in the code? Or should the path to the config file be a command line argument?

  4. Are there any third party plugins for eclipse to help make this easier? I'm using an ant build file at the moment but I'm not sure I know what I'm doing.

  5. Is there an equivelent of the deployment projects in Visual studio, that will figure out everything you need and just make an installer? I've used install4j before, and it was powerful if no where near as automated as .Net deployment projects.

  1. 将其打包以进行部署的最佳方法是什么?

  2. 我的理解是你不能把 Jars 放在另一个 jar 文件中,这是正确的吗?

  3. 我可以将我的配置文件保留在 jars 之外并仍然在代码中引用它们吗?或者配置文件的路径应该是命令行参数?

  4. eclipse 是否有任何第三方插件可以帮助简化此操作?我目前正在使用 ant 构建文件,但我不确定我在做什么。

  5. Visual Studio 中是否有等效的部署项目,它会找出您需要的一切并制作安装程序?我以前使用过 install4j,如果没有像 .Net 部署项目那样自动化,它就很强大。

Cheers.

干杯。

采纳答案by JodaStephen

There is no one 'best way'. It depends on whether you are deploying a swing application, webstart, applet, library or web application. Each is different.

没有一种“最好的方法”。这取决于您要部署的是 Swing 应用程序、webstart、小程序、库还是 Web 应用程序。每个都是不同的。

On point 2, you are correct. Jar files cannot contain other jar files. (Well, technically they can, its just that the inner jar file won't be on your classpath, effectively meaning that jar files do not contain jar files).

在第 2 点,你是对的。Jar 文件不能包含其他 jar 文件。(好吧,从技术上讲,他们可以,只是内部 jar 文件不会在您的类路径上,这实际上意味着 jar 文件不包含 jar 文件)。

On point 3, you certainly can reference config files outside the jar file. You can typically reference a config file as a file or a resource. If you use the resource approach, it typically comes from the classpath (can be in a jar). If you use a file, then you specify the filename (not in a jar).

在第 3 点,您当然可以引用 jar 文件之外的配置文件。您通常可以将配置文件作为文件或资源来引用。如果您使用资源方法,它通常来自类路径(可以在 jar 中)。如果使用文件,则指定文件名(不在 jar 中)。

In general, most Java developers would use Apache Antto achieve deployment. Its well documented, so take a look.

一般来说,大多数 Java 开发人员会使用Apache Ant来实现部署。它有据可查,所以看看吧。

回答by toolkit

(1) An alternative to ant that you may wish to consider is maven.

(1) 您可能希望考虑的 ant 替代品是maven

A brief intro to maven can be found here.

可以在此处找到 Maven 的简要介绍。

For building a JAR, maven has the jarplugin, which can automate the process of ensuring all dependent jars are listed in your jar's manifest.

为了构建 JAR,maven 有jar插件,它可以自动化确保所有依赖 jar 都列在你的 jar 清单中的过程。

If you're using eclipse, then download the maven integrationas well.

如果您使用的是 eclipse,那么也请下载maven 集成

(2) Another alternative is to use OneJar(disclaimer: haven't tried this myself).

(2) 另一种选择是使用OneJar(免责声明:我自己没有尝试过)。

回答by Alex Miller

The answers vary depending on what kind of thing you're building.

答案因您正在构建的事物类型而异。

If you're building a library, it's best to distribute your work as a jar file. It's possible to refer to your jar dependencies via the Class-path attribute in your jar manifest, although I generally think that's uncool. That attribute was designed for applets and it's used infrequently enough in libs that when this technique pulls stuff into the classpath (particularly common stuff the user might already be using), you can get unexpected version conflicts. And it's hard to track down why you're seeing them.

如果您正在构建一个库,最好将您的工作作为 jar 文件分发。可以通过 jar 清单中的 Class-path 属性来引用 jar 依赖项,尽管我通常认为这很不酷。该属性是为小程序设计的,它在库中的使用频率很低,以至于当这种技术将内容拉入类路径时(尤其是用户可能已经在使用的常见内容),您可能会遇到意外的版本冲突。并且很难找到你为什么会看到它们。

Publishing a jar to a Maven repo with pom info to track dependencies is an excellent choice for libraries as well. If you do that, please publish your Maven coordinates in your docs!

使用 pom 信息将 jar 发布到 Maven 存储库以跟踪依赖项也是库的绝佳选择。如果您这样做,请在您的文档中发布您的 Maven 坐标!

If you're building an app, the two popular choices are to distribute a zip/tar/whatever of a deployment structure OR to use an installer program. If the program is a server-ish kind of thing, the former is far more common. The latter is more common for clients. Generally, the installer program is just going to lay out the deployment structure and maybe do some extra tasks like installing in OS-specific locations.

如果您正在构建一个应用程序,两个流行的选择是分发 zip/tar/任何部署结构或使用安装程序。如果程序是类似服务器的东西,前者更为常见。后者更常见于客户。通常,安装程序只会布置部署结构,并且可能会执行一些额外的任务,例如在特定于操作系统的位置进行安装。

To build your deployment structure (aka "kit") you'll want to create a repeatable process in whatever build system you're using. Ant has copious examples of this and Maven has the assembly plugins that can help. Generally you'll want to include a jar of your code, any dependencies, scripts to start the program, maybe a JRE, and any other resources you might need.

要构建您的部署结构(又名“套件”),您需要在您使用的任何构建系统中创建一个可重复的过程。Ant 有很多这方面的例子,Maven 有可以提供帮助的程序集插件。通常,您需要包含一个 jar 代码、任何依赖项、启动程序的脚本、JRE 以及您可能需要的任何其他资源。

If you want to create an installer, there are many options both free and commercial. Some folks I know have recently had good experiences with the free IzPackbut check out your options.

如果您想创建安装程序,有许多免费和商业选项。我认识的一些人最近对免费的IzPack有很好的体验,但请查看您的选择。

回答by Saiyine

You should try FatJar. It's an Eclipse plugin that with just a right click at the Project can build a JAR file with all you need to run the application, including the necesary third party JAR.

你应该试试FatJar。这是一个 Eclipse 插件,只需在项目上单击鼠标右键即可构建一个 JAR 文件,其中包含运行应用程序所需的一切,包括必要的第三方 JAR。

We use it everyday, in conjuction with JSmoothto create the executables, to deploy our software packages to our customers, and works like a charm.

我们每天都使用它,与JSmooth结合使用来创建可执行文件,将我们的软件包部署给我们的客户,并且工作起来很迷人。

回答by Daniel Hiller

Well, if you are speaking of deployment of a standalone desktop application:

好吧,如果您说的是独立桌面应用程序的部署:

Before we switched to web startwe have been creating three deployment archives, one for windows, one for mac and one for other platforms.

在我们切换到 web start 之前,我们已经创建了三个部署档案,一个用于 windows,一个用于 mac,一个用于其他平台。

On windows we have successfully used the Nullsoft Scriptable Install System(known for it's usage by the older winamp versions) and its ant task, although some drawbacks are:

在 Windows 上,我们已经成功地使用了Nullsoft Scriptable Install System(以使用较旧的 winamp 版本而闻名)及其ant task,尽管存在一些缺点:

  • It is only usable on windows AFAIR
  • You have to do some work by hand, i.e. customizing the wizard-created script AFAIR
  • 它只能在 windows AFAIR 上使用
  • 您必须手动完成一些工作,即自定义向导创建的脚本 AFAIR

It can create a windows installation with start menu entries on the other hand. There also exists an eclipse plugin for integrated NSIS shell script editing.

另一方面,它可以使用开始菜单条目创建 Windows 安装。还有一个用于集成 NSIS shell 脚本编辑的 eclipse 插件。

On Mac OS X there is an ant taskto create an .app file from your java files so that you can start it like a native os x application. But beware of not writing any setting to your home dir and using the the application dir instead.

在 Mac OS X 上,有一个ant 任务可以从你的 java 文件创建一个 .app 文件,这样你就可以像本地 os x 应用程序一样启动它。但请注意不要将任何设置写入您的主目录,而是使用应用程序目录。

For others you have to expect they are in a un*x env and deploy your app with a shell script to start the application.

对于其他人,您必须期望他们在 un*x 环境中并使用 shell 脚本部署您的应用程序以启动应用程序。

In any case you may have to deploy your custom policy fileto get access rights for your application.

在任何情况下,您可能都必须部署自定义策略文件才能获得应用程序的访问权限。

If you want to get rid of all the packaging and stuff you should seriously consider using web start. We have saved much time since switching to it, i.e. simplified our deployment process, takes care of updates etc.

如果你想摆脱所有的包装和东西,你应该认真考虑使用web start。自从切换到它后,我们节省了很多时间,即简化了我们的部署过程,处理更新等。

Update 2014

2014 年更新

Use maven assembly plugin, see section "Creating an executable jar"

使用Maven 程序集插件,请参阅“创建可执行 jar”部分

回答by Andy Gherna

You could look at other java projects (e.g. JMeter, SquirrelSQL, JEdit, Cernunnos, etc.). Each package their applications slightly differently, so consider your goals when you review these.

您可以查看其他 Java 项目(例如 JMeter、SquirrelSQL、JEdit、Cernunnos 等)。每个人打包他们的应用程序都略有不同,因此在查看这些内容时请考虑您的目标。

回答by stu

Ant. It's not the best thing in the world, but it's standard, it's apache, and it works. There's some good examples on the web how to make a simple build.xml for any, and it's got some features like the 'war' task that knows how to put all the basic stuff (classes, web.xml etc) in the jar file for you. You can also tell it to pick up other config files and jars and it will happily do it. It's also really smart about what to compile. You give it a directory, and it finds all the java files and builds them only if their classfile is out of date, so you get some of the traditional make functionality for free without much effort.

蚂蚁。它不是世界上最好的东西,但它是标准的,它是 apache,它可以工作。网上有一些很好的例子,如何为任何人制作一个简单的 build.xml,它有一些功能,比如“War”任务,知道如何将所有基本内容(类、web.xml 等)放入 jar 文件中为你。您还可以告诉它选择其他配置文件和 jar,它会很乐意这样做。编译什么也很聪明。你给它一个目录,它会找到所有的 java 文件,并且只有当它们的类文件过期时才构建它们,所以你可以毫不费力地免费获得一些传统的 make 功能。