Skip to content

Commit

Permalink
Make IvyCompileOnlyPlugin config cache compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
rpalcolea committed Nov 15, 2023
1 parent 0c0edb1 commit b629334
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Project> {

static enum DependenciesContent {
Expand All @@ -41,46 +42,57 @@ class IvyCompileOnlyPlugin implements Plugin<Project> {

PublishingExtension publishing = project.extensions.getByType(PublishingExtension)
project.plugins.withType(JavaPlugin) { JavaPlugin javaBasePlugin ->

publishing.publications(new Action<PublicationContainer>() {
project.afterEvaluate(new Action<Project>() {
@Override
void execute(PublicationContainer publications) {
publications.withType(IvyPublication) { IvyPublication publication ->
publication.descriptor(new Action<IvyModuleDescriptorSpec>() {
@Override
void execute(IvyModuleDescriptorSpec ivyModuleDescriptorSpec) {
ivyModuleDescriptorSpec.withXml(new Action<XmlProvider>() {
void execute(Project p) {
def compileOnlyDependencies = project.configurations.compileOnly.incoming.dependencies.collect {
new Dependency(it.group, it.name, it.version)
}

publishing.publications(new Action<PublicationContainer>() {
@Override
void execute(PublicationContainer publications) {
publications.withType(IvyPublication) { IvyPublication publication ->
publication.descriptor(new Action<IvyModuleDescriptorSpec>() {
@Override
void execute(XmlProvider xml) {
configureXml(project, xml)
void execute(IvyModuleDescriptorSpec ivyModuleDescriptorSpec) {
ivyModuleDescriptorSpec.withXml(new Action<XmlProvider>() {
@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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -32,6 +34,10 @@ class IvyCompileOnlyPluginIntegrationSpec extends BaseIntegrationTestKitSpec {
version = '0.1.0'
group = 'test.nebula'
repositories {
mavenCentral()
}
publishing {
repositories {
ivy {
Expand All @@ -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()

Expand All @@ -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()

Expand All @@ -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'
}
}

0 comments on commit b629334

Please sign in to comment.