Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,22 @@
import org.openrewrite.ExecutionContext;
import org.openrewrite.Option;
import org.openrewrite.Recipe;
import org.openrewrite.SourceFile;
import org.openrewrite.TreeVisitor;
import org.openrewrite.gradle.trait.GradleDependency;
import org.openrewrite.java.marker.JavaProject;
import org.openrewrite.java.marker.JavaSourceSet;
import org.openrewrite.marker.SearchResult;
import org.openrewrite.maven.table.DependenciesDeclared;
import org.openrewrite.semver.Semver;
import org.openrewrite.semver.VersionComparator;
import org.openrewrite.Validated;

@Value
@EqualsAndHashCode(callSuper = false)
public class FindDependency extends Recipe {
transient DependenciesDeclared dependenciesDeclared = new DependenciesDeclared(this);

@Option(displayName = "Group",
description = "The first part of a dependency coordinate identifying its publisher.",
example = "com.google.guava")
Expand Down Expand Up @@ -71,7 +77,9 @@ public String getInstanceNameSuffix() {
return String.format("`%s:%s%s`", groupId, artifactId, maybeVersionSuffix);
}

String description = "Finds dependencies declared in gradle build files. 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. " +
String description = "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.\n\n<img alt=\"Gradle compile classpath\" src=\"https://docs.gradle.org/current/userguide/img/java-library-ignore-deprecated-main.png\" width=\"200px\"/>\n" +
"A project's test classpath is based on these configurations.\n\n<img alt=\"Gradle test classpath\" src=\"https://docs.gradle.org/current/userguide/img/java-library-ignore-deprecated-test.png\" width=\"200px\"/>.";

Expand All @@ -81,7 +89,30 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
.groupId(groupId)
.artifactId(artifactId)
.configuration(configuration)
.asVisitor(gd -> versionIsValid(version, versionPattern, gd) ? SearchResult.found(gd.getTree()) : gd.getTree());
.asVisitor((gd, ctx) -> {
if (!versionIsValid(version, versionPattern, gd)) {
return gd.getTree();
}
SourceFile sourceFile = gd.getCursor().firstEnclosing(SourceFile.class);
String projectName = sourceFile == null ? "" : sourceFile.getMarkers()
.findFirst(JavaProject.class)
.map(JavaProject::getProjectName)
.orElse("");
String sourceSetName = sourceFile == null ? "main" : sourceFile.getMarkers()
.findFirst(JavaSourceSet.class)
.map(JavaSourceSet::getName)
.orElse("main");
dependenciesDeclared.insertRow(ctx, new DependenciesDeclared.Row(
projectName,
sourceSetName,
gd.getGroupId(),
gd.getArtifactId(),
gd.getVersion(),
null,
gd.getConfigurationName()
));
return SearchResult.found(gd.getTree());
});
}

