spring_reference/VIII. Build tool plugins/59.4. Packaging executable ...

38 lines
1.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

### 59.4. 打包可执行jar和war文件
一旦`spring-boot`插件被应用到你的项目,它将使用`bootRepackage`任务自动尝试重写存档以使它们能够执行。为了构建一个jar或war你需要按通常的方式配置项目。
你想启动的main类既可以通过一个配置选项指定也可以通过向manifest添加一个`Main-Class`属性。如果你没有指定main类该插件会搜索带有`public static void main(String[] args)`方法的类。
为了构建和运行一个项目artifact你可以输入以下内容
```shell
$ gradle build
$ java -jar build/libs/mymodule-0.0.1-SNAPSHOT.jar
```
为了构建一个即能执行也可以部署到外部容器的war包你需要将内嵌容器依赖标记为"providedRuntime",比如:
```gradle
...
apply plugin: 'war'
war {
baseName = 'myapp'
version = '0.5.0'
}
repositories {
jcenter()
maven { url "http://repo.spring.io/libs-snapshot" }
}
configurations {
providedRuntime
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
...
}
```
**注**:具体参考[“Section 74.1, “Create a deployable war file””](http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-create-a-deployable-war-file)。