Selective regex replacement with maven-replacer-plugin

I would like to use the maven-replacer-plugin to rename my javascript import statements to index.html file.

However, I would only like to do this when the path starts with app

index.html snippet

...
<!-- rename this one to app/something12345.js -->
<script src="app/something.js"></script>

<!-- leave this one as it is -->
<script src="vendor/angular.js"></script>
...

      

In my pom.xml so far I have this config for maven-replacer-plugin

 <plugin>
     <groupId>com.google.code.maven-replacer-plugin</groupId>
     <artifactId>replacer</artifactId>
     <version>1.5.2</version>
     <executions>
         <execution>
             <phase>prepare-package</phase>
             <goals>
                 <goal>replace</goal>
             </goals>
         </execution>
     </executions>
     <configuration>
         <file>target/${project.build.finalName}/index.html</file>
             <replacements>
                 <replacement>
                     <token>(\.js")</token>
                     <value>12345.js"</value>
                 </replacement>
             </replacements>
     </configuration>
 </plugin>

      

At the moment, this will obviously replace all matches .js

.

Is there some magic spell I can add to the section <token>

to achieve this?

+3


source to share


1 answer


I was able to do it using the following

<replacement>
    <token>((app/)(.)*)(\.js")</token>
    <value>$112345.js</value>
</replacement>

      

although in my real application I replaced 12345

with a variable ${buildNumber}

so the syntax was



<replacement>
    <token>((app/)(.)*)(\.js")</token>
    <value>$1${buildNumber}.js</value>
</replacement>

      

which i hope can make the solution clearer

+6


source







All Articles