thecoop
thecoop

Reputation: 46128

Get maven release:prepare to update a property with the version number

For various reasons, I have a property in my pom file that has to match the version number:

<project>
    <groupId>group</groupId>
    <artifactId>artifact</artifactId>
    <version>1.2.3-SNAPSHOT</version>

    <packaging>pom</packaging>

    <properties>
        <!-- can't use ${project.version} here, as that is calculated lazily based on the final jar project! -->
        <artifact-version>1.2.3-SNAPSHOT</artifact-version>
    </properties>
</project>

When I run release:prepare, the release plugin updates the project version, but not the artifact-version property. I need this property to be updated to the released version, and then to the next snapshot version, along with the project property.

I also don't want to add any extra commits to the tree to manually update the property, I want it all done as part of the release prepare & perform steps.

How can I get the release plugin to update this property along with the version number?

Version properties

The reason we need a property as well as the version field is because we have several nested layers of aggregator poms before we reach the jar-building poms. We use <dependencyManagement> to keep dependencies the same, but often we need to override one <dependencyManagement> dependency in a parent pom with one in an intermediate pom (eg to add exclusions).

Maven doesn't allow artifact references in <dependencyManagement> to inherit the version from a parent <dependencyManagement>, so we need to re-specify the version number. We can't use ${project.version} as that resolves to the version number of the child project. Hence using a property to store the version number, that needs to match the release version here.

Upvotes: 3

Views: 1537

Answers (1)

Richard Tingstad
Richard Tingstad

Reputation: 425

You can make maven-release-plugin run versions:set-property:

<project>
    <modelVersion>4.0.0</modelVersion>

    <groupId>group</groupId>
    <artifactId>artifact</artifactId>
    <version>1.2.3-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
        <artifact.version>1.2.3-SNAPSHOT</artifact.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <configuration>
                    <preparationGoals>clean versions:set-property verify</preparationGoals>
                    <completionGoals>versions:set-property</completionGoals>
                    <arguments>-Prelease-super</arguments>
                </configuration>
                <inherited>false</inherited>
            </plugin>
        </plugins>
    </build>
    <profiles>
        <profile>
            <id>release-super</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>versions-maven-plugin</artifactId>
                        <version>2.8.1</version><!-- at least 2.5 -->
                        <configuration>
                            <property>artifact.version</property>
                            <newVersion>${project.version}</newVersion>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

The property name and value has to be specified (with dynamic value); these are set in versions-maven-plugin's <configuration>.

Because I do not want extending projects to inherit this, I use inherited>false and a <profile> which is activated during release. This works when artifact is an aggregator project too; the sub modules also need the version config to not choke on set-property (though it has no effect for them). (If artifact is a single-module project it could use <inherited>false on both plugins instead of profile.)

Upvotes: 3

Related Questions