Skip to content

Commit

Permalink
Improve Gradle transitive dependency collection
Browse files Browse the repository at this point in the history
  • Loading branch information
RobiNino authored May 25, 2021
1 parent dfef185 commit 221430e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,17 @@ import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.file.FileCollection
import org.jfrog.gradle.plugin.artifactory.dsl.ArtifactoryPluginConvention
import org.jfrog.gradle.plugin.artifactory.extractor.ModuleInfoFileProducer
import org.jfrog.gradle.plugin.artifactory.extractor.listener.ArtifactoryDependencyResolutionListener
import org.jfrog.gradle.plugin.artifactory.extractor.listener.ProjectsEvaluatedBuildListener
import org.jfrog.gradle.plugin.artifactory.task.ArtifactoryTask
import org.jfrog.gradle.plugin.artifactory.task.DistributeBuildTask
import org.jfrog.gradle.plugin.artifactory.task.DeployTask
import org.jfrog.gradle.plugin.artifactory.task.DistributeBuildTask
import org.jfrog.gradle.plugin.artifactory.task.ExtractModuleTask
import org.jfrog.gradle.plugin.artifactory.extractor.ModuleInfoFileProducer
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import static org.jfrog.gradle.plugin.artifactory.task.ArtifactoryTask.ARTIFACTORY_PUBLISH_TASK_NAME
import static org.jfrog.gradle.plugin.artifactory.task.ArtifactoryTask.DEPLOY_TASK_NAME
import static org.jfrog.gradle.plugin.artifactory.task.ArtifactoryTask.EXTRACT_MODULE_TASK_NAME
import static org.jfrog.gradle.plugin.artifactory.task.ArtifactoryTask.*
import static org.jfrog.gradle.plugin.artifactory.task.DistributeBuildTask.DISTRIBUTE_TASK_NAME

abstract class ArtifactoryPluginBase implements Plugin<Project> {
Expand All @@ -55,6 +53,9 @@ abstract class ArtifactoryPluginBase implements Plugin<Project> {
if (isRootProject(project)) {
addDeployTask(project)
addDistributeBuildTask(project)

// Add a DependencyResolutionListener, to populate the dependency hierarchy map.
project.getGradle().addListener(artifactoryDependencyResolutionListener)
} else {
// Makes sure the plugin is applied in the root project
project.rootProject.getPluginManager().apply(ArtifactoryPlugin.class)
Expand All @@ -66,8 +67,6 @@ abstract class ArtifactoryPluginBase implements Plugin<Project> {
log.debug("Using Artifactory Plugin for ${project.path}")

project.gradle.addProjectEvaluationListener(new ProjectsEvaluatedBuildListener())
// Add a DependencyResolutionListener, to populate the dependency hierarchy map.
project.getGradle().addListener(artifactoryDependencyResolutionListener)
}

protected abstract ArtifactoryTask createArtifactoryPublishTask(Project project)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private List<Artifact> calculateArtifacts(Iterable<GradleDeployDetails> deployDe

private List<Dependency> calculateDependencies(Project project, String moduleId) throws Exception {
ArtifactoryDependencyResolutionListener artifactoryDependencyResolutionListener =
project.getPlugins().getPlugin(ArtifactoryPlugin.class).getArtifactoryDependencyResolutionListener();
project.getRootProject().getPlugins().getPlugin(ArtifactoryPlugin.class).getArtifactoryDependencyResolutionListener();
Map<String, String[][]> requestedByMap = artifactoryDependencyResolutionListener.getModulesHierarchyMap().get(moduleId);

Set<Configuration> configurationSet = project.getConfigurations();
Expand Down Expand Up @@ -152,8 +152,10 @@ public boolean apply(@Nullable Dependency input) {
.type(getTypeString(artifact.getType(),
artifact.getClassifier(), artifact.getExtension()))
.id(depId)
.scopes(Sets.newHashSet(configuration.getName()))
.requestedBy(requestedByMap.get(depId));
.scopes(Sets.newHashSet(configuration.getName()));
if (requestedByMap != null) {
dependencyBuilder.requestedBy(requestedByMap.get(depId));
}
if (file.isFile()) {
// In recent gradle builds (3.4+) subproject dependencies are represented by a dir not jar.
Map<String, String> checksums = FileChecksumCalculator.calculateChecksums(file, MD5, SHA1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,16 @@ class ArtifactoryDependencyResolutionListener implements DependencyResolutionLis
for (DependencyResult dependency : dependencies) {
// Update the map for every resolved dependency.
if (dependency instanceof ResolvedDependencyResult) {
List<String> newDependentsList = new ArrayList<>()
populateDependentsList(dependency, newDependentsList)

// Add the new dependent's path to root to the 2d array.
String compId = getGav(dependency.getSelected().getModuleVersion())
String[][] curDependents = hierarchyMap[compId]
curDependents = ArrayUtils.add(curDependents, newDependentsList as String[])
hierarchyMap[compId] = curDependents
// If already collected for this compId, skip.
if (curDependents == null) {
List<String> newDependentsList = new ArrayList<>()
populateDependentsList(dependency, newDependentsList)
// Add the new dependent's path to root to the 2d array.
curDependents = ArrayUtils.add(curDependents, newDependentsList as String[])
hierarchyMap[compId] = curDependents
}
}
}
}
Expand Down

0 comments on commit 221430e

Please sign in to comment.