如何用Maven属性替换web.xml中的值?

问题描述 投票:39回答:6

我有一个Maven项目,它将一些测试文件下载到它的构建目录./target/files中。然后这些文件应该可用于servlet,我可以通过将完整路径硬编码为servlet的<init-param>来轻松实现:

<servlet>
    <servlet-name>TestServlet</servlet-name>
    <servlet-class>my.package.TestServlet</servlet-class>
    <init-param>
        <param-name>filepath</param-name>
        <param-value>/home/user/testproject/target/files</param-value>
    </init-param>
</servlet>

如何避免硬编码完整路径并使用动态参数替换?我尝试了以下,但它不起作用:

<param-value>${project.build.directory}/files</param-value>
maven servlets parameters web.xml
6个回答
77
投票

添加到您的pom部分:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
        <webResources>
            <resource>
                <filtering>true</filtering>
                <directory>src/main/webapp</directory>
                <includes>
                    <include>**/web.xml</include>
                </includes>
            </resource>
        </webResources>
        <warSourceDirectory>src/main/webapp</warSourceDirectory>
        <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
    </configuration>
</plugin>

有关详细信息,请参阅Maven: Customize web.xml of web-app project


6
投票

你可以简单地使用maven filtering resources

<build>
    ...
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
      ...
    </resources>
    ...
  </build>
  ...
</project>

您也可以将此组合并希望过滤某些文件,而不应过滤其他文件:

   <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
        <includes>
          <include>**/*.xml</include>
        </includes>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>false</filtering>
        <excludes>
          <exclude>**/*.xml</exclude>
        </excludes>
      </resource>
      ...
    </resources>

将适当的占位符放入您想要替换$ {home}之类的文件中。


4
投票

你可以使用Replace Ant Task来做到这一点。

下面是一个示例实现,我在属性文件中替换tokenkeys,使其适应您的需要

test.properties

SERVER_NAME=@SERVER_NAME@
PROFILE_NAME=@PROFILE_NAME@

pom.hml

 <plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <executions>
      <execution>
        <phase>compile</phase>
        <configuration>
          <tasks>

            <replace dir="${basedir}/src/main/resources" >
              <include name="**/*.properties"/>
             <replacefilter     token="@SERVER_NAME@" value="My Server"/>
             <replacefilter     token="@PROFILE_NAME@" value="My Profile"/>
            </replace>             

          </tasks>
        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
  </plugin> 

瞧!现在执行

mvn clean package

3
投票

我想你可以使用maven-war-plugin的filteringDeploymentDescriptors选项来过滤部署描述符 -

<properties>
    <maven.war.filteringDeploymentDescriptors>true</maven.war.filteringDeploymentDescriptors>
</properties>

1
投票

web.xml中的编码maven参数无法直接工作,因为在运行时,当应用程序启动时,maven已完成其工作,并且应用程序不了解maven。

您可以过滤替代web.xml(请参阅maven过滤:http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html)并在构建war时使用它(请参阅war插件文档中的webXml参数:http://maven.apache.org/plugins/maven-war-plugin/war-mojo.html


0
投票

添加maven-war-plugin并设置<filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>以在运行mvn package时替换占位符

<properties>
  <project.build.directory>your path</project.build.directory>
</properties>

<build>
  <plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.2.2</version>
    <configuration>
      <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
    </configuration>
  </plugin>
</build>
© www.soinside.com 2019 - 2024. All rights reserved.