Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Full search and replace of strings in source files when copying resources

Tags:

maven

I have some java source files that use a package prefix (they are emulating some JDK classes). I use these files with the prefix to run against some unit tests. If the tests pass I want to produce a jar that contains the source files but with the package prefix removed from all the java files.

I am using maven for builds. Does any one know of a way to do this? Essentially what I want is something like the resources plugin filtering feature, but that does proper search and replace (like: s/my.package.prefix.//g), rather than filtering on ${vars}.

like image 301
Dean Povey Avatar asked Feb 03 '10 23:02

Dean Povey


2 Answers

You can also use

http://code.google.com/p/maven-replacer-plugin/

100% maven and doing exactly what you want and more

like image 169
cedric.walter Avatar answered Oct 20 '22 17:10

cedric.walter


This can be solved with the antrun plugin. Firstly the sources need to be copied to the target directory, with:

<build>   ...   <resources>     <resource>       <directory>src/main/java</directory>       <includes>         <include>**/*.java</include>       </includes>     </resource>   </resources>   ... </build> 

Secondly you use the replace task of the antrun plugin to replace the files using the prepare package phase

<build>     ...   <plugin>     <artifactId>maven-antrun-plugin</artifactId>     <executions>       <execution>         <phase>prepare-package</phase>         <configuration>           <tasks>             <replace token= "my.package.prefix." value="" dir="target/classes">                                                <include name="**/*.java"/>             </replace>           </tasks>         </configuration>         <goals>           <goal>run</goal>         </goals>       </execution>     </executions>   </plugin>   ... </build> 

This will copy the source files to target/classes in the process-resources phase, do a search and replace on the files inplace in the target/classes directory in the prepare-package phase and finally they will jarred up in the package phase.

like image 28
Dean Povey Avatar answered Oct 20 '22 16:10

Dean Povey