Skip to main content

Upgrade transitive Gradle dependencies

org.openrewrite.gradle.UpgradeTransitiveDependencyVersion

Upgrades the version of a transitive dependency in a Gradle build file. There are many ways to do this in Gradle, so the mechanism for upgrading a transitive dependency must be considered carefully depending on your style of dependency management.

Recipe source

GitHub, Issue Tracker, Maven Central

This recipe is available under the Apache License Version 2.0.

Options

TypeNameDescriptionExample
StringgroupIdThe first part of a dependency coordinate com.google.guava:guava:VERSION. This can be a glob expression.com.fasterxml.jackson*
StringartifactIdThe second part of a dependency coordinate com.google.guava:guava:VERSION. This can be a glob expression.jackson-module*
StringversionOptional. An exact version number or node-style semver selector used to select the version number. You can also use latest.release for the latest available version and latest.patch if the current version is a valid semantic version. For more details, you can look at the documentation page of version selectors. Defaults to latest.release.29.X
StringversionPatternOptional. Allows version selection to be extended beyond the original Node Semver semantics. So for example,Setting 'newVersion' to "25-29" can be paired with a metadata pattern of "-jre" to select Guava 29.0-jre-jre
StringbecauseOptional. The reason for upgrading the transitive dependency. For example, we could be responding to a vulnerability.CVE-2021-1234
ListonlyForConfigurationsOptional. A list of configurations to consider during the upgrade. For example, For example using implementation, runtimeOnly, we could be responding to a deployable asset vulnerability only (ignoring test scoped vulnerabilities).implementation, runtimeOnly

Examples

Example 1
Parameters
ParameterValue
groupIdcom.fasterxml*
artifactIdjackson-core
version2.12.5
versionPatternnull
becauseCVE-2024-BAD
onlyForConfigurationsnull
Before
build.gradle
plugins {
id 'java'
}
repositories { mavenCentral() }

dependencies {
implementation 'org.openrewrite:rewrite-java:7.0.0'
}
After
build.gradle
plugins {
id 'java'
}
repositories { mavenCentral() }

dependencies {
constraints {
implementation('com.fasterxml.jackson.core:jackson-core:2.12.5') {
because 'CVE-2024-BAD'
}
}

implementation 'org.openrewrite:rewrite-java:7.0.0'
}

Example 2
Parameters
ParameterValue
groupIdorg.apache.commons
artifactIdcommons-lang3
version3.14.0
versionPatternnull
becausenull
onlyForConfigurationsList.of("implementation", "runtimeOnly")
Before
build.gradle
plugins {
id 'info.solidsoft.pitest' version '1.15.0'
id 'java'
}
repositories { mavenCentral() }
dependencies {
compileOnly 'org.apache.activemq:artemis-jakarta-server:2.28.0'
}
After
build.gradle
plugins {
id 'info.solidsoft.pitest' version '1.15.0'
id 'java'
}
repositories { mavenCentral() }
dependencies {
constraints {
implementation('org.apache.commons:commons-lang3:3.14.0')
}

compileOnly 'org.apache.activemq:artemis-jakarta-server:2.28.0'
}

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.UpgradeTransitiveDependencyVersionExample. 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.UpgradeTransitiveDependencyVersionExample
displayName: Upgrade transitive Gradle dependencies example
recipeList:
- org.openrewrite.gradle.UpgradeTransitiveDependencyVersion:
groupId: com.fasterxml.jackson*
artifactId: jackson-module*
version: 29.X
versionPattern: '-jre'
because: CVE-2021-1234
onlyForConfigurations: implementation, runtimeOnly

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

  1. Add the following to your build.gradle file:
build.gradle
plugins {
id("org.openrewrite.rewrite") version("7.8.0")
}

rewrite {
activeRecipe("com.yourorg.UpgradeTransitiveDependencyVersionExample")
setExportDatatables(true)
}

repositories {
mavenCentral()
}
  1. Run gradle rewriteRun 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.