private static boolean versionIsValid(@Nullable String desiredVersion, @Nullable String versionPattern,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ maven,org.openrewrite:rewrite-gradle,org.openrewrite.gradle.search.FindGradlePro
maven,org.openrewrite:rewrite-gradle,org.openrewrite.gradle.search.EffectiveGradlePluginRepositories,List effective Gradle plugin repositories,"Lists the Gradle plugin repositories that would be used for plugin resolution, in order of precedence. This includes Maven repositories defined in the settings.gradle pluginManagement section and build.gradle buildscript repositories as determined when the LST was produced.",1,Search,Gradle,"[{""name"":""useMarkers"",""type"":""Boolean"",""displayName"":""Use markers"",""description"":""Whether to add markers for each effective Gradle plugin repository to the build or settings file. Default `false`.""}]","[{""name"":""org.openrewrite.maven.search.EffectiveMavenRepositoriesTable"",""displayName"":""Effective Maven repositories"",""instanceName"":""Effective Maven repositories"",""description"":""Table showing which Maven repositories were used in dependency resolution for this POM."",""columns"":[{""name"":""pomPath"",""type"":""String"",""displayName"":""POM path"",""description"":""The path to the POM file.""},{""name"":""repositoryUri"",""type"":""String"",""displayName"":""Repository URI"",""description"":""The URI of the Maven repository.""}]}]"
maven,org.openrewrite:rewrite-gradle,org.openrewrite.gradle.search.FindRepositoryOrder,Gradle repository order,Determine the order in which dependencies will be resolved for each `build.gradle` based on its defined repositories as determined when the LST was produced.,1,Search,Gradle,,"[{""name"":""org.openrewrite.maven.table.MavenRepositoryOrder"",""displayName"":""Maven repository order"",""instanceName"":""Maven repository order"",""description"":""The order in which dependencies will be resolved for each `pom.xml` based on its defined repositories and effective `settings.xml`."",""columns"":[{""name"":""id"",""type"":""String"",""displayName"":""Repository ID"",""description"":""The ID of the repository. Note that projects may define the same physical repository with different IDs.""},{""name"":""uri"",""type"":""String"",""displayName"":""Repository URI"",""description"":""The URI of the repository.""},{""name"":""knownToExist"",""type"":""boolean"",""displayName"":""Known to exist"",""description"":""If the repository is provably reachable. If false, does not guarantee that the repository does not exist.""},{""name"":""rank"",""type"":""int"",""displayName"":""Rank"",""description"":""The index order of this repository in the repository list.""}]}]"
maven,org.openrewrite:rewrite-gradle,org.openrewrite.gradle.search.FindRepository,Find Gradle repository,Find a Gradle repository by url.,1,Search,Gradle,"[{""name"":""type"",""type"":""String"",""displayName"":""Type"",""description"":""The type of the artifact repository"",""example"":""maven""},{""name"":""url"",""type"":""String"",""displayName"":""URL"",""description"":""The url of the artifact repository"",""example"":""https://repo.spring.io""},{""name"":""purpose"",""type"":""Purpose"",""displayName"":""Purpose"",""description"":""The purpose of this repository in terms of resolving project or plugin dependencies"",""valid"":[""Project"",""Plugin""]}]",
maven,org.openrewrite:rewrite-gradle,org.openrewrite.gradle.search.FindDependency,Find Gradle dependency,"Finds dependencies declared in gradle build files. 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.
maven,org.openrewrite:rewrite-gradle,org.openrewrite.gradle.search.FindDependency,Find Gradle dependency,"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""/>.",1,Search,Gradle,"[{""name"":""groupId"",""type"":""String"",""displayName"":""Group"",""description"":""The first part of a dependency coordinate identifying its publisher."",""example"":""com.google.guava"",""required"":true},{""name"":""artifactId"",""type"":""String"",""displayName"":""Artifact"",""description"":""The second part of a dependency coordinate uniquely identifying it among artifacts from the same publisher."",""example"":""guava"",""required"":true},{""name"":""configuration"",""type"":""String"",""displayName"":""Dependency configuration"",""description"":""The dependency configuration to search for dependencies in. If omitted then all configurations will be searched."",""example"":""api""},{""name"":""version"",""type"":""String"",""displayName"":""Version"",""description"":""An exact version number or node-style semver selector used to select the version number."",""example"":""3.0.0""},{""name"":""versionPattern"",""type"":""String"",""displayName"":""Version pattern"",""description"":""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"",""example"":""-jre""}]",
<img alt=""Gradle test classpath"" src=""https://docs.gradle.org/current/userguide/img/java-library-ignore-deprecated-test.png"" width=""200px""/>.",1,Search,Gradle,"[{""name"":""groupId"",""type"":""String"",""displayName"":""Group"",""description"":""The first part of a dependency coordinate identifying its publisher."",""example"":""com.google.guava"",""required"":true},{""name"":""artifactId"",""type"":""String"",""displayName"":""Artifact"",""description"":""The second part of a dependency coordinate uniquely identifying it among artifacts from the same publisher."",""example"":""guava"",""required"":true},{""name"":""configuration"",""type"":""String"",""displayName"":""Dependency configuration"",""description"":""The dependency configuration to search for dependencies in. If omitted then all configurations will be searched."",""example"":""api""},{""name"":""version"",""type"":""String"",""displayName"":""Version"",""description"":""An exact version number or node-style semver selector used to select the version number."",""example"":""3.0.0""},{""name"":""versionPattern"",""type"":""String"",""displayName"":""Version pattern"",""description"":""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"",""example"":""-jre""}]","[{""name"":""org.openrewrite.maven.table.DependenciesDeclared"",""displayName"":""Dependencies declared"",""instanceName"":""Dependencies declared"",""description"":""Direct (first-order) dependencies declared by the project."",""columns"":[{""name"":""projectName"",""type"":""String"",""displayName"":""Project name"",""description"":""The name of the project that contains the dependency.""},{""name"":""sourceSet"",""type"":""String"",""displayName"":""Source set"",""description"":""The source set that contains the dependency.""},{""name"":""groupId"",""type"":""String"",""displayName"":""Group"",""description"":""The first part of a dependency coordinate `com.google.guava:guava:VERSION`.""},{""name"":""artifactId"",""type"":""String"",""displayName"":""Artifact"",""description"":""The second part of a dependency coordinate `com.google.guava:guava:VERSION`.""},{""name"":""version"",""type"":""String"",""displayName"":""Version"",""description"":""The resolved version.""},{""name"":""datedSnapshotVersion"",""type"":""String"",""displayName"":""Dated snapshot version"",""description"":""The resolved dated snapshot version or `null` if this dependency is not a snapshot.""},{""name"":""scope"",""type"":""String"",""displayName"":""Scope"",""description"":""Maven scope (e.g. `compile`, `test`) or Gradle configuration name (e.g. `implementation`, `testImplementation`). For Maven, defaults to `compile` when no scope is declared.""}]}]"
maven,org.openrewrite:rewrite-gradle,org.openrewrite.gradle.search.DependencyInsight,Gradle dependency insight,"Find direct and transitive dependencies matching a group, artifact, resolved version, and optionally a configuration name. Results include dependencies that either directly match or transitively include a matching dependency.",1,Search,Gradle,"[{""name"":""groupIdPattern"",""type"":""String"",""displayName"":""Group pattern"",""description"":""Group glob pattern used to match dependencies."",""example"":""com.fasterxml.jackson.module"",""required"":true},{""name"":""artifactIdPattern"",""type"":""String"",""displayName"":""Artifact pattern"",""description"":""Artifact glob pattern used to match dependencies."",""example"":""jackson-module-*"",""required"":true},{""name"":""version"",""type"":""String"",""displayName"":""Version"",""description"":""Match only dependencies with the specified resolved version. Node-style [version selectors](https://docs.openrewrite.org/reference/dependency-version-selectors) may be used.All versions are searched by default."",""example"":""1.x""},{""name"":""configuration"",""type"":""String"",""displayName"":""Scope"",""description"":""Match dependencies with the specified scope. If not specified, all configurations will be searched."",""example"":""compileClasspath""}]","[{""name"":""org.openrewrite.maven.table.DependenciesInUse"",""displayName"":""Dependencies in use"",""instanceName"":""Dependencies in use"",""description"":""Direct and transitive dependencies in use."",""columns"":[{""name"":""projectName"",""type"":""String"",""displayName"":""Project name"",""description"":""The name of the project that contains the dependency.""},{""name"":""sourceSet"",""type"":""String"",""displayName"":""Source set"",""description"":""The source set that contains the dependency.""},{""name"":""groupId"",""type"":""String"",""displayName"":""Group"",""description"":""The first part of a dependency coordinate `com.google.guava:guava:VERSION`.""},{""name"":""artifactId"",""type"":""String"",""displayName"":""Artifact"",""description"":""The second part of a dependency coordinate `com.google.guava:guava:VERSION`.""},{""name"":""version"",""type"":""String"",""displayName"":""Version"",""description"":""The resolved version.""},{""name"":""datedSnapshotVersion"",""type"":""String"",""displayName"":""Dated snapshot version"",""description"":""The resolved dated snapshot version or `null` if this dependency is not a snapshot.""},{""name"":""scope"",""type"":""String"",""displayName"":""Scope"",""description"":""Dependency scope. This will be `compile` if the dependency is direct and a scope is not explicitly specified in the POM.""},{""name"":""count"",""type"":""Integer"",""displayName"":""Count"",""description"":""How many times does this dependency appear.""}]},{""name"":""org.openrewrite.maven.table.ExplainDependenciesInUse"",""displayName"":""Explain dependencies in use"",""instanceName"":""Explain dependencies in use"",""description"":""A dependency graph explainer similar to that shown by `gradle dependencyInsight` for each matching dependency. This table will contain a row per matching dependency per configuration per (sub)project."",""columns"":[{""name"":""projectName"",""type"":""String"",""displayName"":""Project name"",""description"":""The name of the project that contains the dependency.""},{""name"":""sourceSet"",""type"":""String"",""displayName"":""Source set"",""description"":""The source set that contains the dependency.""},{""name"":""groupId"",""type"":""String"",""displayName"":""Group"",""description"":""The first part of a dependency coordinate `com.google.guava:guava:VERSION`.""},{""name"":""artifactId"",""type"":""String"",""displayName"":""Artifact"",""description"":""The second part of a dependency coordinate `com.google.guava:guava:VERSION`.""},{""name"":""version"",""type"":""String"",""displayName"":""Version"",""description"":""The resolved version.""},{""name"":""datedSnapshotVersion"",""type"":""String"",""displayName"":""Dated snapshot version"",""description"":""The resolved dated snapshot version or `null` if this dependency is not a snapshot.""},{""name"":""scope"",""type"":""String"",""displayName"":""Scope"",""description"":""Dependency scope. This will be `compile` if the dependency is direct and a scope is not explicitly specified in the POM.""},{""name"":""count"",""type"":""Integer"",""displayName"":""Count"",""description"":""How many times does this dependency appear.""},{""name"":""dependencyGraph"",""type"":""String"",""displayName"":""Dependency graph"",""description"":""The dependency paths that requested the dependency.""}]}]"
maven,org.openrewrite:rewrite-gradle,org.openrewrite.gradle.search.ModuleHasPlugin,Module has plugin,"Searches for Gradle Projects (modules) that have a plugin matching the specified id or implementing class. Places a `SearchResult` marker on all sources within a project with a matching plugin. This recipe is intended to be used as a precondition for other recipes. For example this could be used to limit the application of a spring boot migration to only projects that apply the spring dependency management plugin, limiting unnecessary upgrading. If the search result you want is instead just the build.gradle(.kts) file applying the plugin, use the `FindPlugins` recipe instead.",1,Search,Gradle,"[{""name"":""pluginId"",""type"":""String"",""displayName"":""Plugin id"",""description"":""The unique identifier used to apply a plugin in the `plugins` block. Note that this alone is insufficient to search for plugins applied by fully qualified class name and the `buildscript` block."",""example"":""`com.jfrog.bintray`"",""required"":true},{""name"":""pluginClass"",""type"":""String"",""displayName"":""Plugin class"",""description"":""The fully qualified name of a class implementing a Gradle plugin. "",""example"":""com.jfrog.bintray.gradle.BintrayPlugin""}]",
maven,org.openrewrite:rewrite-gradle,org.openrewrite.gradle.search.FindGradleWrapper,Find Gradle wrappers,Find Gradle wrappers.,1,Search,Gradle,"[{""name"":""version"",""type"":""String"",""displayName"":""Version expression"",""description"":""A version expression representing the versions to search for"",""example"":""7.x""},{""name"":""versionPattern"",""type"":""String"",""displayName"":""Version pattern"",""description"":""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"",""example"":""-jre""},{""name"":""distribution"",""type"":""String"",""displayName"":""Distribution type"",""description"":""The distribution of Gradle to find. \""bin\"" includes Gradle binaries. \""all\"" includes Gradle binaries, source code, and documentation."",""valid"":[""bin"",""all""]}]","[{""name"":""org.openrewrite.gradle.table.GradleWrappersInUse"",""displayName"":""Gradle wrappers in use"",""instanceName"":""Gradle wrappers in use"",""description"":""Gradle wrappers in use."",""columns"":[{""name"":""version"",""type"":""String"",""displayName"":""Wrapper version"",""description"":""The version of the Gradle wrapper in use.""},{""name"":""distribution"",""type"":""String"",""displayName"":""Wrapper distribution"",""description"":""The distribution type of the Gradle wrapper in use.""}]}]"
Expand Down
Loading
Loading