Java 用maven替换my Web.xml中的值

Java 用maven替换my Web.xml中的值,java,maven,Java,Maven,我的Web.xml中有一个值 <display-name> MyApp version @minorversion@- Pré prod </display-name> MyApp版本@minorversion@-Préprod 我想知道当我想编译或打包时,是否可以用maven读取属性文件来替换Web.xml中的值 您可以使用maven replacer插件 <plugin> <gr

我的Web.xml中有一个值

<display-name> MyApp version @minorversion@- Pré prod </display-name>
MyApp版本@minorversion@-Préprod

我想知道当我想编译或打包时,是否可以用maven读取属性文件来替换Web.xml中的值

您可以使用maven replacer插件

              <plugin>
                    <groupId>com.google.code.maven-replacer-plugin</groupId>
                    <artifactId>replacer</artifactId>
                    <version>1.5.3</version>
                    <executions>
                        <execution>
                            <id>REPLACE</id>
                            <goals>
                                <goal>replace</goal>
                            </goals>
                            <phase>{Phase to execute in}</phase>
                          <configuration>
                                <file>${Path to file to replace}</file>
                                <regex>true</regex>
                                <token>${Regex to match in file}</token>
                                <value>${New Value to Replace matched}</value>
                                <regexFlags>
                                    <regexFlag>MULTILINE</regexFlag>
                                    <regexFlag>DOTALL</regexFlag>
                                </regexFlags>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>

com.google.code.maven-replacer-plugin
替代者
1.5.3
代替
代替
{要在中执行的阶段}
${要替换的文件的路径}
真的
${要在文件中匹配的正则表达式}
${要替换匹配的新值}
多行
多特尔
  • 添加src\main\resources\conf.properties:

    minorversion=1.0.0.0
    
  • pom.xml

           <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <resourceEncoding>${project.build.sourceEncoding}</resourceEncoding>
                    <webResources>
                         <resource>
                              <filtering>true</filtering>
                              <directory>src/main/webapp</directory>
                              <includes>
                                <include>**/web.xml</include>
                              </includes>
                            </resource>
                    </webResources>
                    <filters>
                      <filter>src/main/resources/conf.properties</filter>
                    </filters>
                </configuration>
            </plugin>
    


  • 澄清一下:您想从一个文件中读取属性并使用其条目替换另一个文件中的令牌,对吗?也许吧?是的,如果maven有可能的话,这就是我想要做的
    ...
    @minorversion@
    
    ${minorversion}
    ...