Skip to content

Commit

Permalink
Merge pull request #115 from chali/MissingReasonsInSubmodules
Browse files Browse the repository at this point in the history
When version is found through recursion find also reasons of recommendation
  • Loading branch information
chali authored Jan 14, 2020
2 parents 2f8147c + 88cfe8d commit 49e2364
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@
import org.gradle.api.plugins.ExtraPropertiesExtension;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.*;

public class DependencyRecommendationsPlugin implements Plugin<Project> {
public static final String NEBULA_RECOMMENDER_BOM = "nebulaRecommenderBom";
Expand Down Expand Up @@ -128,7 +126,7 @@ public void execute(DependencyResolveDetails details) {
String strategyText = whichStrategy(strategy);
logger.debug("Recommending version " + version + " for dependency " + coordinate);
details.because("Recommending version " + version + " for dependency " + coordinate + " via " + strategyText + "\n" +
"\twith reasons: " + StringUtils.join(recommendationProviderContainer.getReasons(), ", "));
"\twith reasons: " + StringUtils.join(getReasonsRecursive(project), ", "));
} else {
if (recommendationProviderContainer.isStrictMode()) {
String errorMessage = "Dependency " + details.getRequested().getGroup() + ":" + details.getRequested().getName() + " omitted version with no recommended version. General causes include a dependency being removed from the recommendation source or not applying a recommendation source to a project that depends on another project using a recommender.";
Expand Down Expand Up @@ -225,4 +223,20 @@ public String getRecommendedVersionRecursive(Project project, ModuleVersionSelec
return getRecommendedVersionRecursive(project.getParent(), mvSelector);
return null;
}

/**
* Look for recommendation reasons in a project and each of its ancestors in order until one is found or the root is reached
*
* @param project the gradle <code>Project</code>
* @return the recommended version or <code>null</code>
*/
public Set<String> getReasonsRecursive(Project project) {
Set<String> reasons = Objects.requireNonNull(project.getExtensions().findByType(RecommendationProviderContainer.class))
.getReasons();
if (! reasons.isEmpty())
return reasons;
if (project.getParent() != null)
return getReasonsRecursive(project.getParent());
return Collections.emptySet();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,57 @@ class DependencyRecommendationsPluginMultiprojectSpec extends IntegrationSpec {
//output where message is printed is different between Gradle 4.7 and 4.8 while we are testing Gradle 4.8 we need to check both
results.standardError.contains(expectedMessage) || results.standardOutput.contains(expectedMessage)
}

def 'recommendation is defined in root and we can see proper reasons in submodule dependency insight'() {
def repo = new MavenRepo()
repo.root = new File(projectDir, 'build/bomrepo')
def pom = new Pom('test.nebula.bom', 'multiprojectbom', '1.0.0', ArtifactType.POM)
pom.addManagementDependency('example', 'foo', '1.0.0')
pom.addManagementDependency('example', 'bar', '1.0.0')
repo.poms.add(pom)
repo.generate()
def depGraph = new DependencyGraphBuilder()
.addModule('example:foo:1.0.0')
.addModule('example:bar:1.0.0')
.build()
def generator = new GradleDependencyGenerator(depGraph)
generator.generateTestMavenRepo()

addSubproject('a', '''\
apply plugin: 'java'
dependencies {
implementation 'example:foo'
}
'''.stripIndent())

addSubproject('b', '''\
apply plugin: 'java'
dependencies {
implementation project(':a')
}
'''.stripIndent())

buildFile << """\
allprojects {
apply plugin: 'nebula.dependency-recommender'
repositories {
maven { url '${repo.root.absoluteFile.toURI()}' }
${generator.mavenRepositoryBlock}
}
}
dependencyRecommendations {
mavenBom module: 'test.nebula.bom:multiprojectbom:1.0.0@pom'
}
""".stripIndent()
when:
def results = runTasksSuccessfully(':a:dependencyInsight', '--dependency', 'foo', '--configuration', 'compileClasspath')

then:
results.standardOutput.contains 'Recommending version 1.0.0 for dependency example:foo'
results.standardOutput.contains 'nebula.dependency-recommender uses mavenBom: test.nebula.bom:multiprojectbom:pom:1.0.0'
}
}

0 comments on commit 49e2364

Please sign in to comment.