創建springboot項目,且不采用<parent>引入springboot時,pom.xml如下:
<?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> <groupId>com.example</groupId> <artifactId>demo-without-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>demo-without-parent</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <maven.compiler.encoding>UTF-8</maven.compiler.encoding> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.0.2.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
執行
mvn clean install
控制台報錯:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war
原因:當設置為war時,web.xml必須存在。
解決:可通過插件配置屬性將其忽略。
首先,spring-boot-dependencies 這個pom文件,找到
<maven-war-plugin.version>3.1.0</maven-war-plugin.version>
找到版本號是3.1.0
然后,在項目的pom.xml中,加入
<plugin> <artifactId>maven-war-plugin</artifactId> <version>3.1.0</version> </plugin>
版本號必須與spring-boot-dependencies中maven-war-plugin-version版本號一致。
再次運行mvn clean install ,提示打包成功。
在idea中,運行啟動類,成功啟動。但是
java -jar demo-without-parent-0.0.1-SNAPSHOT.war
提示:demo-without-parent-0.0.1-SNAPSHOT.war中沒有主清單屬性
因為,maven-war-plugin的確執行了,但是spring-boot-maven-plugin插件卻沒有執行,原因在於該插件未指定版本,故設置版本號,依舊是從spring-boot-dependencies中找對應的版本號:2.0.2.RELEASE
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.0.2.RELEASE</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin>
重新執行
mvn clean install ,
mvn clean install
java -jar demo-without-parent-0.0.1-SNAPSHOT.war
成功。
最后總結:
1)、maven-war-plugin插件新老版本差異:
2.2:項目中WEB-INF下必須存在web.xml
3.1.0:不需要依賴web.xml
而且,版本號必須與所引入的spring-boot-dependencies下的maven-war-plugin版本號一致。
2)、spring-boot-maven-plugin插件需要配置repackage,否則不會添加spring boot 引導依賴,進而無法引導當前應用。
3)、根據使用習慣通常不會采用spring-boot-dependencies
附,完整pom.xml
<?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> <groupId>com.example</groupId> <artifactId>demo-without-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>demo-without-parent</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <maven.compiler.encoding>UTF-8</maven.compiler.encoding> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.0.2.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.0.2.RELEASE</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.1.0</version> </plugin> </plugins> </build> </project>