Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven: Replace token in source file before compilation

Tags:

java

maven

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!

like image 356
Michael S Avatar asked Aug 25 '15 07:08

Michael S


People also ask

Which Maven plugin will replace the tokens inside a file?

The plugin that replace that tokens is the maven-resources-plugin. The following is an example for using the maven-resources-plugin: The trick here is the true. When this is true Maven will copy the files to the appropriate locations and it will also replace the tokens inside that files.

How do I use the Maven- resources-plugin?

The following is an example for using the maven-resources-plugin: The trick here is the true. When this is true Maven will copy the files to the appropriate locations and it will also replace the tokens inside that files. The tokens inside that files can be in the following format $ {property.name}.

How do I replace Maven properties with actual values?

After you have declare your profiles for each environment and load the properties you can use tokens on property files or other files that maven can replace them with actual values. The plugin that replace that tokens is the maven-resources-plugin. The following is an example for using the maven-resources-plugin: The trick here is the true.

What is Maven settings XML?

The settings.xml file configures a Maven installation. It's similar to a pom.xml file but is defined globally or per user. Let's explore the elements we can configure in the settings.xml file. The main, settings element of the settings.xml file, can contain nine possible predefined child elements:


2 Answers

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 :)

like image 56
wemu Avatar answered Oct 17 '22 08:10

wemu


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>
    
like image 40
stefitz Avatar answered Oct 17 '22 09:10

stefitz