Skip to main content

Change Maven parent

org.openrewrite.maven.ChangeParentPom

Change the parent pom of a Maven pom.xml. Identifies the parent pom to be changed by its groupId and artifactId.

Recipe source

GitHub, Issue Tracker, Maven Central

This recipe is available under the Apache License Version 2.0.

Options

TypeNameDescriptionExample
StringoldGroupIdThe group ID of the Maven parent pom to be changed away from.org.springframework.boot
StringnewGroupIdOptional. The group ID of the new maven parent pom to be adopted. If this argument is omitted it defaults to the value of oldGroupId.org.springframework.boot
StringoldArtifactIdThe artifact ID of the maven parent pom to be changed away from.spring-boot-starter-parent
StringnewArtifactIdOptional. The artifact ID of the new maven parent pom to be adopted. If this argument is omitted it defaults to the value of oldArtifactId.spring-boot-starter-parent
StringnewVersionAn exact version number or node-style semver selector used to select the version number.29.X
StringoldRelativePathOptional. The relativePath of the maven parent pom to be changed away from. Use an empty String to match <relativePath />, use ../pom.xml to match the default value.../../pom.xml
StringnewRelativePathOptional. New relative path attribute for parent lookup.../pom.xml
StringversionPatternOptional. Allows version selection to be extended beyond the original Node Semver semantics. So for example,Setting 'version' to "25-29" can be paired with a metadata pattern of "-jre" to select Guava 29.0-jre-jre
BooleanallowVersionDowngradesOptional. If the new parent has the same group/artifact, this flag can be used to only upgrade the version if the target version is newer than the current.

Examples

Example 1
Parameters
ParameterValue
oldGroupIdorg.springframework.boot
newGroupIdcom.fasterxml.jackson
oldArtifactIdspring-boot-starter-parent
newArtifactIdjackson-parent
newVersion2.12
oldRelativePathnull
newRelativePathnull
versionPatternnull
allowVersionDowngradesfalse
Before
pom.xml
<project>
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.12.RELEASE</version>
</parent>

<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</project>
After
pom.xml
<project>
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-parent</artifactId>
<version>2.12</version>
</parent>

<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</project>

Example 2
Parameters
ParameterValue
oldGroupIdorg.springframework.cloud
newGroupIdnull
oldArtifactIdspring-cloud-config-dependencies
newArtifactIdnull
newVersion3.1.4
oldRelativePathnull
newRelativePathnull
versionPatternnull
allowVersionDowngradesnull
Before
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>org.sample</groupId>
<artifactId>sample</artifactId>
<version>1.0.0</version>

<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-dependencies</artifactId>
<version>3.1.2</version>
</parent>

<dependencies>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
</dependencies>
</project>
After
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>org.sample</groupId>
<artifactId>sample</artifactId>
<version>1.0.0</version>

<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-dependencies</artifactId>
<version>3.1.4</version>
</parent>

<dependencies>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
</dependencies>
</project>

Usage

This recipe has required configuration parameters. Recipes with required configuration parameters cannot be activated directly (unless you are running them via the Moderne CLI). To activate this recipe you must create a new recipe which fills in the required parameters. In your rewrite.yml create a new recipe with a unique name. For example: com.yourorg.ChangeParentPomExample. Here's how you can define and customize such a recipe within your rewrite.yml:

rewrite.yml
---
type: specs.openrewrite.org/v1beta/recipe
name: com.yourorg.ChangeParentPomExample
displayName: Change Maven parent example
recipeList:
- org.openrewrite.maven.ChangeParentPom:
oldGroupId: org.springframework.boot
newGroupId: org.springframework.boot
oldArtifactId: spring-boot-starter-parent
newArtifactId: spring-boot-starter-parent
newVersion: 29.X
oldRelativePath: ../../pom.xml
newRelativePath: ../pom.xml
versionPattern: '-jre'

Now that com.yourorg.ChangeParentPomExample has been defined, activate it in your build file:

  1. Add the following to your pom.xml file:
pom.xml
<project>
<build>
<plugins>
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>6.8.0</version>
<configuration>
<exportDatatables>true</exportDatatables>
<activeRecipes>
<recipe>com.yourorg.ChangeParentPomExample</recipe>
</activeRecipes>
</configuration>
</plugin>
</plugins>
</build>
</project>
  1. 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.

Contributors

Sam Snyder, Nick McKinney, Jonathan Schnéider, Valentin Delaye, DidierLoiseau, Tyler Van Gorder, Tim te Beek, Knut Wannheden, Jonathan Leitschuh