From 6826f856b6e19a4ddb86be9a2e55cd8136fd7893 Mon Sep 17 00:00:00 2001 From: Roberto Perez Alcolea Date: Tue, 17 Sep 2019 09:42:41 -0700 Subject: [PATCH] IvyRemovePlatformDependenciesPlugin: support platform coming from subproject. Fixes #146 --- .../ivy/PlatformDependencyVerifier.groovy | 18 ++-- ...emovePlatformDependenciesPluginSpec.groovy | 90 ++++++++++++++++++- 2 files changed, 101 insertions(+), 7 deletions(-) diff --git a/src/main/groovy/nebula/plugin/publishing/ivy/PlatformDependencyVerifier.groovy b/src/main/groovy/nebula/plugin/publishing/ivy/PlatformDependencyVerifier.groovy index c2da5b43..c02ca1c0 100644 --- a/src/main/groovy/nebula/plugin/publishing/ivy/PlatformDependencyVerifier.groovy +++ b/src/main/groovy/nebula/plugin/publishing/ivy/PlatformDependencyVerifier.groovy @@ -21,6 +21,9 @@ import org.gradle.api.Project import org.gradle.api.artifacts.Configuration import org.gradle.api.artifacts.ConfigurationContainer import org.gradle.api.artifacts.ModuleIdentifier +import org.gradle.api.artifacts.component.ComponentSelector +import org.gradle.api.artifacts.component.ModuleComponentSelector +import org.gradle.api.artifacts.component.ProjectComponentSelector /** * Verifies if a dependency has platform or enhanced-platform category attribute @@ -42,23 +45,28 @@ class PlatformDependencyVerifier { private static boolean checkIfPlatformDependency(Project project, String scope, String group, String name) { def platformDependencies = findPlatformDependencies(project)[scope] - return platformDependencies.find { requested -> requested.group == group && requested.name == name + return platformDependencies.find { ComponentSelector componentSelector -> + if(componentSelector instanceof ModuleComponentSelector) { + return componentSelector.moduleIdentifier.group == group && componentSelector.moduleIdentifier.name == name + } else if(componentSelector instanceof ProjectComponentSelector) { + return componentSelector.projectName == name + } } } @Memoized static Map> findPlatformDependencies(Project project) { ConfigurationContainer configurations = project.configurations - Map> dependencyMap = [:] + Map> dependencyMap = [:] dependencyMap['runtime'] = platformDependencies(configurations.runtimeClasspath) dependencyMap['compile'] = platformDependencies(configurations.compileClasspath) dependencyMap['test'] = platformDependencies(configurations.testRuntimeClasspath) dependencyMap } - static Set platformDependencies(Configuration configuration) { + static Set platformDependencies(Configuration configuration) { return configuration.incoming.resolutionResult.allDependencies.requested.findAll { - it.attributes.keySet().name.contains(CATEGORY_ATTRIBUTE) && it.attributes.findEntry(CATEGORY_ATTRIBUTE).get() in [REGULAR_PLATFORM, ENFORCED_PLATFORM] - }?.moduleIdentifier + it.attributes.keySet().name.contains(CATEGORY_ATTRIBUTE) && it.attributes.findEntry(CATEGORY_ATTRIBUTE).get() in [REGULAR_PLATFORM, ENFORCED_PLATFORM] + } } } diff --git a/src/test/groovy/nebula/plugin/publishing/ivy/IvyRemovePlatformDependenciesPluginSpec.groovy b/src/test/groovy/nebula/plugin/publishing/ivy/IvyRemovePlatformDependenciesPluginSpec.groovy index 3c4fe07d..eed64cf8 100644 --- a/src/test/groovy/nebula/plugin/publishing/ivy/IvyRemovePlatformDependenciesPluginSpec.groovy +++ b/src/test/groovy/nebula/plugin/publishing/ivy/IvyRemovePlatformDependenciesPluginSpec.groovy @@ -3,7 +3,9 @@ package nebula.plugin.publishing.ivy import nebula.test.IntegrationSpec import nebula.test.dependencies.DependencyGraphBuilder import nebula.test.dependencies.GradleDependencyGenerator +import spock.lang.Subject +@Subject(IvyRemovePlatformDependenciesPlugin) class IvyRemovePlatformDependenciesPluginSpec extends IntegrationSpec { File publishDir @@ -67,6 +69,90 @@ class IvyRemovePlatformDependenciesPluginSpec extends IntegrationSpec { !bom } + def 'publishes ivy descriptor without platform dependency - platform is a subproject'() { + buildFile << """\ + allprojects { + ${applyPlugin(IvyResolvedDependenciesPlugin)} + ${applyPlugin(IvyNebulaPublishPlugin)} + ${applyPlugin(IvyRemovePlatformDependenciesPlugin)} + + version = '0.1.0' + group = 'test.nebula' + + publishing { + repositories { + ivy { + name = 'testLocal' + url = 'testrepo' + } + } + } + } + """.stripIndent() + + + addSubproject('platform', """ +plugins { + id 'java-platform' +} + +dependencies { + constraints { + api 'commons-httpclient:commons-httpclient:3.1' + runtime 'org.postgresql:postgresql:42.2.5' + } +} + +publishing { + publications { + myPlatform(IvyPublication) { + from components.javaPlatform + } + } +} + """) + + + def graph = new DependencyGraphBuilder().addModule('test.resolved:a:1.0.0') + .addModule('test.resolved:a:1.1.0') + .addModule('test.resolved:b:1.1.0').build() + def generator = new GradleDependencyGenerator(graph, "${projectDir}/testrepogen") + + generator.generateTestIvyRepo() + + + addSubproject('clienta', """ +plugins { + id 'java' +} + +repositories { + ${generator.ivyRepositoryBlock} + jcenter() +} + +dependencies { + implementation platform('com.github.sghill.jenkins:jenkins-bom:latest.release') + implementation platform(project(":platform")) + implementation 'test.resolved:a:1.+' +} + """) + + when: + def x = runTasks(':clienta:publishNebulaIvyPublicationToTestLocalRepository') + + then: + File ivyDescriptor = new File(buildFile.parentFile, 'clienta/build/publications/nebulaIvy/ivy.xml') + def a = findDependency('a', ivyDescriptor) + a.@rev == '1.1.0' + + def jenkinsBom = findDependency('jenkins-bom', ivyDescriptor) + !jenkinsBom + + def projectPlatform = findDependency('platform', ivyDescriptor) + !projectPlatform + } + def 'publishes ivy descriptor without enforced-platform dependency'() { buildFile << """\ ${applyPlugin(IvyResolvedDependenciesPlugin)} @@ -222,8 +308,8 @@ class IvyRemovePlatformDependenciesPluginSpec extends IntegrationSpec { } - def findDependency(String module) { - def root = new XmlSlurper().parseText(new File(publishDir, 'ivy-0.1.0.xml').text) + def findDependency(String module, File ivyDescriptor = new File(publishDir, 'ivy-0.1.0.xml')) { + def root = new XmlSlurper().parseText(ivyDescriptor.text) def d = root.dependencies.dependency.find { it.@name == module }