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

2

42

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}.

Influence answered 3/2, 2010 at 23:50 Comment(0)
I
37

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.

Influence answered 4/2, 2010 at 1:8 Comment(4)
I prefer this way as this enable more execution phases to occur, and allows to work on different files separately.Explicable
The replace is done correctly but the replaced .java is not compiled and not included in the generated package. It is because you apply the replace plugin in the prepare-package phase, when source files only has collected and them has been yet compiled. You must apply this solution in the compile phase, and it works!!!Wariness
For doing something in Java files I can recommend templating-maven-pluginAldrin
is there a way to remove whole line that starts with some character, say '#' in properties file?Cockeyed
I
52

You can also use

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

100% maven and doing exactly what you want and more

Incase answered 25/7, 2011 at 9:56 Comment(7)
Regardless of not being accepted, this is the "Maven Way". +1Conics
Well, I would not be so assertive about the "Maven Way". This plugin seems actually too much like an antrun brother or so. Anyway, growing the pom.xml with chunks and chunks of XML almost programmatic configurations is definitely not the maven way.Estimative
The link gives a 404 and I am having trouble googling it. Any idea what happened to the plugin?Galegalea
I am not getting a 404.Gogetter
I'm getting a 404, too. Is the project dead? Any alternatives?Ngo
The link in the answer (sometimes) redirects to code.google.com/archive/p/maven-replacer-pluginLegalize
Find and Replace Maven Plugin could be an alternative.Matroclinous
I
37

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.

Influence answered 4/2, 2010 at 1:8 Comment(4)
I prefer this way as this enable more execution phases to occur, and allows to work on different files separately.Explicable
The replace is done correctly but the replaced .java is not compiled and not included in the generated package. It is because you apply the replace plugin in the prepare-package phase, when source files only has collected and them has been yet compiled. You must apply this solution in the compile phase, and it works!!!Wariness
For doing something in Java files I can recommend templating-maven-pluginAldrin
is there a way to remove whole line that starts with some character, say '#' in properties file?Cockeyed

© 2022 - 2024 — McMap. All rights reserved.