Skip to main content

Find Maven dependency

org.openrewrite.maven.search.FindDependency

Finds first-order dependency uses, i.e. dependencies that are defined directly in a project. Each match is also recorded as a row in the DependenciesDeclared data table.

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 com.google.guava:guava:VERSION. Supports glob.com.google.guava
StringartifactIdThe second part of a dependency coordinate com.google.guava:guava:VERSION. Supports glob.guava
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
groupIdjakarta.activation
artifactIdjakarta.activation-api
versionnull
versionPatternnull
Before
pom.xml
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.sample</groupId>
<artifactId>sample</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>2.1.2</version>
</dependency>
</dependencies>
</project>
After
pom.xml
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.sample</groupId>
<artifactId>sample</artifactId>
<version>1.0.0</version>
<dependencies>
<!--~~>--><dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>2.1.2</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.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 Maven dependency example
recipeList:
- org.openrewrite.maven.search.FindDependency:
groupId: com.google.guava
artifactId: guava
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 pom.xml file:
    pom.xml
    <project>
    <build>
    <plugins>
    <plugin>
    <groupId>org.openrewrite.maven</groupId>
    <artifactId>rewrite-maven-plugin</artifactId>
    <version>6.40.0</version>
    <configuration>
    <exportDatatables>true</exportDatatables>
    <activeRecipes>
    <recipe>com.yourorg.FindDependencyExample</recipe>
    </activeRecipes>
    </configuration>
    </plugin>
    </plugins>
    </build>
    </project>
  2. 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

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.