Java 如果我将 maven-war-plugin 配置为在丢失 web.xml 时不会失败,为什么 maven-war-plugin 会因丢失 web.xml 而失败?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24291299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:20:07  来源:igfitidea点击:

Why is maven-war-plugin failing for web.xml missing if I configured it not to fail on missing web.xml?

javamavenmaven-3warmaven-war-plugin

提问by Edy Bourne

Here's a challenge: why is this build failing?

这是一个挑战:为什么这个构建失败了?

I have configured Maven's maven-war-plugin not to fail on an abscent web.xml file, it seems:

我已经将 Maven 的 maven-war-plugin 配置为不会在一个 abscent web.xml 文件上失败,它似乎:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <executions>
            <execution>
                <id>prepare-war</id>
                <phase>prepare-package</phase>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <archiveClasses>false</archiveClasses>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix />
                        </manifest>
                        <manifestEntries>
                            <Implementation-Build>${build.number}</Implementation-Build>
                            <Implementation-Title>${project.name}</Implementation-Title>
                            <Built-By>${user.name}</Built-By>
                            <Built-OS>${os.name}</Built-OS>
                            <Build-Date>${build.date}</Build-Date>
                        </manifestEntries>
                    </archive>
                    <webResources>
                        <resource>
                            <!-- this is relative to the pom.xml directory -->
                            <directory>./target/dist</directory>
                        </resource>
                    </webResources>
                </configuration>
            </execution>
        </executions>
    </plugin>

But despite of this configuration it keeps failing like this:

但是尽管有这种配置,它仍然像这样失败:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.4:war (default-war) on project com.specktro.orchid.operations.portal.frontend: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1]

I actually don't have the web.xml, so I need it to assemble the war without it.

我实际上没有 web.xml,所以我需要它来组装没有它的War。

I tried adding a bogus <webXml>none</webXml>into the config, but that didn't change anything...

我尝试<webXml>none</webXml>在配置中添加一个虚假内容,但这并没有改变任何东西......

What am I missing?

我错过了什么?

采纳答案by user944849

The execution ID in the POM is prepare-war. Maven runs its own default execution of the war plugin for projects with packing type war. The default execution has ID default-war. As the POM is currently configured, the wargoal is running twice.

POM 中的执行 ID 是prepare-war. Maven 为打包类型的项目运行它自己的 war 插件的默认执行war。默认执行具有 ID default-war。由于当前配置了 POM,war目标运行了两次。

If you look at the error message:

如果您查看错误消息:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.4:war (default-war) on project com.specktro.orchid.operations.portal.frontend: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1]

You may see the execution ID that fails in parenthesis (default-war). If you change the execution ID to default-waryour problem will go away, AND you will no longer have two executions of the war goal running.

您可能会在括号中看到失败的执行 ID (default-war)。如果您将执行 ID 更改为default-war您的问题将消失,并且您将不再有两次执行War目标。

回答by Jorge_B

This should work:

这应该有效:

<plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
            <executions>
                <execution>
                    <id>prepare-war</id>
                    <phase>prepare-package</phase>
                    <configuration>
                        <archiveClasses>false</archiveClasses>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                                <classpathPrefix />
                            </manifest>
                            <manifestEntries>
                                <Implementation-Build>${build.number}</Implementation-Build>
                                <Implementation-Title>${project.name}</Implementation-Title>
                                <Built-By>${user.name}</Built-By>
                                <Built-OS>${os.name}</Built-OS>
                                <Build-Date>${build.date}</Build-Date>
                            </manifestEntries>
                        </archive>
                        <webResources>
                            <resource>
                                <!-- this is relative to the pom.xml directory -->
                                <directory>./target/dist</directory>
                            </resource>
                        </webResources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Please notice that the <failOnMissingWebXml>false</failOnMissingWebXml>section has been moved up to the plugin configuration rather than the execution.

请注意,该<failOnMissingWebXml>false</failOnMissingWebXml>部分已移至插件配置而不是执行。

回答by Pablo Guzman Labra

The problem is related to the version that you are currently working with. You need to see your local repository and see the version that you have already downloaded. Go to your .m2folder and look for

该问题与您当前使用的版本有关。您需要查看本地存储库并查看已下载的版本。转到您的.m2文件夹并查找

.m2\repository\org\apache\maven\plugins\maven-compiler-plugin

In that folder you can see the versions that are available for you.

在该文件夹中,您可以看到可供您使用的版本。

Change the version for the version that you have in your folder and it works!

更改您文件夹中的版本的版本,它可以工作!