8.3 Spring Boot AntLib模块

优质
小牛编辑
127浏览
2023-12-01

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.3.RELEASE.jar>

“使用Spring Boot”部分包含一个使用带有spring-boot-antlib的Apache Ant的更完整示例。

8.3.1 Spring Boot Ant Tasks

一旦声明了spring-boot-antlib命名空间,就可以使用以下附加任务:

  • spring-boot:exejar
  • spring-boot:findmainclass

8.3.1.1 spring-boot:exejar

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

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

以下嵌套元素可与任务一起使用:

ElementDescription
resources一个或多个资源集合,描述应添加到创建的jar文件内容中的一组资源。
lib应该添加到构成应用程序的运行时依赖性类路径的jar库集的一个或多个资源集合。

8.3.1.2 示例

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

<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>
<exejar destfile="target/my-application.jar" classes="target/classes">
	<lib>
		<fileset dir="lib" />
	</lib>
</exejar>

8.3.2 spring-boot:findmainclass

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

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

8.3.2.1 示例

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

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