Spring Boot项目可以内嵌Web容器发布成可独立运行的可执行jar或war文件。
打包可执行jar文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- ... -->
<packaging>jar</packaging>
<!-- ... -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.4.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
打包可执行war
打包成war文件时将packaging类型修改为war,需要注意,如果项目没有继承Spring Boot默认的Parent项目,会因为web.xml
文件不存在而报错,需增加如下配置
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
运行
打包后的文件通过java -jar filename
命令即可运行
$ mvn package
$ java -jar target/mymodule-0.0.1-SNAPSHOT.jar