【问题标题】:How to replace web.xml during build with Maven?如何在使用 Maven 构建期间替换 web.xml?
【发布时间】:2023-04-10 19:14:01
【问题描述】:

我对 Maven 有疑问。我已经在使用 tomcat7-maven-plugin 将我的战争部署到 tomcat 中,并且效果很好。

但现在我想将一个文件更改为我生成的战争,然后再将其发送到 servlet 容器。可能吗? 特别是我想用另一个更改默认 web.xml。

我已经尝试过如下所示的 maven-resources-plugin:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-resources-plugin</artifactId>
  <version>2.4.2</version>
  <executions>
    <execution>
        <id>default-copy-resources</id>
        <phase>package</phase>
        <goals>
            <goal>copy-resources</goal>
        </goals>
        <configuration>
                <overwrite>true</overwrite>
                <outputDirectory>${project.build.directory}/${project.artifactId}/WEB-INF/</outputDirectory>
                <resources>
                    <resource>
                        <directory>${project.basedir}/src/main/webapp/ext/WEB-INF</directory>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

但是这个插件只是在目标 WEB-INF 文件夹上替换了 web.xml,没有任何东西会变成 war 文件。 有人可以帮我找到合适的插件来实现我的目的吗?

更新:

我用这个解决了:https://stackoverflow.com/a/3298876/2148530

【问题讨论】:

  • 我不太确定你想要达到什么目的?如果您想使用(可能有问题的)web.xml,您可以随时将其放在标准目录布局中的src/main/webapp/WEB-INF 中。
  • 您想仅针对特定版本更改它吗?这可以使用 Maven 配置文件来完成。但是你想达到什么目的?要更改整个 web.xml 或使其某些属性具有不同的值?
  • 我在src/main/webapp/WEB-INF 中已经有一个web.xml,但我会在构建和部署期间将其替换为另一个。
  • 认为正确的阶段不是打包而是tomcat7:deploy。所以当我启动mvn tomcat7:deploy。我想更改 web.xml。但你的第二个选择可能没问题。替换的web.xml 和原来的差不多,略有不同。

标签: java maven tomcat replace web.xml


【解决方案1】:

您可以在其中定义您的配置文件(在您的 pom.xml 中),您可以覆盖 ma​​ven.war.webxml 属性:

<properties>
    <maven.war.webxml>path/to/your/custom/web.xml</maven.war.webxml>
</properties>

【讨论】:

  • 这个解决方案在与 maven 配置文件结合使用时非常强大。这样,就可以为每个配置文件定义一个不同的 web.xml。
【解决方案2】:

解决方案(不是我的),取自问题Maven: Customize web.xml of web-app project,带有Maven War PluginwebXml 参数。

webXml 要使用的 web.xml 文件的路径。

使用示例

<profiles>
    <profile>
        <id>tomcat</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <configuration>
                        <webXml>${project.basedir}/src/main/webapp/ext/WEB-INF/web.xml</webXml>
                    </configuration>
                </plugin>
                 ...

【讨论】:

  • 您的命令和您的编辑没有任何效果,而且在最后它返回:[警告]请求的配置文件“tomcat”无法激活,因为它不存在。
  • 首先创建配置文件tomcat - maven.apache.org/guides/introduction/… 并将您的资源插件放入其中。
  • 我用这个解决了:stackoverflow.com/a/3298876/2148530。谢谢让我知道个人资料!
  • 个人资料非常有帮助:)
  • @giaffa86 我已经用你的解决方案替换了我的答案。
猜你喜欢
  • 2015-04-28
  • 2016-08-05
  • 2013-02-27
  • 2014-06-23
  • 2018-12-11
  • 1970-01-01
  • 1970-01-01
  • 2021-07-14
  • 1970-01-01
相关资源
最近更新 更多