【问题标题】:maven-war-plugin and the Spring JavaConfigmaven-war-plugin 和 Spring JavaConfig
【发布时间】:2024-05-02 17:45:03
【问题描述】:

我正在使用 JavaConfig 替换我的 web.xmlservlet.xml 文件,基于: http://javahash.com/spring-4-mvc-hello-world-tutorial-full-example/

正如 cmets 中所建议的,我也在尝试遵循以下建议的解决方案: Why is maven-war-plugin failing for web.xml missing if I configured it not to fail on missing web.xml?

但它不能解决我的问题,见下文。

但是在我替换了这些文件(添加了相应的 *.java 文件并删除了旧的 *.xml 文件)之后,我从 ma​​ven-war-plugin 收到了这个错误:

web.xml is missing and <failOnMissingWebXml> is set to true

然后我添加了:

        <failOnMissingWebXml>false</failOnMissingWebXml>

到配置,在插件配置下面

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <executions>
            <execution>
                <id>default-war</id>
    <phase>prepare-package</phase>                      
                <configuration>
    <failOnMissingWebXml>false</failOnMissingWebXml>
    <archive>
      <manifest>
        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
      </manifest>
      <manifestEntries>
        <Implementation-Build>${buildNumber}</Implementation-Build>
      </manifestEntries>
    </archive>
                    <webResources>
                        <resource>
                            <directory>${basedir}/src/main/webapp</directory>
                            <filtering>true</filtering>
                            <includes>
                                <include>**/*.jsp</include>
                            </includes>
                        </resource>
                    </webResources>
                </configuration>
            </execution>
        </executions>
    </plugin>

但我在 Eclipse 中收到此错误:

web.xml is missing and <failOnMissingWebXml> is set to true

当我构建时,我得到:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.0.2:war (default-war) on project webapp-module: Error assembling WAR: Deployment descriptor: /home/user/workspace_j2ee/webapp/webapp-module/target/webapp-module/WEB-INF/web.xml does not exist. -> [Help 1]
[ERROR] 

如果我只是添加一个最小的 web.xml 像:

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring MVC Application</display-name>

</web-app>

我可以使用 maven 构建项目,但在 eclipse 中出现此错误:

org.eclipse.jst.j2ee.commonarchivecore.internal.exception.DeploymentDescriptorLoadException: WEB-INF/web.xml
Stack trace of nested exception:
org.eclipse.jst.j2ee.commonarchivecore.internal.exception.EmptyResourceException: platform:/resource/webapp-module/target/m2e-wtp/web-resources/WEB-INF/web.xml
    at org.eclipse.jst.j2ee.commonarchivecore.internal.util.ArchiveUtil.getRoot(ArchiveUtil.java:439)
    at org.eclipse.jst.j2ee.commonarchivecore.internal.strategy.XmlBasedImportStrategyImpl.primLoadDeploymentDescriptor(XmlBasedImportStrategyImpl.java:42)
    at org.eclipse.jst.j2ee.commonarchivecore.internal.strategy.War22ImportStrategyImpl.loadDeploymentDescriptor(War22ImportStrategyImpl.java:90)
    at org.eclipse.jst.j2ee.commonarchivecore.internal.strategy.War22ImportStrategyImpl.importMetaData(War22ImportStrategyImpl.java:84)
    at org.eclipse.jst.j2ee.commonarchivecore.internal.impl.WARFileImpl.getDeploymentDescriptor(WARFileImpl.java:146)
    at org.eclipse.jst.j2ee.model.internal.validation.WarValidator.validateInJob(WarValidator.java:334)
    at org.eclipse.jst.j2ee.internal.web.validation.UIWarValidator.validateInJob(UIWarValidator.java:113)
    at org.eclipse.wst.validation.internal.operations.ValidatorJob.run(ValidatorJob.java:78)
    at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)

JavaConfig 的意义不在于您应该能够替换那些文件吗?

在这个例子中,web.xml文件似乎被完全删除了:

http://kielczewski.eu/2013/11/spring-mvc-without-web-xml-using-webapplicationinitializer/

【问题讨论】:

标签: java maven spring-mvc


【解决方案1】:

看来你的插件版本比较旧

maven-war-plugin:2.0.2

但是根据这个页面

http://maven.apache.org/plugins/maven-war-plugin/war-mojo.html#failOnMissingWebXml

failOnMissingWebXml 从 2.1.x 版本开始引入。

尝试使用更多最新版本。

【讨论】: