[英]How to build a distributable jar with Ant for a java project having external jar dependencies
I have a Java project in Eclipse with class MainClass
having main method in package : 我在Eclipse中有一个Java项目,类
MainClass
在package中具有main方法:
com.nik.mypackage.
The project also references two external libraries, which I copied in the lib
folder in Eclipse and then added to build path using ADD JAR
function. 该项目还引用了两个外部库,我将它们复制到Eclipse的
lib
文件夹中,然后使用ADD JAR
函数添加到构建路径中。 The libraries being one.jar
and two.jar
这些库是
one.jar
和two.jar
This library is in lib
folder in eclipse and added to the build path. 该库位于eclipse中的
lib
文件夹中,并添加到了构建路径中。
I want to create a executable JAR
of the application using ant script. 我想使用ant脚本创建应用程序的可执行
JAR
。 So that user can access my application using command: 这样用户可以使用以下命令访问我的应用程序:
c:>java -jar MyProject-20111126.jar
I know about the Eclipse plugin which directly exports a java application as runnable JAR
. 我知道有关Eclipse插件的信息,它直接将Java应用程序导出为可运行的
JAR
。 But I want to learn ant and the build process so manually want to create the build.xm. 但是我想学习蚂蚁和构建过程,所以要手动创建build.xm。
You have two options from your build.xml. 您在build.xml中有两个选择。 You can either unjar the library jars and then bundle their contents with the code compiled for your application.
您可以解压缩库jar,然后将其内容与为应用程序编译的代码捆绑在一起。 Or, you can put the library jars on the filesystem and supply a ClassPath entry in the manifest file of the MyProject-2011126.jar file.
或者,您可以将库jar放在文件系统上,并在MyProject-2011126.jar文件的清单文件中提供ClassPath条目。
If you set the classpath in the manifest remember that the path you supply is relative to the MyProject-2011126.jar. 如果在清单中设置类路径,请记住,您提供的路径是相对于MyProject-2011126.jar的。
one alternative: 一种选择:
Instead of having only a jar, you build mutiple jars (your jar + libs) +batch file. 您可以构建多个jars(您的jar + libs)+ batch文件,而不是只有一个jar。
So, your built package can be like this structure: 因此,您构建的包可以像这样的结构:
-/package/bin/app.bat
/package/lib/my.jar
/package/lib/one.jar
/package/lib/two.jar
In app.bat you just have the same as your code 在app.bat中,您的代码与您的代码相同
java -jar MyProject-20111126.jar
PS: if you want to start learning built tools, ANT may be a bit tool old. PS:如果您想开始学习内置工具,则ANT可能已经有点旧了。 I suggest http://maven.apache.org/
我建议http://maven.apache.org/
Please try one-jar . 请尝试一罐 。 It helps to redistribute everything packaged as single jar and comes with ant-task .
它有助于重新分发打包成单个jar的所有内容,并带有ant-task 。 See Easiest way to merge a release into one JAR file .
请参阅将发行版合并到一个JAR文件中的最简单方法 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.