Skip to main content

Migrate to Maven 4

org.openrewrite.maven.MigrateToMaven4

Migrates Maven POMs from Maven 3 to Maven 4, addressing breaking changes and deprecations. This recipe updates property expressions, lifecycle phases, removes duplicate plugin and dependency declarations, upgrades plugins known to fail under Maven 4, switches repository URLs to HTTPS, and replaces removed properties to ensure compatibility with Maven 4.

Recipe source

GitHub: maven.yml, Issue Tracker, Code Genome Project

info

This recipe is composed of more than one recipe. If you want to customize the set of recipes this is composed of, you can find and copy the GitHub source for the recipe from the link above.

This recipe is available under the Apache License Version 2.0.

Definition

Examples

Example 1

MigrateToMaven4Test#comprehensiveMigration

Before
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>

<modules>
<module>child-a</module>
<module>child-b</module>
</modules>

<properties>
<project.root>${executionRootDirectory}</project.root>
<multi.root>${multiModuleProjectDirectory}</multi.root>
<my.version>${version}</my.version>
<my.basedir>${basedir}</my.basedir>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>pre-clean-task</id>
<phase>pre-clean</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>pre-integration-test-task</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>post-integration-test-task</id>
<phase>post-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
After
pom.xml
<project xmlns="http://maven.apache.org/POM/4.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.1.0 http://maven.apache.org/xsd/maven-4.1.0.xsd">
<modelVersion>4.1.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>

<subprojects>
<subproject>child-a</subproject>
<subproject>child-b</subproject>
</subprojects>

<properties>
<project.root>${session.rootDirectory}</project.root>
<multi.root>${project.rootDirectory}</multi.root>
<my.version>${project.version}</my.version>
<my.basedir>${project.basedir}</my.basedir>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>pre-clean-task</id>
<phase>before:clean</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>pre-integration-test-task</id>
<phase>before:integration-test</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>post-integration-test-task</id>
<phase>after:integration-test</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Example 2

MigrateToMaven4Test#comprehensiveMigration

Before
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>

<modules>
<module>child-a</module>
<module>child-b</module>
</modules>

<properties>
<project.root>${executionRootDirectory}</project.root>
<multi.root>${multiModuleProjectDirectory}</multi.root>
<my.version>${version}</my.version>
<my.basedir>${basedir}</my.basedir>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>pre-clean-task</id>
<phase>pre-clean</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>pre-integration-test-task</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>post-integration-test-task</id>
<phase>post-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
After
pom.xml
<project xmlns="http://maven.apache.org/POM/4.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.1.0 http://maven.apache.org/xsd/maven-4.1.0.xsd">
<modelVersion>4.1.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>

<subprojects>
<subproject>child-a</subproject>
<subproject>child-b</subproject>
</subprojects>

<properties>
<project.root>${session.rootDirectory}</project.root>
<multi.root>${project.rootDirectory}</multi.root>
<my.version>${project.version}</my.version>
<my.basedir>${project.basedir}</my.basedir>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>pre-clean-task</id>
<phase>before:clean</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>pre-integration-test-task</id>
<phase>before:integration-test</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>post-integration-test-task</id>
<phase>after:integration-test</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Usage

This recipe has no required configuration parameters and comes from a rewrite core library. It can be activated directly without adding any dependencies.

OpenRewrite artifacts, including the Maven and Gradle plugins themselves, are distributed through the Code Genome Project repository (https://artifacts.codegenomeproject.org/maven), which requires authentication. Sign in to the Code Genome Project to create a download token; your build authenticates with the email or username you signed in with, plus that token as the password. See the quickstart guide for details.

  1. Add the Code Genome Project credentials to your Maven settings.xml file (typically at ~/.m2/settings.xml), replacing USERNAME and TOKEN with your own:
    settings.xml
    <settings>
    <servers>
    <server>
    <id>codegenome</id>
    <username>USERNAME</username>
    <password>TOKEN</password>
    </server>
    </servers>
    </settings>
  2. Add the following to your pom.xml file:
    pom.xml
    <project>
    <repositories>
    <repository>
    <id>codegenome</id>
    <url>https://artifacts.codegenomeproject.org/maven</url>
    </repository>
    </repositories>
    <pluginRepositories>
    <pluginRepository>
    <id>codegenome</id>
    <url>https://artifacts.codegenomeproject.org/maven</url>
    </pluginRepository>
    </pluginRepositories>
    <build>
    <plugins>
    <plugin>
    <groupId>org.openrewrite.maven</groupId>
    <artifactId>rewrite-maven-plugin</artifactId>
    <version>6.47.0</version>
    <configuration>
    <exportDatatables>true</exportDatatables>
    <activeRecipes>
    <recipe>org.openrewrite.maven.MigrateToMaven4</recipe>
    </activeRecipes>
    </configuration>
    </plugin>
    </plugins>
    </build>
    </project>
  3. Run mvn rewrite:run to run the recipe.

See how this recipe works across multiple open-source repositories

Run this recipe on OSS repos at scale with the Moderne SaaS.

The community edition of the Moderne platform enables you to easily run recipes across thousands of open-source repositories.

Please contact Moderne for more information about safely running the recipes on your own codebase in a private SaaS.

Data Tables

Maven metadata failures

org.openrewrite.maven.table.MavenMetadataFailures

Attempts to resolve maven metadata that failed.

Column NameDescription
Group idThe groupId of the artifact for which the metadata download failed.
Artifact idThe artifactId of the artifact for which the metadata download failed.
VersionThe version of the artifact for which the metadata download failed.
Maven repositoryThe URL of the Maven repository that the metadata download failed on.
SnapshotsDoes the repository support snapshots.
ReleasesDoes the repository support releases.
FailureThe reason the metadata download failed.