From 742dc19b0cd2250be12883fdf4701098b1dada20 Mon Sep 17 00:00:00 2001 From: Roberto Perez Alcolea Date: Wed, 17 Jul 2019 13:27:18 -0700 Subject: [PATCH] Introduce MavenRemoveInvalidDependenciesPlugin to remove dependencies without versions --- ...avenRemoveInvalidDependenciesPlugin.groovy | 35 ++++++++ ...oveInvalidDependendenciesPluginSpec.groovy | 81 +++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 src/main/groovy/nebula/plugin/publishing/maven/MavenRemoveInvalidDependenciesPlugin.groovy create mode 100644 src/test/groovy/nebula/plugin/publishing/maven/MavenRemoveInvalidDependendenciesPluginSpec.groovy diff --git a/src/main/groovy/nebula/plugin/publishing/maven/MavenRemoveInvalidDependenciesPlugin.groovy b/src/main/groovy/nebula/plugin/publishing/maven/MavenRemoveInvalidDependenciesPlugin.groovy new file mode 100644 index 00000000..9f3c9531 --- /dev/null +++ b/src/main/groovy/nebula/plugin/publishing/maven/MavenRemoveInvalidDependenciesPlugin.groovy @@ -0,0 +1,35 @@ +package nebula.plugin.publishing.maven + +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.publish.maven.MavenPublication + +class MavenRemoveInvalidDependenciesPlugin implements Plugin { + @Override + void apply(Project project) { + project.afterEvaluate { + project.publishing { + publications { + withType(MavenPublication) { + pom.withXml { XmlProvider xml -> + project.plugins.withType(JavaBasePlugin) { + def dependencies = xml.asNode()?.dependencies?.dependency + dependencies?.each { Node dep -> + String version = dep.version.text() + if(!version) { + dep.parent().remove(dep) + } + + } + } + } + } + } + } + } + } +} + + diff --git a/src/test/groovy/nebula/plugin/publishing/maven/MavenRemoveInvalidDependendenciesPluginSpec.groovy b/src/test/groovy/nebula/plugin/publishing/maven/MavenRemoveInvalidDependendenciesPluginSpec.groovy new file mode 100644 index 00000000..c2870197 --- /dev/null +++ b/src/test/groovy/nebula/plugin/publishing/maven/MavenRemoveInvalidDependendenciesPluginSpec.groovy @@ -0,0 +1,81 @@ +package nebula.plugin.publishing.maven + + +import nebula.test.IntegrationSpec +import nebula.test.dependencies.DependencyGraphBuilder +import nebula.test.dependencies.GradleDependencyGenerator + +class MavenRemoveInvalidDependendenciesPluginSpec extends IntegrationSpec { + File publishDir + + def setup() { + settingsFile << '''\ + rootProject.name = 'resolvedmaventest' + '''.stripIndent() + + publishDir = new File(projectDir, 'testrepo/test/nebula/resolvedmaventest/0.1.0') + } + + def 'publishes maven descriptor without platform dependency'() { + buildFile << """\ + ${applyPlugin(MavenResolvedDependenciesPlugin)} + ${applyPlugin(MavenNebulaPublishPlugin)} + ${applyPlugin(MavenRemoveInvalidDependenciesPlugin)} + + version = '0.1.0' + group = 'test.nebula' + + publishing { + repositories { + maven { + name = 'testLocal' + url = 'testrepo' + } + } + } + """.stripIndent() + + + def graph = new DependencyGraphBuilder().addModule('test.resolved:a:1.0.0') + .addModule('test.resolved:a:1.1.0') + .addModule('test.resolved:b:1.1.0').build() + File mavenrepo = new GradleDependencyGenerator(graph, "${projectDir}/testrepogen").generateTestMavenRepo() + + buildFile << """\ + apply plugin: 'java' + + repositories { maven { url '${mavenrepo.absolutePath}' } } + + + dependencies { + implementation 'test.resolved:a' + implementation 'test.resolved:b:1.+' + modules { + module("test.resolved:a") { + replacedBy("test.resolved:b", "b is better") + } + } + } + + """.stripIndent() + + when: + runTasks('publishNebulaPublicationToTestLocalRepository') + + then: + def a = findDependency('a') + !a + + def b = findDependency('b') + b.version == '1.1.0' + } + + def findDependency(String artifactId) { + def root = new XmlSlurper().parseText(new File(publishDir, 'resolvedmaventest-0.1.0.pom').text) + def d = root.dependencies.dependency.find { + it.artifactId == artifactId + } + return d + } + +} \ No newline at end of file