diff --git a/src/main/groovy/nebula/plugin/publishing/ivy/IvyCompileOnlyPlugin.groovy b/src/main/groovy/nebula/plugin/publishing/ivy/IvyCompileOnlyPlugin.groovy index 99f9fe2..e05ce35 100644 --- a/src/main/groovy/nebula/plugin/publishing/ivy/IvyCompileOnlyPlugin.groovy +++ b/src/main/groovy/nebula/plugin/publishing/ivy/IvyCompileOnlyPlugin.groovy @@ -15,18 +15,19 @@ */ package nebula.plugin.publishing.ivy +import groovy.transform.Canonical import groovy.transform.CompileDynamic import org.gradle.api.Action import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.XmlProvider -import org.gradle.api.plugins.JavaBasePlugin import org.gradle.api.plugins.JavaPlugin import org.gradle.api.publish.PublicationContainer import org.gradle.api.publish.PublishingExtension import org.gradle.api.publish.ivy.IvyModuleDescriptorSpec import org.gradle.api.publish.ivy.IvyPublication +@CompileDynamic class IvyCompileOnlyPlugin implements Plugin { static enum DependenciesContent { @@ -41,46 +42,57 @@ class IvyCompileOnlyPlugin implements Plugin { PublishingExtension publishing = project.extensions.getByType(PublishingExtension) project.plugins.withType(JavaPlugin) { JavaPlugin javaBasePlugin -> - - publishing.publications(new Action() { + project.afterEvaluate(new Action() { @Override - void execute(PublicationContainer publications) { - publications.withType(IvyPublication) { IvyPublication publication -> - publication.descriptor(new Action() { - @Override - void execute(IvyModuleDescriptorSpec ivyModuleDescriptorSpec) { - ivyModuleDescriptorSpec.withXml(new Action() { + void execute(Project p) { + def compileOnlyDependencies = project.configurations.compileOnly.incoming.dependencies.collect { + new Dependency(it.group, it.name, it.version) + } + + publishing.publications(new Action() { + @Override + void execute(PublicationContainer publications) { + publications.withType(IvyPublication) { IvyPublication publication -> + publication.descriptor(new Action() { @Override - void execute(XmlProvider xml) { - configureXml(project, xml) + void execute(IvyModuleDescriptorSpec ivyModuleDescriptorSpec) { + ivyModuleDescriptorSpec.withXml(new Action() { + @Override + void execute(XmlProvider xml) { + def root = xml.asNode() + def dependencies = compileOnlyDependencies + if (dependencies.size() > 0) { + def confs = root.configurations ? root.configurations[0] : root.appendNode('configurations') + confs.appendNode('conf', [name: 'provided', visibility: 'public']) + def deps = root.dependencies ? root.dependencies[0] : root.appendNode('dependencies') + dependencies.each { dep -> + def newDep = deps.appendNode('dependency') + newDep.@org = dep.organisation + newDep.@name = dep.module + newDep.@rev = dep.version + newDep.@conf = 'provided' + } + deps.children().sort(true, { + DependenciesContent.valueOf(it.name()).ordinal() + }) + } + } + }) } }) } - }) - } + } + }) } }) + } } - @CompileDynamic - private void configureXml(Project project, XmlProvider xml) { - def root = xml.asNode() - def dependencies = project.configurations.compileOnly.dependencies - if (dependencies.size() > 0) { - def confs = root.configurations ? root.configurations[0] : root.appendNode('configurations') - confs.appendNode('conf', [name: 'provided', visibility: 'public']) - def deps = root.dependencies ? root.dependencies[0] : root.appendNode('dependencies') - dependencies.each { dep -> - def newDep = deps.appendNode('dependency') - newDep.@org = dep.group - newDep.@name = dep.name - newDep.@rev = dep.version - newDep.@conf = 'provided' - } - deps.children().sort(true, { - DependenciesContent.valueOf(it.name()).ordinal() - }) - } + @Canonical + private static class Dependency { + String organisation + String module + String version } } diff --git a/src/test/groovy/nebula/plugin/publishing/ivy/IvyCompileOnlyPluginIntegrationSpec.groovy b/src/test/groovy/nebula/plugin/publishing/ivy/IvyCompileOnlyPluginIntegrationSpec.groovy index b5141b8..fdc8908 100644 --- a/src/test/groovy/nebula/plugin/publishing/ivy/IvyCompileOnlyPluginIntegrationSpec.groovy +++ b/src/test/groovy/nebula/plugin/publishing/ivy/IvyCompileOnlyPluginIntegrationSpec.groovy @@ -18,7 +18,9 @@ package nebula.plugin.publishing.ivy import nebula.plugin.publishing.BaseIntegrationTestKitSpec import nebula.test.dependencies.DependencyGraphBuilder import nebula.test.dependencies.GradleDependencyGenerator +import spock.lang.Subject +@Subject(IvyCompileOnlyPlugin) class IvyCompileOnlyPluginIntegrationSpec extends BaseIntegrationTestKitSpec { File publishDir @@ -32,6 +34,10 @@ class IvyCompileOnlyPluginIntegrationSpec extends BaseIntegrationTestKitSpec { version = '0.1.0' group = 'test.nebula' + repositories { + mavenCentral() + } + publishing { repositories { ivy { @@ -51,18 +57,10 @@ class IvyCompileOnlyPluginIntegrationSpec extends BaseIntegrationTestKitSpec { def 'verify ivy contains compileOnly dependencies'() { keepFiles = true - def graph = new DependencyGraphBuilder().addModule('testjava:c:0.0.1').build() - File ivyrepo = new GradleDependencyGenerator(graph, "${projectDir}/testrepogen").generateTestIvyRepo() - buildFile << """\ apply plugin: 'java' - - repositories { - ivy { url '${ivyrepo.toURI().toURL()}' } - } - dependencies { - compileOnly 'testjava:c:0.0.1' + compileOnly 'com.google.guava:guava:19.0' } """.stripIndent() @@ -72,30 +70,23 @@ class IvyCompileOnlyPluginIntegrationSpec extends BaseIntegrationTestKitSpec { then: def root = new XmlSlurper().parseText(new File(publishDir, 'ivy-0.1.0.xml').text) def dependency = root.dependencies.dependency[0] - dependency.@org == 'testjava' - dependency.@name == 'c' - dependency.@rev == '0.0.1' + dependency.@org == 'com.google.guava' + dependency.@name == 'guava' + dependency.@rev == '19.0' dependency.@conf == 'provided' } def 'verify ivy contains compileOnly dependencies together with global excludes'() { keepFiles = true - def graph = new DependencyGraphBuilder().addModule('testjava:c:0.0.1').build() - File ivyrepo = new GradleDependencyGenerator(graph, "${projectDir}/testrepogen").generateTestIvyRepo() buildFile << """\ apply plugin: 'java' - - repositories { - ivy { url '${ivyrepo.toURI().toURL()}' } - } - configurations.all { exclude group: 'org.slf4j', module: 'slf4j-api' } dependencies { - compileOnly 'testjava:c:0.0.1' + compileOnly 'com.google.guava:guava:19.0' } """.stripIndent() @@ -105,9 +96,9 @@ class IvyCompileOnlyPluginIntegrationSpec extends BaseIntegrationTestKitSpec { then: def root = new XmlSlurper().parseText(new File(publishDir, 'ivy-0.1.0.xml').text) def dependency = root.dependencies.dependency[0] - dependency.@org == 'testjava' - dependency.@name == 'c' - dependency.@rev == '0.0.1' + dependency.@org == 'com.google.guava' + dependency.@name == 'guava' + dependency.@rev == '19.0' dependency.@conf == 'provided' } }