使用 ant 创建一个可执行的 jar 文件,其中包含 build.xml 文件

发布于 2024-07-29 06:12:36 字数 359 浏览 1 评论 0原文

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

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

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

预先非常感谢您的见解!

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.

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

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?

Many thanks in advance for the insight!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欢烬 2024-08-05 06:12:36

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

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

"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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文