使用 ant 创建一个可执行的 jar 文件,其中包含 build.xml 文件 [英] creating an executable jar file with ant which includes the build.xml file

查看:32
本文介绍了使用 ant 创建一个可执行的 jar 文件,其中包含 build.xml 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ant 构建应用程序,运行应用程序的 main() 方法,运行 junit 测试,并将所有内容打包在一个 jar 文件中(source+build+libraries+build.xml).这些类包括一个带有 main() 方法入口点的 runner 类.

I am trying to use ant to build an application, run the application's main() method, run junit tests, and package everything in a jar file (source+build+libraries+build.xml). The classes include a runner class with a main() method entry point.

然而,目标是包含所有使用的库,如 junit.jar 和 ant build.xml 本身.

The objective however is to inlcude all libraries used, like junit.jar, and the ant build.xml itself.

我想知道是否有一种方法可以让可执行 jar 文件运行 ant 构建文件 itslef 的选定目标,而不仅仅是 main() 方法.显然我不需要再次运行 compile 目标,而只需要 main() 方法(我的 run 目标中的 java 元素)和 junit 目标.这可能吗?

I was wondering if there is a way for the executable jar file, to run selected targets of the ant build file itslef, instead of just the main() method. Obviously I wouldn't need to run the compile targets again, but only the main() method (the java element in my run target) and the junit target. Is that possible?

非常感谢您的洞察力!

推荐答案

Executable jar"并不是你想象的那样.它允许您将所有您的 类打包在一起(您也可以向其中添加源代码,尽管我认为这没什么意义)并使用 jar 清单中的 Main-Class 属性声明一个主类.详细信息位于此处.

"Executable jar" is not what you think it is. It allows you pack all your classes together (you can add source to it as well though I see little point in that) and declare a main class using Main-Class attribute in the jar manifest. Details are here.

然后,您可以使用java -jar my.jar"命令行启动该 jar,该命令行将调用您指定的类的 main() 方法.您还可以通过 Class指定类路径清单文件中的 -Path 属性,可以指向您的应用程序所需的其他 jar.并不意味着这些 jar 已存档在您的 jar 中;事实上,如果您这样做,除非您采取特殊预防措施并编写自定义类加载器,否则 JVM 将无法从这些 jar 加载类.

You can then launch that jar using "java -jar my.jar" command line which would invoke main() method of the class you've specified. You can also specify classpath via Class-Path attribute in the manifest file that can point to other jars needed by your application. That DOES NOT MEAN those jars are archived within your jar; in fact if you were to do that, JVM won't be able to load classes from those jars unless you were to take special precautions and write a custom classloader.

这篇关于使用 ant 创建一个可执行的 jar 文件,其中包含 build.xml 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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