Search code examples
javamavenmaven-resources-plugin

maven filtering replace multiple tokens during build?


I am able to replace the token with patter ${..} under src/main/resources. I want to also replace the tokens with pattern @{} under specific file src/main/webapp/WEB-INF/content/test.jsp or directory src/main/webapp/WEB-INF/content.

I tried adding <delimiter>@{test.version}</delimiter> But I am not sure how to make specific directory src/main/webapp/WEB-INF/content.

test.version will come at runtime like mvn clean install -Dtest.version=100

<build>
    <resources>
    <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*</include>
            </includes>
        </resource> 

    </resources>
 </build>



<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.5</version>
    <inherited>true</inherited>
    <configuration>
        <encoding>UTF-8</encoding>
        <nonFilteredFileExtensions>
            <nonFilteredFileExtension>xls</nonFilteredFileExtension>
        </nonFilteredFileExtensions>
         <delimiters>
          <delimiter>${*}</delimiter>
          <delimiter>@{*}</delimiter>
        </delimiters>
    </configuration>
</plugin>

Solution

  • The placeholder @{*} seems not to work with the replacer plugin.

    As you anyway want to replace the placeholer by a value. You might use in the JSP files #{test.version} instead. For the filtering you can define different directories. See the snippet below.

    Assuming following structure.

    pom.xml
    src/main/resources/hello.txt
    src/main/webapp/WEB-INF/content/test.jsp
    src/main/webapp/WEB-INF/web.xml
    

    pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
             http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>sub.optimal.example</groupId>
        <artifactId>superapp</artifactId>
        <packaging>jar</packaging>
        <version>1.0-SNAPSHOT</version>
        <name>superapp</name>
    
        <properties>
            <test.version>world</test.version>
        </properties>
        <build>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <filtering>true</filtering>
                </resource>
                <resource>
                    <targetPath>../webapp/WEB-INF/content</targetPath>
                    <filtering>true</filtering>
                    <directory>src/main/webapp/WEB-INF/content</directory>
                </resource>
            </resources>
            <plugins>      
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>2.7</version>
                    <inherited>true</inherited>
                    <configuration>
                        <encoding>UTF-8</encoding>
                        <delimiters>
                            <delimiter>${*}</delimiter>
                            <delimiter>#{*}</delimiter>
                            <!-- this delimiter is not recognized -->
                            <delimiter>@{*}</delimiter>
                        </delimiters>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.6</version>
                    <configuration>
                        <webResources>
                            <resource>
                                <directory>target/webapp</directory>
                            </resource>
                        </webResources>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    

    hello.txt

    Hello $ ${test.version} txt
    Hello # #{test.version} txt
    Hello @ @{test.version} txt
    

    test.jsp

    Hello $ ${test.version} jsp
    Hello # #{test.version} jsp
    Hello @ @{test.version} jsp
    

    build the WAR file

    mvn clean package
    

    output

    > jar tf superapp-1.0-SNAPSHOT.war
    META-INF/
    META-INF/MANIFEST.MF
    WEB-INF/
    WEB-INF/classes/
    WEB-INF/content/
    WEB-INF/classes/hello.txt
    WEB-INF/content/test.jsp
    WEB-INF/web.xml
    META-INF/maven/sub.optimal.example/superapp/pom.xml
    META-INF/maven/sub.optimal.example/superapp/pom.properties
    
    # copy the file into a temp directory and extract it
    # jar xf superapp-1.0-SNAPSHOT.war
    
    >cat WEB-INF/classes/hello.txt
    Hello $ world txt
    Hello # world txt
    Hello @ @{test.version} txt
    
    >cat WEB-INF/content/test.jsp
    Hello $ world jsp
    Hello # world jsp
    Hello @ @{test.version} jsp
    

    The example only shows the idea and might need some tweaks to exclude files, file locations, and so on.