73. Spring Boot AntLib 模块

Spring Boot AntLib 模块为 Apache Ant 提供了基本的 Spring Boot 支持。您可以使用该模块创建可执行 jar。要使用该模块,您需要在build.xml中声明一个额外的spring-boot命名空间,如以下示例所示:

<project xmlns:ivy="antlib:org.apache.ivy.ant"
	xmlns:spring-boot="antlib:org.springframework.boot.ant"
	name="myapp" default="build">
	...
</project>

您需要记住使用-lib选项启动 Ant,如以下示例所示:

$ ant -lib <folder containing spring-boot-antlib-2.1.1.RELEASE.jar>

Tip

“使用 Spring Boot”部分包括结合使用 Apache Ant 和 spring-boot-antlib的更完整示例。

73.1 Spring Boot Ant 任务

声明spring-boot-antlib名称空间后,可以使用以下附加任务:

73.1.1 spring-boot:exejar

您可以使用exejar任务创建一个 Spring Boot 可执行 jar。任务支持以下属性:

AttributeDescriptionRequired
destfile要创建的目标 jar 文件Yes
classesJava 类文件的根目录Yes
start-class要运行的主要应用程序类否*(默认为找到的第一个声明main方法的类)*

以下嵌套元素可用于任务:

ElementDescription
resources一个或多个Resource Collections描述一组Resources,应将其添加到创建的 jar 文件的内容中。
lib应该将一个或多个Resource Collections添加到组成应用程序运行时依赖项 Classpath 的 jar 库集合中。

73.1.2 Examples

本节显示了两个 Ant 任务示例。

Specify start-class.

<spring-boot:exejar destfile="target/my-application.jar"
		classes="target/classes" start-class="com.example.MyApplication">
	<resources>
		<fileset dir="src/main/resources" />
	</resources>
	<lib>
		<fileset dir="lib" />
	</lib>
</spring-boot:exejar>

Detect start-class.

<exejar destfile="target/my-application.jar" classes="target/classes">
	<lib>
		<fileset dir="lib" />
	</lib>
</exejar>

73.2 spring-boot:findmainclass

exejar在内部使用findmainclass任务来查找声明main的类。如有必要,您也可以直接在构建中使用此任务。支持以下属性:

AttributeDescriptionRequired
classesrootJava 类文件的根目录是*(除非指定了mainclass)*
mainclass可用于短路main类搜索No
property应该与结果一起设置的 Ant 属性否*(如果未指定,将记录结果)*

73.2.1 Examples

本节包含三个使用findmainclass的示例。

查找并记录.

<findmainclass classesroot="target/classes" />

查找并设置.

<findmainclass classesroot="target/classes" property="main-class" />

覆盖并设置.

<findmainclass mainclass="com.example.MainClass" property="main-class" />