[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war

创建springboot项目,且不采用引入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.0modelVersion>
<groupId>com.examplegroupId>
<artifactId>demo-without-parentartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>warpackaging>

    <name>demo-without-parentname>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <maven.compiler.encoding>UTF-8maven.compiler.encoding>
    properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-dependenciesartifactId>
                <version>2.0.2.RELEASEversion>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-tomcatartifactId>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            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.0maven-war-plugin.version>

找到版本号是3.1.0

然后,在项目的pom.xml中,加入

<plugin>
    <artifactId>maven-war-pluginartifactId>
    <version>3.1.0version>
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.bootgroupId>
    <artifactId>spring-boot-maven-pluginartifactId>
    <version>2.0.2.RELEASEversion>
    <executions>
        <execution>
            <goals>
                <goal>repackagegoal>
            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.0modelVersion>
<groupId>com.examplegroupId>
<artifactId>demo-without-parentartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>warpackaging>

    <name>demo-without-parentname>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <maven.compiler.encoding>UTF-8maven.compiler.encoding>
    properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-dependenciesartifactId>
                <version>2.0.2.RELEASEversion>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-tomcatartifactId>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <version>2.0.2.RELEASEversion>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackagegoal>
                        goals>
                    execution>
                executions>
            plugin>
            <plugin>
                <artifactId>maven-war-pluginartifactId>
                <version>3.1.0version>
            plugin>
        plugins>
    build>

project>

 

你可能感兴趣的:([ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war)