Replace task in Maven Antrun Plugin
Asked Answered
N

2

5

I am using antrun plugin in my maven build to replace a token @version@ in some of the JSP files with the application version. This is what I am doing:

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.6</version>
    <executions>
        <execution>
            <phase>compile</phase>
            <configuration>
                 <target>
                      <echo>${displayVersion}</echo>
                      <replace file="src/main/webapp/admin/decorators/default.jsp" token="@version@" value="${displayVersion}"/>
                 </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

I am passing displayVersion as a parameter to maven

mvn clean install -DdisplayVersion="Version-1.1"

And this is the console output for Antrun Plugin

[INFO] [antrun:run {execution: default}]
[INFO] [antrun:run {execution: default}]  
[INFO] Executing tasks  
main:  
[echo] 9.4_70  
[INFO] Executed tasks

Although the property is being echoed properly, it's not substituted in my JSP. The @version@ token is replaced by {displayVersion} and not it's actual value.

Norse answered 17/3, 2011 at 14:24 Comment(2)
In the antrun configuration you posted you actually use value="${display}" and not value="${displayVersion}", is it a typo in the original code or a cut'n'paste issue?Duluth
Its a typo... my Bad. I have edited the question to rectify the sameNorse
S
7

Use Maven Resources Filtering as Aaron suggested and set the delimiters in the Maven Resource Plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.5</version>
    <configuration>
      <delimiters>
        <!-- enable maven's standard delimiters -->
        <delimiter>${*}</delimiter>
        <!-- enable your @delimiters@ -->
        <delimiter>@</delimiter>
      </delimiters>
    </configuration>
</plugin>
Sesquialtera answered 17/3, 2011 at 15:49 Comment(0)
M
1

The Maven resources plugin can replace variables in resources; so if you deliver the JSP (instead of compiling it with the jspc plugin), you can simply let the resource plugin do the work while it copies resources by enabling filtering.

Mantle answered 17/3, 2011 at 14:44 Comment(2)
Aaron, I am aware about the Maven resource filtering. But I am migrating from AMT to MAVEN and the token I use is @ -- @version@ and I believe for Maven Resource filtering I need to change it to ${version} Now, there are a lot of files in which I need to replace this token, and I can't go and make it ${version}. Is there any way by which I can filter @version@ with Maven Resource FilteringNorse
Try Sean's solution. Which IDE are you using? In Eclipse, you can do a global search'n'replace over all files.Mantle

© 2022 - 2024 — McMap. All rights reserved.