-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/main/groovy/nebula/plugin/publishing/ivy/IvyCompileOnlyPlugin.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/test/groovy/nebula/plugin/publishing/ivy/IvyCompileOnlyPluginIntegrationSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
} |