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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1215874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:40:26  来源:igfitidea点击:

creating an executable jar file with ant which includes the build.xml file

javaantjunitexecutable-jar

提问by denchr

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.

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

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

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

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?

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

Many thanks in advance for the insight!

非常感谢您的洞察力!

回答by ChssPly76

"Executable jar" is not what you think it is. It allows you pack all yourclasses 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.

“可执行 jar”不是你想的那样。它可以让你收拾所有课一起上(你可以将其添加源好像还有我看到那个小点)和JAR清单利用Main-Class属性声明一个主类。详细信息在这里

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 classpathvia Class-Pathattribute in the manifest file that can point to other jars needed by your application. That DOES NOT MEANthose 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.

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