0

I already have my pom.xml set up to do some basic replacing on a file during the prepare-package phase:

<plugin>
    <groupId>com.google.code.maven-replacer-plugin</groupId>
    <artifactId>maven-replacer-plugin</artifactId>
    <version>1.3.8</version>
    <executions>
        <execution>
            <phase>prepare-package</phase>
            <goals>
                <goal>replace</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <file>target/classes/plugin.yml</file>
        <replacements>
            <replacement>
                <token>maven-version-number</token>
                <value>${project.version}-${project.build.number}</value>
            </replacement>
        </replacements>
    </configuration>
</plugin>

However, I'd like to have that value be conditional on whether a particular environment variable is set. I run builds through Jenkins, and so I'd like to check if the environment variable BUILD_NUMBER is set; if so, it should take the place of project.build.number.

Is there any convenient way to do that within the replacer plugin? I read over the usage guide, and couldn't find anything immediately useful.

Tim
  • 59,527
  • 19
  • 156
  • 165

1 Answers1

3

You can use profiles to do this.

Use something like the following properties:

<properties>    
    <!-- The default build number if the env var is not set -->
    <build.number.to.use>0</build.number.to.use>
</properties>

Define your replacer plugin plugin to use ${build.number.to.use}:

...
<configuration>
    <file>target/classes/plugin.yml</file>
    <replacements>
        <replacement>
            <token>maven-version-number</token>
            <value>${project.version}-${build.number.to.use}</value>
        </replacement>
    </replacements>
</configuration>
...

And define the profile that is activated when the environment variable is set:

<profiles>
    <profile>
        <id>build-number-is-set</id>
        <activation>
          <property>
            <name>env.BUILD_NUMBER</name>
          </property>
        </activation>
        <properties>
            <build.number.to.use>${env.BUILD_NUMBER}</build.number.to.use>
        </properties>
    </profile>
</profiles>
prunge
  • 22,460
  • 3
  • 73
  • 80