打包并运行具有 spring 依赖项的 Java 应用程序 [英] Package and run a Java application with spring dependencies

查看:35
本文介绍了打包并运行具有 spring 依赖项的 Java 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个独立的 Java 应用程序,该应用程序具有大量依赖项(Apache Commons 库等)以及对 Spring 框架的依赖,而后者又具有大量依赖项.

I built a stand-alone Java application that has a bunch of dependencies (Apache Commons libs, etc) as well as a dependency on the Spring framework, which in turn has a bunch of dependencies.

我在 Eclipse 中构建了它,它运行良好.现在我需要将它部署到生产环境,所以我试图找出将它与所有依赖项一起打包的最佳方法,以及如何调用该事物(它将从命令行调用).

I built this in Eclipse, where it runs fine. Now I need to deploy it to production and so I'm trying to figure out the best way to package it with all dependencies and also how to even invoke the thing (it will be invoked from the command line).

蛮力的方法是将我的项目导出为 jar,找到所有依赖的 jar(和它们的依赖!),将它们复制到一个公共目录,编写一个包含类路径上的每个的 shell 脚本并运行它.显然,这看起来既愚蠢又乏味.

The brute-force way would be to export my project as a jar, find all the dependent jars (and their dependencies!), copy them to a common directory, write a shell script that includes every one on the classpath and run it. Obviously that seems stupid and tedious.

我应该使用 Ant、Maven 吗?我应该将所有依赖的 Spring jar 打包成一个大文件吗?有关处理此问题的任何提示都会有所帮助.

Should I use Ant, Maven? Should I package all of the dependent Spring jars into one big file? Any tips on dealing with this would be helpful.

推荐答案

Java 部署在 2009 年仍然愚蠢而乏味.所以通常的选择是编写这个小脚本.如果您将所有 JAR 放在一个目录中(例如,lib\),您可以使用一个通用脚本来自动构建类路径(请参阅 此博客条目).我建议在开头添加一个 cd/d %~dp0 到 cd 到脚本目录中.

Java deployment is still stupid and tedious in 2009. So the usual option is to write this small script. If you put all your JARs into one directory (say, lib\), you can use a common script which builds the classpath automatically (see this blog entry). I suggest to add a cd /d %~dp0 at the beginning to cd into the script's directory.

Maven 2 有一个插件可以将您的应用程序组装到一个超级 JAR"中(mvn 程序集:程序集).除非您使用 DB2 JCC 驱动程序(其中包含转换为 com.ibm. -> ClassNotFoundException 的COM.ibm."包),否则此方法有效.

Maven 2 has a plugin which can assemble your application in one "super JAR" (mvn assembly:assembly). This works unless you use the DB2 JCC driver (which contains "COM.ibm." packages which get converted to com.ibm. -> ClassNotFoundException).

最后一个解决方案,如果您已经有一个可以运行的 ANT build.xml,则是解压所有依赖的 JAR 并自己创建一个超级 JAR".

The last solution, if you have already an ANT build.xml which works, is to unpack all the dependent JARs and create a "super JAR" yourself.

这篇关于打包并运行具有 spring 依赖项的 Java 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