Fork me on GitHub

Changing the project version

The set goal can be used to update the version of the current module. It will automatically climb up local directories to find the aggregation root. It will automatically update explicitly referenced dependencies. You specify the version to update to via the newVersion property, for example

mvn versions:set -DnewVersion=1.0.3-SNAPSHOT

Here is a real-life example:

svn co http://svn.apache.org/repos/asf/maven/enforcer/trunk/@778424 enforcer
A    enforcer/maven-enforcer-plugin
[...]
A    enforcer/enforcer-rules/pom.xml
 U   enforcer
Checked out revision 778424.
mvn versions:set -DnewVersion=2.0.0-SNAPSHOT -f enforcer/pom.xml
[INFO] Scanning for projects...
[INFO] Reactor build order:
[INFO]   Enforcer
[INFO]   Enforcer API
[INFO]   Enforcer Rules
[INFO]   Maven Enforcer Plugin
[INFO] ------------------------------------------------------------------------
[INFO] Building Enforcer
[INFO]    task-segment: [org.codehaus.mojo:versions-maven-plugin:1.0-beta-1:set] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] [versions:set]
[INFO] Searching for local aggregator root...
[INFO] Local aggregation root: ./enforcer
[INFO] Processing org.apache.maven.enforcer:enforcer
[INFO]     Updating project org.apache.maven.enforcer:enforcer
[INFO]         from version 1.0-beta-2-SNAPSHOT to 2.0.0-SNAPSHOT
[INFO]
[INFO] Processing org.apache.maven.enforcer:enforcer-api
[INFO]     Updating parent org.apache.maven.enforcer:enforcer
[INFO]         from version 1.0-beta-2-SNAPSHOT to 2.0.0-SNAPSHOT
[INFO]     Updating project org.apache.maven.enforcer:enforcer-api
[INFO]         from version 1.0-beta-2-SNAPSHOT to 2.0.0-SNAPSHOT
[INFO]
[INFO] Processing org.apache.maven.enforcer:enforcer-rules
[INFO]     Updating parent org.apache.maven.enforcer:enforcer
[INFO]         from version 1.0-beta-2-SNAPSHOT to 2.0.0-SNAPSHOT
[INFO]     Updating project org.apache.maven.enforcer:enforcer-rules
[INFO]         from version 1.0-beta-2-SNAPSHOT to 2.0.0-SNAPSHOT
[INFO]
[INFO] Processing org.apache.maven.plugins:maven-enforcer-plugin
[INFO]     Updating parent org.apache.maven.enforcer:enforcer
[INFO]         from version 1.0-beta-2-SNAPSHOT to 2.0.0-SNAPSHOT
[INFO]     Updating project org.apache.maven.plugins:maven-enforcer-plugin
[INFO]         from version 1.0-beta-2-SNAPSHOT to 2.0.0-SNAPSHOT
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3 seconds
[INFO] Finished at: Mon May 25 15:30:00 IST 2009
[INFO] Final Memory: 22M/355M
[INFO] ------------------------------------------------------------------------

To illustrate how dependencies version updating and aggregator root detection works reactor, we need to edit the dependencyManagement section of the root pom.xml to reference the version explicitly, as opposed to via a property.

Change:

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.apache.maven.enforcer</groupId>
        <artifactId>enforcer-api</artifactId>
        <version>${project.version}</version>
      </dependency>
      <dependency>
        <groupId>org.apache.maven.enforcer</groupId>
        <artifactId>enforcer-rules</artifactId>
        <version>${project.version}</version>
      </dependency>
      <dependency>
        <groupId>org.apache.maven.enforcer</groupId>
        <artifactId>enforcer-rules</artifactId>
        <version>${project.version}</version>
        <type>test-jar</type>
        <scope>test</scope>
      </dependency>

To

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.apache.maven.enforcer</groupId>
        <artifactId>enforcer-api</artifactId>
        <version>2.0.0-SNAPSHOT</version>
      </dependency>
      <dependency>
        <groupId>org.apache.maven.enforcer</groupId>
        <artifactId>enforcer-rules</artifactId>
        <version>2.0.0-SNAPSHOT</version>
      </dependency>
      <dependency>
        <groupId>org.apache.maven.enforcer</groupId>
        <artifactId>enforcer-rules</artifactId>
        <version>2.0.0-SNAPSHOT</version>
        <type>test-jar</type>
        <scope>test</scope>
      </dependency>

Now if we run

mvn versions:set -DnewVersion=2.1.0-SNAPSHOT -f enforcer/enforcer-api/pom.xml
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Enforcer API
[INFO]    task-segment: [org.codehaus.mojo:versions-maven-plugin:1.0-beta-1:set] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] [versions:set]
[INFO] Searching for local aggregator root...
[INFO] Local aggregation root: /home/connollys/src/enforcer/../enforcer
[INFO] Processing org.apache.maven.enforcer:enforcer-api
[INFO]     Updating project org.apache.maven.enforcer:enforcer-api
[INFO]         from version 2.0.0-SNAPSHOT to 2.1.0-SNAPSHOT
[INFO]
[INFO] Processing org.apache.maven.enforcer:enforcer
[INFO]     Updating dependency org.apache.maven.enforcer:enforcer-api
[INFO]         from version 2.0.0-SNAPSHOT to 2.1.0-SNAPSHOT
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3 seconds
[INFO] Finished at: Mon May 25 15:41:54 IST 2009
[INFO] Final Memory: 22M/350M
[INFO] ------------------------------------------------------------------------

In this case it has updated the version of enforcer-api, it has found that enforcer-api is part of an aggregator build in the local filesystem, and it has updated all references to enforcer-api in the aggregator build to reference the new version.

Widening the scope of the change

You can increase or change the scope of the change by specifying the groupId or artifactId or oldVersion on the command line directly. These can also support wildcards to help match multiple modules which will become origins of change.

mvn versions:set -DgroupId=org.apache.maven.* -DartifactId=* -DoldVersion=2.* -DnewVersion=2.1.0-SNAPSHOT