Skip to content

Commit

Permalink
Make OptionalBasePlugin no-op
Browse files Browse the repository at this point in the history
  • Loading branch information
rpalcolea committed Mar 21, 2023
1 parent 3c19b73 commit e8764db
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 369 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,95 +17,14 @@ package nebula.plugin.extraconfigurations

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.publish.PublishingExtension
import org.gradle.api.publish.ivy.IvyPublication
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.publish.plugins.PublishingPlugin
import org.slf4j.Logger
import org.slf4j.LoggerFactory

class OptionalBasePlugin implements Plugin<Project> {
static final String OPTIONAL_IDENTIFIER = 'optional'
static final Logger LOGGER = LoggerFactory.getLogger(ProvidedBasePlugin)

@Override
void apply(Project project) {
enhanceProjectModel(project)
configureMavenPublishPlugin(project)
configureIvyPublishPlugin(project)
}

/**
* Enhances the Project domain object by adding
*
* a) a extra property List that holds optional dependencies
* b) a extra method that can be executed as parameter when declaring dependencies
*
* @param project Project
*/
private void enhanceProjectModel(Project project) {
project.ext.optionalDeps = []

project.ext.optional = { dep ->
project.ext.optionalDeps << dep
}
}

/**
* Configures Maven Publishing plugin to ensure that published dependencies receive the optional element.
*
* @param project Project
*/
private void configureMavenPublishPlugin(Project project) {
project.plugins.withType(PublishingPlugin) {
project.publishing {
publications {
project.extensions.findByType(PublishingExtension)?.publications?.withType(MavenPublication) { MavenPublication pub ->
pub.pom.withXml {
project.ext.optionalDeps.each { dep ->
def foundDep = asNode().dependencies.dependency.find {
it.groupId.text() == dep.group && it.artifactId.text() == dep.name
}

if (foundDep) {
if (foundDep.optional) {
foundDep.optional.value = 'true'
} else {
foundDep.appendNode(OPTIONAL_IDENTIFIER, 'true')
}
}
}
}
}
}
}
}
}

/**
* Configures Ivy Publishing plugin to ensure that published dependencies receive the correct conf attribute value.
*
* @param project Project
*/
private void configureIvyPublishPlugin(Project project) {
project.plugins.withType(PublishingPlugin) {
project.publishing {
publications {
project.extensions.findByType(PublishingExtension)?.publications?.withType(IvyPublication) { IvyPublication pub ->
pub.descriptor.withXml {
def rootNode = asNode()

// Add optional configuration if it doesn't exist yet
if (!rootNode.configurations.find { it.@name == OPTIONAL_IDENTIFIER }) {
rootNode.configurations[0].appendNode('conf', [name: OPTIONAL_IDENTIFIER, visibility: 'public'])
}

// Replace dependency "runtime->default" conf attribute value with "optional"
project.ext.optionalDeps.each { dep ->
def foundDep = rootNode.dependencies.dependency.find { it.@name == dep.name }
foundDep?.@conf = OPTIONAL_IDENTIFIER
}
}
}
}
}
}
LOGGER.warn("nebula.optional-base plugin does nothing. Please remove the plugin")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,197 +15,18 @@
*/
package nebula.plugin.extraconfigurations

import nebula.test.dependencies.DependencyGraph
import nebula.test.dependencies.DependencyGraphBuilder
import nebula.test.dependencies.GradleDependencyGenerator
import nebula.test.functional.ExecutionResult

class OptionalBasePluginIntegrationTest extends AbstractIntegrationTest {
def setup() {
buildFile << """
apply plugin: 'java-library'
apply plugin: 'com.netflix.nebula.optional-base'
"""
}

def "Can use optional"() {
given:
File baseDir = new File(projectDir, 'build')
File mavenRepoDir = new File(baseDir, 'mavenrepo')
def generator = new GradleDependencyGenerator(new DependencyGraph(['foo:bar:2.4 -> custom:baz:5.1.27', 'custom:baz:5.1.27']), baseDir.canonicalPath)
generator.generateTestMavenRepo()

when:
buildFile << """
repositories {
maven { url '${mavenRepoDir.toURI().toURL()}' }
}
dependencies {
implementation 'foo:bar:2.4', optional
}
"""
ExecutionResult result = runTasksSuccessfully('dependencies')
def output = result.standardOutput.readLines().join('\n').replaceAll("'implementation '", "'implementation'")

then:
output.contains("""compileClasspath - Compile classpath for source set 'main'.""")
output.contains("""\\--- foo:bar:2.4
\\--- custom:baz:5.1.27
""")
}

def "Can combine optional with other operators"() {
given:
File baseDir = new File(projectDir, 'build')
File mavenRepoDir = new File(baseDir, 'mavenrepo')
def generator = new GradleDependencyGenerator(new DependencyGraph(['foo:bar:2.4 -> custom:baz:5.1.27', 'custom:baz:5.1.27']), baseDir.canonicalPath)
generator.generateTestMavenRepo()

when:
buildFile << """
ext.excludeOptional = { dep ->
exclude module: 'baz'
optional(dep)
}
repositories {
maven { url '${mavenRepoDir.toURI().toURL()}' }
}
dependencies {
implementation 'foo:bar:2.4', excludeOptional
}
"""
ExecutionResult result = runTasksSuccessfully('dependencies')
def output = result.standardOutput.readLines().join('\n').replaceAll("'implementation '", "'implementation'")

then:
output.contains("""compileClasspath - Compile classpath for source set 'main'.""")
output.contains("""\\--- foo:bar:2.4
""")
!result.standardOutput.contains('custom:baz:5.1.27')
}

def "Publishing provided dependencies to a Maven repository preserves the scope when using Maven Publish plugin"() {
given:
File repoUrl = new File(projectDir, 'build/repo')

def "plugin can be applied and prints a warning"() {
when:
buildFile << """
apply plugin: 'maven-publish'
group = '$GROUP_ID'
version '$VERSION'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.apache.commons:commons-lang3:3.3.2', optional
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
repositories {
maven {
url '${repoUrl.toURI().toURL()}'
}
}
}
"""
runTasksSuccessfully('publish')

then:
assertOptionalDependencyInGeneratedPom(repoUrl, 'org.apache.commons', 'commons-lang3', '3.3.2', 'runtime')
}

def "Publishing optional dependencies to an Ivy repository preserves the scope"() {
given:
File repoUrl = new File(projectDir, 'build/repo')

when:
buildFile << """
apply plugin: 'ivy-publish'
group = '$GROUP_ID'
version '$VERSION'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.apache.commons:commons-lang3:3.3.2', optional
}
publishing {
publications {
ivyJava(IvyPublication) {
from components.java
}
}
repositories {
ivy {
url '${repoUrl.toURI().toURL()}'
}
}
}
apply plugin: 'java'
apply plugin: 'com.netflix.nebula.optional-base'
"""
runTasksSuccessfully('publish')

then:
assertOptionalDependencyInGeneratedIvy(repoUrl, 'org.apache.commons', 'commons-lang3', '3.3.2')
}

def 'still works if maven-publish publication is modified in after evaluate'() {
given:
def graph = new DependencyGraphBuilder().addModule('test.nebula:foo:1.0.0').build()
File mavenRepo = new GradleDependencyGenerator(graph, "${projectDir}/testrepogen").generateTestMavenRepo()
File repoUrl = new File(projectDir, 'build/repo')

buildFile << """\
apply plugin: 'maven-publish'
group = '$GROUP_ID'
version = '$VERSION'
repositories { maven { url '${mavenRepo.toURI().toURL()}' } }
dependencies {
api 'test.nebula:foo:1.0.0', optional
}
afterEvaluate {
publishing {
repositories {
maven {
name 'testRepo'
url '${repoUrl.toURI().toURL()}'
}
}
publications {
testMaven(MavenPublication) {
from components.java
}
}
}
}
""".stripIndent()

when:
runTasksSuccessfully('publish')
def result = runTasksSuccessfully('help')

then:
assertOptionalDependencyInGeneratedPom(repoUrl, 'test.nebula', 'foo', '1.0.0', 'compile')
result.standardOutput.contains("nebula.optional-base plugin does nothing")
}
}
Loading

0 comments on commit e8764db

Please sign in to comment.