Categories
Development

Maven update versions

In my projects I use Maven as dependency managment system.

In the past I updated versions of libraries manually in pom.xml. But as projects grow, this becomes more and more annoying and time consuming. So I decided to give it a try to do this automatically.

Add Plugins

Add the Enforcer Maven Plugin and the Versions Maven Plugin:

<build>        
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-enforcer-plugin</artifactId>
      <version>3.5.0</version>
      <executions>
        <execution>
          <id>enforce-maven</id>
          <goals>
            <goal>enforce</goal>
          </goals>
          <configuration>
            <rules>
              <requireMavenVersion>
                <version>3.9</version>
              </requireMavenVersion>
            </rules>    
          </configuration>
        </execution>
      </executions>
    </plugin>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>versions-maven-plugin</artifactId>
      <version>2.16.2</version>
      <configuration>
        <generateBackupPoms>false</generateBackupPoms>
      </configuration>
    </plugin>
  </plugins>
</build>

Preparation

I could not update the version of the plugins when the version information is hardcoded in the plugin section. So I used properties for the plugin versions.

<properties>
  <enforcer-plugin.version>3.5.0</enforcer-plugin.version>
  <versions-plugin.version>2.16.2</versions-plugin.version>
</properties>

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-enforcer-plugin</artifactId>
      <version>${enforcer-plugin.version}</version>
      <executions>
        <execution>
          <id>enforce-maven</id>
          <goals>
            <goal>enforce</goal>
          </goals>
          <configuration>
            <rules>
              <requireMavenVersion>
                <version>3.9</version>
              </requireMavenVersion>
            </rules>    
          </configuration>
        </execution>
      </executions>
    </plugin>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>versions-maven-plugin</artifactId>
      <version>${versions-plugin.version}</version>
      <configuration>
        <generateBackupPoms>false</generateBackupPoms>
      </configuration>
    </plugin>
  </plugins>
</build>

Check updates

Check for newer versions in properties, dependencies and plugins:

mvn versions:display-property-updates
mvn versions:display-dependency-updates
mvn versions:display-plugin-updates

Update

Update everything:

mvn versions:update-properties
mvn versions:use-latest-releases

Leave a Reply

Your email address will not be published. Required fields are marked *