4

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>
Tunaki
  • 132,869
  • 46
  • 340
  • 423
user3198603
  • 5,528
  • 13
  • 65
  • 125
  • Unfortunately, the maven war plugin doesn't support custom delimiters: https://issues.apache.org/jira/browse/MWAR-225 you'll have to use `${...}`. – Tunaki Apr 14 '16 at 12:24
  • If you like to have this feature make requests/votes on the issue @Tunaki has mentioned. – khmarbaise Apr 14 '16 at 12:54
  • If you want to use a different delimiter than `${*}` have a look at my answer. But if you could use `${*}` in your JSP page as well, have a look at the answer from [Tunaki](http://stackoverflow.com/users/1743880/tunaki). Which would be less fiddling. – SubOptimal Apr 15 '16 at 05:49
  • Ironically, MWAR-225 was closed fixed on 15-Apr-16 as of maven-war-plugin 3.0.0 – Ed Randall Dec 02 '20 at 17:04

3 Answers3

1

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.

SubOptimal
  • 22,518
  • 3
  • 53
  • 69
  • Except this won't work when building the real WAR. The JSP isn't supposed to go under `target/classes`. – Tunaki Apr 14 '16 at 23:11
  • @Tunaki See my updated example. About the location of the file `The example only shows the idea and might need some tweaks ...` I updated this part to include the file location as well. – SubOptimal Apr 15 '16 at 05:47
  • Yes well it did need a lot of tweaks :) but now it'll work indeed, thanks for updating! – Tunaki Apr 15 '16 at 11:39
  • Thanks a lot SubOptimal for such a detailed answer – user3198603 Apr 24 '16 at 09:57
0

The file src/main/webapp/WEB-INF/content/test.jsp is a web resource: it is under src/main/webapp so it is handled by the maven-war-plugin and not the maven-resources-plugin.

You can also filter web resources with this plugin, with the help of the filtering attribute. You could have the following configuration to filter all web resources under src/main/webapp/WEB-INF/content:

<plugin>
  <artifactId>maven-war-plugin</artifactId>
  <version>2.6</version>
  <configuration>
    <webResources>
      <resource>
        <directory>src/main/webapp/WEB-INF/content</directory>
        <filtering>true</filtering>
      </resource>
    </webResources>
  </configuration>
</plugin>

If you only want to filter test.jsp, you can also restrict this by including only this file, and have

<resource>
  <directory>src/main/webapp/WEB-INF/content</directory>
  <filtering>true</filtering>
  <includes>
    <include>test.jsp</include>
  </includes>
</resource>

However, you cannot currently set custom delimiters with this plugin (issue MWAR-225 and unresolved), so you will have to stick to the default ones, which are ${...} and @...@ (but not @{...}).

Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • Hum why downvote? It answers the question (there's a bug so OP can't do what they want) so I offer alternative. There's no duplicate mentioning that particular maven-war-plugin issue. And I'm fairly sure it works since I tested it. – Tunaki Apr 18 '16 at 22:27
0

This might be a way to do it:

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
        <execution>
            <id>Filter specific resources</id>
            <phase>process-resources</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>
                    [Place to put filtered result]
                </outputDirectory>
                <resources>
                    <resource>
                        <directory>
                            src/main/webapp/WEB-INF/content
                        </directory>
                        <filtering>true</filtering>
                    </resource>
                </resources>
                <delimiters>
                    <delimiter>${*}</delimiter>
                    <delimiter>@{*}</delimiter>
                </delimiters>
                <!--<useDefaultDelimiters>false</useDefaultDelimiters>-->
            </configuration>
        </execution>
    </executions>
</plugin>
uniknow
  • 938
  • 6
  • 5
  • No, this won't work. A JSP isn't a project resource. It is a web resources. It shouldn't be output to `target/classes`. – Tunaki Apr 14 '16 at 23:13
  • With the tag `outputDirectory` one can specify the location where it should be put. – uniknow Apr 15 '16 at 06:28
  • Yes, and what would that be? Take a look at SubOptimal's answer. It isn't straightforward at all... – Tunaki Apr 15 '16 at 11:41