Skip to content

Commit

Permalink
Merge pull request #60 from nebula-plugins/buildscript-classpath-depe…
Browse files Browse the repository at this point in the history
…ndencies

Add support for buildscript classpath dependencies to info report
  • Loading branch information
rpalcolea authored Feb 17, 2020
2 parents 387b3bf + cfc2c98 commit 624a659
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Project>, InfoCollectorPlugin {
private final DefaultVersionComparator versionComparator = new DefaultVersionComparator()
private final DefaultVersionComparator versionComparator = new DefaultVersionComparator()
private final VersionParser versionParser = new VersionParser()
private static final List<String> RESOLVABLE_WITH_DEPRECATION_CONF_SUFFIXES = ['compileOnly', 'compile', 'runtime']

Expand All @@ -38,35 +38,52 @@ class DependenciesInfoPlugin implements Plugin<Project>, 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)
}
}
}

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()) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ class InfoBrokerPluginIntegrationSpec extends IntegrationSpec {
result.standardOutput.contains(report)
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'() {
Expand Down Expand Up @@ -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}')
}

}

0 comments on commit 624a659

Please sign in to comment.