Home > Software design >  How can I change string placeholder using maven before compilation
How can I change string placeholder using maven before compilation

Time:11-08

Let's say that I have:

private const val VERSION = "@VERSION@"

And I want to change @VERSION@ with my ${project.version} from maven.

I tried using google replacer plugin with this configuration

<plugin>
    <groupId>com.google.code.maven-replacer-plugin</groupId>
    <artifactId>replacer</artifactId>
    <version>1.5.1</version>
    <executions>
        <execution>
            <phase>prepare-package</phase>
            <goals>
                <goal>replace</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <file>/src/main/kotlin/tech/goksi/pterobot/util/VersionCheck.kt</file>
        <replacements>
            <replacement>
                <!--suppress UnresolvedMavenProperty -->
                <token>@VERSION@</token>
                <value>${project.version}</value>
            </replacement>
        </replacements>
    </configuration>
</plugin>

But it seems that it changes my original source after compiling (replaced placeholder is not actually compiled). I would like to know if there is a way to replace it just during compile time.

CodePudding user response:

You can control the execution time of a plugin using <execution><phase> setting.

You specified it to be prepare-package which happens after compile. The default for this plugin is compile: maven-replacer-plugin/wikis/UsageGuide.wiki

  • Related