How to replace a value in web.xml with a Maven property?

前端 未结 6 462
滥情空心
滥情空心 2020-12-03 06:35

I have a Maven project that downloads some test files into its build directory ./target/files. These files should then be available to a servlet, which I can ea

相关标签:
6条回答
  • 2020-12-03 06:56

    You can simply use maven filtering resources:

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

    You can also combine this and would like to filter some files whereas others shouldn't be filtered:

       <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>
    

    Put appropriate placeholders into the files you would like having replaced things like ${home}.

    0 讨论(0)
  • 2020-12-03 06:57

    Coding maven parameters in web.xml can't work directly, because at run time, when your application starts, maven has finished its work and the application has no knowledge about maven.

    You can filter an alternative web.xml (see maven filtering: http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html) and use it when building the war (see the webXml parameter at the war plugin documentation: http://maven.apache.org/plugins/maven-war-plugin/war-mojo.html)

    0 讨论(0)
  • 2020-12-03 07:07

    I think you can use maven-war-plugin's filteringDeploymentDescriptors option to filter deployment descriptors -

    <properties>
        <maven.war.filteringDeploymentDescriptors>true</maven.war.filteringDeploymentDescriptors>
    </properties>
    
    0 讨论(0)
  • 2020-12-03 07:14

    You can use Replace Ant Task to do it.

    Heres a sample Implementation where i replace the tokenkeys in a property file , adapt it to suit your needs

    test.properties

    SERVER_NAME=@SERVER_NAME@
    PROFILE_NAME=@PROFILE_NAME@
    

    pom.xml

     <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> 
    

    voila! Now execute

    mvn clean package
    
    0 讨论(0)
  • 2020-12-03 07:22

    Add to your pom section:

    <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>
    

    See Maven: Customize web.xml of web-app project for more details

    0 讨论(0)
  • 2020-12-03 07:22

    Add maven-war-plugin and set <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors> to replace the placeholder when running 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>
    
    0 讨论(0)
提交回复
热议问题