Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Maven Replace Plugin to replace a phrase on a generated Java file inside Target/generated-sources

I spent hours on this problem, searching several Google and SO entries, I have some ideas but not getting the result.

I have a maven file that does something like this:

  1. grab a jar containing JSON schemas, and unpack them.

  2. Using the Maven Replacer plugin (v 1.5.3), replace a line in a schema file called “MySchema.json” as such:

    ”Hello” : ”HelloWorld” :

  3. then Maven would use another plugin to compile a class called “converter.java” and runs this class to output a Java file based on “MySchema.json”. let’s call the generated Java file “MyPojo.java”.

  4. Now, I want Maven to replace a line in “MyPojo.java”, but no matter what I do I cannot achieve this.

I tried:

  • include a separate replace plugin entry for step 4 after the plugin that converts schemas to Java, but ofcourse this caused Maven to complain about existing replace plugin with same artifact/group id from step 2.
  • Tried adding a separate execution id to the goal “replace” for second plugin, this is invalid for this plugin.
  • There is a parent project to my current project folder, I tried putting another replacer plugin in the parent POM and make the phase to be any of the “package”, “generate-resources”, “compile”, etc. did not work. Note: the phase for replacements inside “MySchema.json” (in my current project POM) is generate-sources.
  • give absolute path to the Java, it kept complaining that path does not exist. But I copied and pasted the path to the Java inside windows explorer address bar after it was generated and could read it from Windows explorer. Note that the generated Java file “MyPojo.java”, went under “target/generated-sources” which is sourced by a parent POM above this project using a Maven Helper plugin in parent POM, so this folder should be visible as a source for further compilation. That Maven Helper plugin has phase generate-sources.
  • Use with same result as above

In my current project (non-parent one) this is the POM code:

    <build>
        <!—execute a plugin grab schemas jar and unpack schemas-->

 ...

            <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>${project.basedir}/target/schemas/MySchema.json</include>
                    </includes>
                    <replacements>
                        <replacement>
                            <token>"Hello":</token>
                            <value>"Hello World":</value>
                        </replacement>
                    </replacements>
                </configuration>
            </plugin>

        <!-- execute a Plugin for converting shcemas to POJO -->

        . . .

        </plugins>
    </build>
</project>
like image 577
Pooyan Najafi Avatar asked Dec 14 '25 02:12

Pooyan Najafi


1 Answers

You should be able to declare the plugin only once and run two replace execution at different Maven Build Lifecycle phases:

  • Before the Json -> POJO conversion
  • After the Json -> POJO conversion

So, translating that into could would result in something like:

<plugin>
    <!-- (unique) plugin declaration -->
    <groupId>com.google.code.maven-replacer-plugin</groupId>
    <artifactId>maven-replacer-plugin</artifactId>
    <version>1.3.5</version>
    <executions>
        <!-- first execution: replace on json file -->
        <execution>
            <id>replace-for-json</id>
            <phase>some-phase-before-conversion</phase>
            <goals>
                <goal>replace</goal>
            </goals>
            <configuration>
                <filesToInclude>${project.basedir}/target/schemas/MySchema.json</filesToInclude>
                <preserveDir>true</preserveDir>
                <outputDir>target</outputDir>
                <replacements>
                    <replacement>
                        <token>"Hello":</token>
                        <value>"Hello World (Json)":</value>
                    </replacement>
                </replacements>
            </configuration>
        </execution>
        <!-- second execution: replace on java file -->
        <execution>
            <id>replace-for-pojo</id>
            <phase>some-phase-after-conversion</phase>
            <goals>
                <goal>replace</goal>
            </goals>
            <configuration>
                <filesToInclude>${project.basedir}/target/generated-sources/MyPojo.java</filesToInclude>
                <preserveDir>true</preserveDir>
                <outputDir>target</outputDir>
                <replacements>
                    <replacement>
                        <token>"Hello":</token>
                        <value>"Hello World (Java)":</value>
                    </replacement>
                </replacements>
            </configuration>
        </execution>
    </executions>
</plugin>

Source: Configuration for the maven-replacer-plugin on two separate executions

like image 198
marcelovca90 Avatar answered Dec 15 '25 17:12

marcelovca90



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!