简体   繁体   English

如何使用Ant为具有外部jar依赖项的Java项目构建可分发jar

[英]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.jartwo.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.

相关问题 Eclipse:如何使用带有外部项目依赖项的ant构建可执行jar? - Eclipse: How to build an executable jar using ant with external project dependencies? java build ant文件与外部jar文件 - java build ant file with external jar files 在ant中引用外部.jar库以构建Android Java jar - Referencing external .jar libraries in ant to build a Android java jar 如何导出具有外部Jar依赖项的Eclipse项目? - How to export an Eclipse Project with External Jar dependencies? 你如何构建一个包含外部.jar依赖项的.jar? - How do you build a .jar that contains external .jar dependencies? 如何创建单个构建文件以在ant中创建具有其他项目依赖项的项目的jar文件 - How to create single build file to create jar file of project having other project dependency in ant 如何导出将在另一个Java项目中使用的jar以及jar使用的整个maven /外部依赖关系? - How do I export a jar that will be used in another Java project, along with the entire maven/external dependencies that the jar uses? 如何在Eclipse中构建Java项目以创建外部JAR - How do I build a Java project in Eclipse, to create an external JAR 如何使用ANT构建具有不同Java版本的jar - How to build a jar with different java version with ANT 使用Maven,我如何构建一个包含我的项目jar和所有依赖jar的distributable? - With Maven, how can I build a distributable that has my project's jar and all of the dependent jars?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM