Is there a way to interpolate all ${project.version} variables in a Maven pom.xml?

1k views Asked by At

I need to replace all occurrences of ${project.version} with the real value in a pom.xml.

I can do that with the maven-flatten-plugin, but that will rewrite the whole pom.xml instead of just replacing the project.version only.

As I want to use the jgitver-maven-plugin I can't use the resolveCiFriendliesOnly mode of the flatten plugin.

Is there an easy way to replace only one variable in the whole pom.xml? Is there another (hidden) option in the flatten plugin to do this?

2

There are 2 answers

0
Mohamad.K On

You can use the maven release plugin, specifically the update-versions goal.

ex: mvn --batch-mode release:update-versions -DdevelopmentVersion=1.2.0-SNAPSHOT

https://maven.apache.org/maven-release/maven-release-plugin/examples/update-versions.html

0
donmarjanus On

There is something close to what you want to achieve, but may be good enough. However this will resolve all variables in POM sections of your choice.

I used resolveCiFriendliesOnly, as it's the only mode that kind of preserves the original POM.

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>flatten-maven-plugin</artifactId>
                <version>1.2.2</version>
                <configuration>
                    <flattenMode>resolveCiFriendliesOnly</flattenMode>
                    <pomElements>
                        <dependencyManagement>resolve</dependencyManagement>
                        <!-- add more sections to resolve -->
                    </pomElements>
                </configuration>
                <executions>
                    <execution>
                        <id>flatten</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>flatten</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

Now you can add more sections under <pomElements> where you want variables resolved. As a side-effect of resolveCiFriendliesOnly, variables ${revision}, ${sha1} and ${changelist} will be resolved in the whole POM file.

Keep in mind that flatten-maven-plugin will still rewrite your POM, i.e. you may get sections reordered, lose comments, indentation, etc.

Here's a list of sections you can configure: https://www.mojohaus.org/flatten-maven-plugin/apidocs/org/codehaus/mojo/flatten/FlattenDescriptor.html

If resolveCiFriendliesOnly still messes up your POM, you can remove flattenMode, but then it will go wild and you must add nearly all possible sections to <pomElements> with keep option, e.g.

<pomElements>
    <dependencyManagement>resolve</dependencyManagement>
    <!-- keep everything else -->
    <parent>keep</parent>
    <build>keep</build>
    <distributionManagement>keep</distributionManagement>
    <repositories>keep</repositories>
    <pluginRepositories>keep</pluginRepositories>
    <profiles>keep</profiles>
    ...
</pomElements>