Michael S
Michael S

Reputation: 769

Maven: Replace token in source file before compilation

I want to replace a token @NAME@ in a source file (in my case *.java) before compilation.

I try to use google replacer plugin but I am open for anything which will help me.

1.pom.xml The pom file look like this

<plugin>
    <groupId>com.google.code.maven-replacer-plugin</groupId>
    <artifactId>replacer</artifactId>
    <version>1.5.3</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>replace</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <includes>
            <include>src/main/java/com/test/sample/File.java</include>
        </includes>
        <replacements>
            <replacement>
                <token>@NAME@</token>
                <value>New content</value>
            </replacement>
        </replacements>
    </configuration>
</plugin>

But after I run mvn package the output is:

--- replacer:1.5.3:replace (default) @ MyProject --- [INFO] Replacement run on 0 file.

Because there is no error I do not know what I have done wrong. Maybe:

  1. Defined phase is wrong
  2. Defined include is wrong
  3. ...

Greetings!

Upvotes: 9

Views: 25447

Answers (2)

stefitz
stefitz

Reputation: 516

While this is something you usually should not do in the first place, sometimes you have no choice (in my case it was "converting" an old project to Maven with changing as little of the code as possible). The above somehow did not work (while I could replace a placeholder in the source file and add the generated-sources folder to be compiled, it complained about duplicate source files). Then I found an easier way by using the templating-maven-plugin as described here http://www.mojohaus.org/templating-maven-plugin/examples/source-filtering.html:

  1. Put the file with the placeholder in the folder /src/main/java-templates. Excerpt from my source code:

    public static final String APPLICATION_VERSION = "r${project.version}";
    
  2. Add the following to your pom's plugins section:

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>templating-maven-plugin</artifactId>
            <version>1.0.0</version>
            <executions>
                <execution>
                    <id>filter-src</id>
                    <goals>
                        <goal>filter-sources</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    

Upvotes: 5

wemu
wemu

Reputation: 8160

I think there are two options.

If you keep using the plugin I think you need to add the ${basedir} to the include statement:

<include>${basedir}/src/main/java/com/test/sample/File.java</include>

If you dont want to modify the file in src/main but filter the file and add that one to the build you can use the standard resource filtering and the buildhelper plugin to add those "generated sources" to the build.

So step one would be using resource filtering to copy the file: http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

And then use the http://www.mojohaus.org/build-helper-maven-plugin/ to add those sources to the build.

Some IDEs (IntelliJ) will recognize /target/genereated-sources automatically if you keep using that folder (its not standard but very common). If you search for "maven" and "generated-sources" you will find quite some tutorials.

Hope this helps :)

Upvotes: 9

Related Questions