# 86.9 使用Ant构建可执行存档（不使用spring-boot-antlib）

想要使用Ant进行构建，你需要抓取依赖，编译，然后像通常那样创建一个jar或war存档。为了让它可以执行，你可以使用`spring-boot-antlib`，也可以使用以下指令：

1. 如果构建jar，你需要将应用的类和资源打包进内嵌的`BOOT-INF/classes`目录。如果构建war，你需要将应用的类打包进内嵌的`WEB-INF/classes`目录。
2. 对于jar，添加运行时依赖到内嵌的`BOOT-INF/lib`目录。对于war，则添加到`WEB-INF/lib`目录。注意不能压缩存档中的实体。
3. 对于jar，添加`provided`依赖到内嵌的`BOOT-INF/lib`目录。对于war，则添加到`WEB-INF/lib-provided`目录。注意不能压缩存档中的实体。
4. 在存档的根目录添加`spring-boot-loader`类（这样`Main-Class`就可用了）。
5. 使用恰当的启动器，比如对于jar使用`JarLauncher`作为manifest的`Main-Class`属性，指定manifest的其他属性，特别是设置`Start-Class`属性。

示例：

```markup
<target name="build" depends="compile">
    <jar destfile="target/${ant.project.name}-${spring-boot.version}.jar" compress="false">
        <mappedresources>
            <fileset dir="target/classes" />
            <globmapper from="*" to="BOOT-INF/classes/*"/>
        </mappedresources>
        <mappedresources>
            <fileset dir="src/main/resources" erroronmissingdir="false"/>
            <globmapper from="*" to="BOOT-INF/classes/*"/>
        </mappedresources>
        <mappedresources>
            <fileset dir="${lib.dir}/runtime" />
            <globmapper from="*" to="BOOT-INF/lib/*"/>
        </mappedresources>
        <zipfileset src="${lib.dir}/loader/spring-boot-loader-jar-${spring-boot.version}.jar" />
        <manifest>
            <attribute name="Main-Class" value="org.springframework.boot.loader.JarLauncher" />
            <attribute name="Start-Class" value="${start-class}" />
        </manifest>
    </jar>
</target>
```

该[Ant示例](https://github.com/spring-projects/spring-boot/tree/v2.0.0.RELEASE/spring-boot-samples/spring-boot-sample-ant)中有一个`build.xml`文件及`manual`任务，可以使用以下命令来运行：

```
$ ant -lib <folder containing ivy-2.2.jar> clean manual
```

在上述操作之后，你可以使用以下命令运行该应用：

```
$ java -jar target/*.jar
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jack80342.gitbook.io/spring-boot/ix.-how-to-guides/86.-build/86.9-build-an-executable-archive-from-ant-without-using-spring-boot-antlib.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
