0

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.

AlexT
  • 2,524
  • 1
  • 11
  • 23
  • It seems you want to get the Maven artifact version at runtime. [Does this answer your question?](https://stackoverflow.com/questions/2712970/get-maven-artifact-version-at-runtime) – Chin Huang Nov 07 '22 at 21:49

1 Answers1

1

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

Lesiak
  • 22,088
  • 2
  • 41
  • 65
  • It compiles alright right now. Is there a way I can return original `@VERSION@` placeholder after compile ? – Вукашин Лекић Nov 07 '22 at 16:01
  • I guess you could rely on this (or other plugin) to clean up source to original state. But frankly speaking, for your use case, my first choice would be resources plugin to & resource filtering (around package phase) to embed the version in properties file - no need to cleanup the sources. – Lesiak Nov 07 '22 at 17:17
  • After research, will definitely try that, thanks – Вукашин Лекић Nov 07 '22 at 23:11
  • @ВукашинЛекић upon reflection, there is yet another approach: use some plugin to generate sources (in generate-sources phase, this happens pre-compile). Generated sources are typically placed in a separate for, not modified in-place. – Lesiak Nov 08 '22 at 06:14