Skip to main content

Find Gradle dependency

org.openrewrite.gradle.search.FindDependency

Finds dependencies declared in gradle build files. Each match is also recorded as a row in the `DependenciesDeclared` data table. See the [reference](https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_configurations_graph) on Gradle configurations or the diagram below for a description of what configuration to use. A project's compile and runtime classpath is based on these configurations.

<img alt="Gradle compile classpath" src="https://docs.gradle.org/current/userguide/img/java-library-ignore-deprecated-main.png" width="200px"/>
A project's test classpath is based on these configurations.

<img alt="Gradle test classpath" src="https://docs.gradle.org/current/userguide/img/java-library-ignore-deprecated-test.png" width="200px"/>.

Recipe source

GitHub: FindDependency.java, Issue Tracker, Maven Central

This recipe is available under the Apache License Version 2.0.

Options

TypeNameDescriptionExample
StringgroupIdThe first part of a dependency coordinate identifying its publisher.com.google.guava
StringartifactIdThe second part of a dependency coordinate uniquely identifying it among artifacts from the same publisher.guava
StringconfigurationOptional. The dependency configuration to search for dependencies in. If omitted then all configurations will be searched.api
StringversionOptional. An exact version number or node-style semver selector used to select the version number.3.0.0
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

Used by

This recipe is used as part of the following composite recipes:

Example

Parameters
ParameterValue
groupIdorg.openrewrite
artifactIdrewrite-core
configurationapi
versionnull
versionPatternnull
Before
build.gradle
plugins {
id 'java-library'
}
repositories {
mavenCentral()
}
dependencies {
api "org.openrewrite:rewrite-core:latest.release"
}
After
build.gradle
plugins {
id 'java-library'
}
repositories {
mavenCentral()
}
dependencies {
/*~~>*/api "org.openrewrite:rewrite-core:latest.release"
}

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.FindDependencyExample. 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.FindDependencyExample
displayName: Find Gradle dependency example
recipeList:
- org.openrewrite.gradle.search.FindDependency:
groupId: com.google.guava
artifactId: guava
configuration: api
version: 3.0.0
versionPattern: '-jre'

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

Now that com.yourorg.FindDependencyExample 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("latest.release")
    }

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

    repositories {
    mavenCentral()
    }
  2. 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

Dependencies declared

org.openrewrite.maven.table.DependenciesDeclared

Direct (first-order) dependencies declared by the project.

Column NameDescription
Project nameThe name of the project that contains the dependency.
Source setThe source set that contains the dependency.
GroupThe first part of a dependency coordinate com.google.guava:guava:VERSION.
ArtifactThe second part of a dependency coordinate com.google.guava:guava:VERSION.
VersionThe resolved version.
Dated snapshot versionThe resolved dated snapshot version or null if this dependency is not a snapshot.
ScopeMaven scope (e.g. compile, test) or Gradle configuration name (e.g. implementation, testImplementation). For Maven, defaults to compile when no scope is declared.