diff --git a/src/main/groovy/nebula/plugin/info/dependencies/DependenciesInfoPlugin.groovy b/src/main/groovy/nebula/plugin/info/dependencies/DependenciesInfoPlugin.groovy index b57764b..1444b9a 100644 --- a/src/main/groovy/nebula/plugin/info/dependencies/DependenciesInfoPlugin.groovy +++ b/src/main/groovy/nebula/plugin/info/dependencies/DependenciesInfoPlugin.groovy @@ -28,7 +28,7 @@ import org.gradle.api.internal.artifacts.ivyservice.ivyresolve.strategy.DefaultV import org.gradle.api.internal.artifacts.ivyservice.ivyresolve.strategy.VersionParser class DependenciesInfoPlugin implements Plugin, InfoCollectorPlugin { - private final DefaultVersionComparator versionComparator = new DefaultVersionComparator() + private final DefaultVersionComparator versionComparator = new DefaultVersionComparator() private final VersionParser versionParser = new VersionParser() private static final List RESOLVABLE_WITH_DEPRECATION_CONF_SUFFIXES = ['compileOnly', 'compile', 'runtime'] @@ -38,28 +38,8 @@ class DependenciesInfoPlugin implements Plugin, InfoCollectorPlugin { def dependencyMap = project.rootProject.property('nebulaInfoDependencies') Map dependencies = [:] project.plugins.withType(InfoBrokerPlugin) { InfoBrokerPlugin manifestPlugin -> - project.configurations.all( { Configuration conf -> - if (canBeResolved(conf)) { - conf.incoming.afterResolve { ResolvableDependencies resolvableDependencies -> - if (project.configurations.contains(conf)) { - String resolvedDependencies = resolvableDependencies.resolutionResult.allComponents.findAll { - it.id instanceof ModuleComponentIdentifier - }*.moduleVersion - .sort(true, { m1, m2 -> - if (m1.group != m2.group) - return m1.group <=> m2.group ?: -1 - if (m1.name != m2.name) - return m1.name <=> m2.name // name is required - versionComparator.compare(new VersionInfo(versionParser.transform(m1.version)), new VersionInfo(versionParser.transform(m2.version))) - })*.toString().join(',') - if (resolvedDependencies) { - dependencies.put("Resolved-Dependencies-${resolvableDependencies.name.capitalize()}", resolvedDependencies) - } - } - } - } - }) - + processBuildscriptClasspath(project, dependencies) + processProjectDependencies(project, dependencies) dependencyMap["${project.name}-dependencies".toString()] = dependencies if (project == project.rootProject) { manifestPlugin.addReport('resolved-dependencies', dependencyMap) @@ -67,6 +47,43 @@ class DependenciesInfoPlugin implements Plugin, InfoCollectorPlugin { } } + private void processBuildscriptClasspath(Project project, Map dependencies) { + Configuration buildscriptClasspath = project.buildscript.configurations.getByName('classpath') + if (canBeResolved(buildscriptClasspath)) { + ResolvableDependencies resolvableDependencies = buildscriptClasspath.incoming + processIncomingDependencies(resolvableDependencies, 'Resolved-Buildscript-Dependencies', dependencies) + } + } + + private void processProjectDependencies(Project project, Map dependencies) { + project.configurations.all({ Configuration conf -> + if (canBeResolved(conf)) { + conf.incoming.afterResolve { ResolvableDependencies resolvableDependencies -> + if (project.configurations.contains(conf)) { + processIncomingDependencies(resolvableDependencies, 'Resolved-Dependencies', dependencies) + } + } + } + }) + } + private void processIncomingDependencies(ResolvableDependencies resolvableDependencies, String prefix, Map dependencies) { + String resolvedDependencies = resolvableDependencies.resolutionResult.allComponents.findAll { + it.id instanceof ModuleComponentIdentifier + }*.moduleVersion + .sort(true, { m1, m2 -> + if (m1.group != m2.group) { + return m1.group <=> m2.group ?: -1 + } + if (m1.name != m2.name) { + return m1.name <=> m2.name + } // name is required + versionComparator.compare(new VersionInfo(versionParser.transform(m1.version)), new VersionInfo(versionParser.transform(m2.version))) + })*.toString().join(',') + if (resolvedDependencies) { + dependencies.put("$prefix-${resolvableDependencies.name.capitalize()}", resolvedDependencies) + } + } + private boolean canBeResolved(Configuration conf) { conf.isCanBeResolved() && !RESOLVABLE_WITH_DEPRECATION_CONF_SUFFIXES.any { conf.name.toLowerCase().endsWith(it.toLowerCase()) } } diff --git a/src/test/groovy/nebula/plugin/info/InfoBrokerPluginIntegrationSpec.groovy b/src/test/groovy/nebula/plugin/info/InfoBrokerPluginIntegrationSpec.groovy index 529a70e..5350765 100644 --- a/src/test/groovy/nebula/plugin/info/InfoBrokerPluginIntegrationSpec.groovy +++ b/src/test/groovy/nebula/plugin/info/InfoBrokerPluginIntegrationSpec.groovy @@ -48,4 +48,5 @@ class InfoBrokerPluginIntegrationSpec extends IntegrationSpec { result.standardOutput.contains(report) } + } diff --git a/src/test/groovy/nebula/plugin/info/InfoPluginIntegrationSpec.groovy b/src/test/groovy/nebula/plugin/info/InfoPluginIntegrationSpec.groovy index ce97618..78f40f4 100644 --- a/src/test/groovy/nebula/plugin/info/InfoPluginIntegrationSpec.groovy +++ b/src/test/groovy/nebula/plugin/info/InfoPluginIntegrationSpec.groovy @@ -22,6 +22,15 @@ class InfoPluginIntegrationSpec extends IntegrationSpec { def 'it returns build reports at the end of the build'() { given: buildFile << """ + buildscript { + repositories { + mavenCentral() + } + dependencies { + classpath "com.google.guava:guava:21.0" + } + } + ${applyPlugin(InfoPlugin)} apply plugin: 'java' @@ -36,13 +45,16 @@ class InfoPluginIntegrationSpec extends IntegrationSpec { } """.stripIndent() + settingsFile << """ + rootProject.name='buildscript-singlemodule-test' + """ this.writeHelloWorld('com.nebula.test') when: ExecutionResult result = runTasksSuccessfully('assemble') then: - result.standardOutput.contains('-dependencies={Resolved-Dependencies-CompileClasspath=com.google.guava:guava:18.0}}') + result.standardOutput.contains('{buildscript-singlemodule-test-dependencies={Resolved-Buildscript-Dependencies-Classpath=com.google.guava:guava:21.0, Resolved-Dependencies-CompileClasspath=com.google.guava:guava:18.0}}') } def 'it returns build reports at the end of multiproject build'() { @@ -84,4 +96,60 @@ class InfoPluginIntegrationSpec extends IntegrationSpec { result.standardOutput.contains('common-dependencies={Resolved-Dependencies-CompileClasspath=com.google.guava:guava:18.0}') result.standardOutput.contains('app-dependencies={Resolved-Dependencies-CompileClasspath=com.google.guava:guava:19.0}') } + + def 'it returns build reports at the end of multiproject build - with buildscript classpath info'() { + given: + buildFile << """ + buildscript { + repositories { + mavenCentral() + } + dependencies { + classpath "com.google.guava:guava:21.0" + } + } + + allprojects { + ${applyPlugin(InfoPlugin)} + } + + subprojects { + repositories { jcenter() } + } + + def broker = project.plugins.getPlugin(${InfoBrokerPlugin.name}) + + gradle.buildFinished { + println broker.buildReports().get('resolved-dependencies') + } + """.stripIndent() + + settingsFile << """ + rootProject.name='buildscript-multimodule-test' + """ + + def common = addSubproject('common', '''\ + apply plugin: 'java' + dependencies { + implementation 'com.google.guava:guava:18.0' + } + '''.stripIndent()) + writeHelloWorld('nebula.common', common) + def app = addSubproject('app', '''\ + apply plugin: 'java' + dependencies { + implementation 'com.google.guava:guava:19.0' + } + '''.stripIndent()) + writeHelloWorld('nebula.app', app) + + when: + ExecutionResult result = runTasksSuccessfully('build') + + then: + result.standardOutput.contains('buildscript-multimodule-test-dependencies={Resolved-Buildscript-Dependencies-Classpath=com.google.guava:guava:21.0}') + result.standardOutput.contains('common-dependencies={Resolved-Dependencies-CompileClasspath=com.google.guava:guava:18.0}') + result.standardOutput.contains('app-dependencies={Resolved-Dependencies-CompileClasspath=com.google.guava:guava:19.0}') + } + }