Skip to content

Commit

Permalink
Merge pull request #147 from nebula-plugins/IvyRemovePlatformDependen…
Browse files Browse the repository at this point in the history
…ciesPlugin-project-platform-support

IvyRemovePlatformDependenciesPlugin: support platform coming from project.
  • Loading branch information
rpalcolea authored Sep 17, 2019
2 parents 7e8a83b + 6826f85 commit 8c767d1
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String, Set<? extends ModuleIdentifier>> findPlatformDependencies(Project project) {
ConfigurationContainer configurations = project.configurations
Map<String, Set<? extends ModuleIdentifier>> dependencyMap = [:]
Map<String, Set<? extends ComponentSelector>> dependencyMap = [:]
dependencyMap['runtime'] = platformDependencies(configurations.runtimeClasspath)
dependencyMap['compile'] = platformDependencies(configurations.compileClasspath)
dependencyMap['test'] = platformDependencies(configurations.testRuntimeClasspath)
dependencyMap
}

static Set<? extends ModuleIdentifier> platformDependencies(Configuration configuration) {
static Set<? extends ComponentSelector> 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]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 8c767d1

Please sign in to comment.