如何使用 Ant 为具有外部 jar 依赖项的 java 项目构建可分发的 jar [英] How to build a distributable jar with Ant for a java project having external jar dependencies

查看:34
本文介绍了如何使用 Ant 为具有外部 jar 依赖项的 java 项目构建可分发的 jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Eclipse 中有一个 Java 项目,类 MainClass 在包中有 main 方法:

I have a Java project in Eclipse with class MainClass having main method in package :

com.nik.mypackage. 

该项目还引用了两个外部库,我将它们复制到 Eclipse 的 lib 文件夹中,然后使用 ADD JAR 函数添加到构建路径中.库是 one.jartwo.jar

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. The libraries being one.jar and two.jar

这个库在eclipse的lib文件夹中,并添加到构建路径中.

This library is in lib folder in eclipse and added to the build path.

我想使用 ant 脚本创建应用程序的可执行 JAR.这样用户就可以使用以下命令访问我的应用程序:

I want to create a executable JAR of the application using ant script. So that user can access my application using command:

c:>java -jar MyProject-20111126.jar

我知道 Eclipse 插件,它直接将 Java 应用程序导出为可运行的 JAR.但是我想学习ant和构建过程所以想手动创建build.xm.

I know about the Eclipse plugin which directly exports a java application as runnable JAR. But I want to learn ant and the build process so manually want to create the build.xm.

推荐答案

您的 build.xml 中有两个选项.您可以解压库 jar,然后将它们的内容与为您的应用程序编译的代码捆绑在一起.或者,您可以将库 jar 放在文件系统上,并在 MyProject-2011126.jar 文件的清单文件中提供一个 ClassPath 条目.

You have two options from your build.xml. You can either unjar the library jars and then bundle their contents with the code compiled for your application. 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.

如果您在清单中设置了类路径,请记住您提供的路径是相对于 MyProject-2011126.jar 的.

If you set the classpath in the manifest remember that the path you supply is relative to the MyProject-2011126.jar.

这篇关于如何使用 Ant 为具有外部 jar 依赖项的 java 项目构建可分发的 jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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