Skip to content

Commit

Permalink
Modify conf for provided dependencies when generating Ivy metadata.
Browse files Browse the repository at this point in the history
  • Loading branch information
bmuschko committed Jul 12, 2014
1 parent 3604ba1 commit 674b87a
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.publish.internal.DefaultPublishingExtension
import org.gradle.api.publish.ivy.IvyPublication
import org.gradle.api.publish.ivy.plugins.IvyPublishPlugin
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin
import org.gradle.plugins.ide.idea.IdeaPlugin
Expand All @@ -42,11 +44,7 @@ class ProvidedBasePlugin implements Plugin<Project> {

configureIdeaPlugin(project, providedConf)
configureMavenPublishPlugin(project, providedConf)

// provided needs to be available to compile, runtime, testCompile, testRuntime
// provided needs to be absent from ivy/pom
// or for ivy -- conf provided
// or for maven -- scope provided
configureIvyPublishPlugin(project, providedConf)
}
}

Expand All @@ -65,6 +63,7 @@ class ProvidedBasePlugin implements Plugin<Project> {
DefaultPublishingExtension publishingExtension = project.extensions.getByType(DefaultPublishingExtension)
publishingExtension.publications.withType(MavenPublication) {
pom.withXml {
// Replace dependency "runtime" scope element value with "provided"
asNode().dependencies.dependency.findAll {
it.scope.text() == JavaPlugin.RUNTIME_CONFIGURATION_NAME && providedConfiguration.allDependencies.find { dep ->
dep.name == it.artifactId.text()
Expand All @@ -78,4 +77,33 @@ class ProvidedBasePlugin implements Plugin<Project> {
}
}
}

private void configureIvyPublishPlugin(Project project, Configuration providedConfiguration) {
project.plugins.withType(IvyPublishPlugin) {
project.publishing {
publications {
DefaultPublishingExtension publishingExtension = project.extensions.getByType(DefaultPublishingExtension)
publishingExtension.publications.withType(IvyPublication) {
descriptor.withXml {
def rootNode = asNode()

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

// Replace dependency "runtime->default" conf attribute value with "provided"
rootNode.dependencies.dependency.findAll {
it.@conf == "$JavaPlugin.RUNTIME_CONFIGURATION_NAME->default" && providedConfiguration.allDependencies.find { dep ->
dep.name == it.@name
}
}.each { runtimeDep ->
runtimeDep.@conf = PROVIDED_CONFIGURATION_NAME
}
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,56 @@ publishing {
commonsLang.version.text() == '3.3.2'
commonsLang.scope.text() == 'provided'
}

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

when:
buildFile << """
apply plugin: 'java'
apply plugin: 'nebula-provided-base'
apply plugin: 'ivy-publish'
group = 'nebula.extraconf'
version '1.0'
repositories {
mavenCentral()
}
dependencies {
provided 'org.apache.commons:commons-lang3:3.3.2'
}
publishing {
publications {
ivyJava(IvyPublication) {
from components.java
}
}
repositories {
ivy {
url '$repoUrl.canonicalPath'
}
}
}
"""
createProductionJavaSourceFile()
runTasksSuccessfully('publish')

then:
true
File ivyFile = new File(repoUrl, "nebula.extraconf/$moduleName/1.0/ivy-1.0.xml")
ivyFile.exists()
def ivyXml = new XmlSlurper().parseText(ivyFile.text)
def dependencies = ivyXml.dependencies
dependencies.size() == 1
def commonsLang = dependencies.dependency[0]
commonsLang.@org.text() == 'org.apache.commons'
commonsLang.@name.text() == 'commons-lang3'
commonsLang.@rev.text() == '3.3.2'
commonsLang.@conf.text() == 'provided'
}
}

0 comments on commit 674b87a

Please sign in to comment.