Skip to content

Commit

Permalink
Restore IvyCompileOnlyPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
rpalcolea committed Nov 20, 2023
1 parent 0bf65e2 commit 5ffcbb4
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 0 deletions.
8 changes: 8 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,14 @@ gradlePlugin {
tags.set(['nebula', 'publish', 'ivy'])
}

ivyCompileOnly {
id = 'com.netflix.nebula.ivy-compile-only'
displayName = 'com.netflix.nebula.ivy-compile-only'
description = 'Plugins to ease maven-publish and ivy-publish configuration'
implementationClass = 'nebula.plugin.publishing.ivy.IvyCompileOnlyPlugin'
tags.set(['nebula', 'publish', 'ivy'])
}

publishVerification {
id = 'com.netflix.nebula.publish-verification'
displayName = 'com.netflix.nebula.publish-verification'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
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.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 {
dependency,
exclude,
override,
conflict
}

void apply(Project project) {
project.plugins.apply IvyBasePublishPlugin

PublishingExtension publishing = project.extensions.getByType(PublishingExtension)
project.plugins.withType(JavaPlugin) { JavaPlugin javaBasePlugin ->
project.afterEvaluate(new Action<Project>() {
@Override
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(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()
})
}
}
})
}
})
}
}
})
}
})

}
}

@Canonical
private static class Dependency {
String organisation
String module
String version
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class IvyPublishPlugin implements Plugin<Project> {
project.plugins.apply IvyNebulaPublishPlugin
project.plugins.apply IvyBasePublishPlugin
project.plugins.apply IvyResolvedDependenciesPlugin
project.plugins.apply IvyCompileOnlyPlugin
project.plugins.apply IvyManifestPlugin
project.plugins.apply IvyRemoveInvalidDependenciesPlugin
project.plugins.apply IvyShadowPublishPlugin
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
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

def setup() {
buildFile << """\
plugins {
id 'com.netflix.nebula.ivy-nebula-publish'
id 'com.netflix.nebula.ivy-compile-only'
}
version = '0.1.0'
group = 'test.nebula'
repositories {
mavenCentral()
}
publishing {
repositories {
ivy {
name = 'testLocal'
url = 'testrepo'
}
}
}
""".stripIndent()

settingsFile << '''\
rootProject.name = 'ivytest'
'''.stripIndent()

publishDir = new File(projectDir, 'testrepo/test.nebula/ivytest/0.1.0')
}

def 'verify ivy contains compileOnly dependencies'() {
keepFiles = true
buildFile << """\
apply plugin: 'java'
dependencies {
compileOnly 'com.google.guava:guava:19.0'
}
""".stripIndent()

when:
runTasks('publishNebulaIvyPublicationToTestLocalRepository')

then:
def root = new XmlSlurper().parseText(new File(publishDir, 'ivy-0.1.0.xml').text)
def dependency = root.dependencies.dependency[0]
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

buildFile << """\
apply plugin: 'java'
configurations.all {
exclude group: 'org.slf4j', module: 'slf4j-api'
}
dependencies {
compileOnly 'com.google.guava:guava:19.0'
}
""".stripIndent()

when:
runTasks('publishNebulaIvyPublicationToTestLocalRepository')

then:
def root = new XmlSlurper().parseText(new File(publishDir, 'ivy-0.1.0.xml').text)
def dependency = root.dependencies.dependency[0]
dependency.@org == 'com.google.guava'
dependency.@name == 'guava'
dependency.@rev == '19.0'
dependency.@conf == 'provided'
}
}

0 comments on commit 5ffcbb4

Please sign in to comment.