Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selective regex replacement with the maven-replacer-plugin

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

However I would only like to do this where 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 configuration for the 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 the .js matches.

Is there some magic incantation I can put in the <token> section to achieve this?

like image 974
user591948 Avatar asked Aug 05 '14 18:08

user591948


1 Answers

I managed to do this using the following

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

although in my real application I replaced 12345 with the ${buildNumber} variable, so the syntax was

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

which I hope might make the solution a bit more understandable

like image 123
user591948 Avatar answered Sep 18 '22 11:09

user591948