diff --git a/src/main/groovy/nebula/plugin/publishing/maven/MavenResolvedDependenciesPlugin.groovy b/src/main/groovy/nebula/plugin/publishing/maven/MavenResolvedDependenciesPlugin.groovy index 09c1ac9c..48ba324f 100644 --- a/src/main/groovy/nebula/plugin/publishing/maven/MavenResolvedDependenciesPlugin.groovy +++ b/src/main/groovy/nebula/plugin/publishing/maven/MavenResolvedDependenciesPlugin.groovy @@ -1,5 +1,5 @@ /* - * Copyright 2015 Netflix, Inc. + * Copyright 2015-2019 Netflix, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,17 +15,14 @@ */ package nebula.plugin.publishing.maven -import nebula.plugin.publishing.ivy.AbstractResolvedDependenciesPlugin +import org.gradle.api.Plugin import org.gradle.api.Project -import org.gradle.api.XmlProvider -import org.gradle.api.artifacts.ModuleVersionIdentifier -import org.gradle.api.plugins.JavaBasePlugin import org.gradle.api.publish.maven.MavenPublication /** * Replaces first order dependencies with the selected versions when publishing. */ -class MavenResolvedDependenciesPlugin extends AbstractResolvedDependenciesPlugin { +class MavenResolvedDependenciesPlugin implements Plugin { @Override void apply(Project project) { project.plugins.apply MavenBasePublishPlugin @@ -34,38 +31,9 @@ class MavenResolvedDependenciesPlugin extends AbstractResolvedDependenciesPlugin project.publishing { publications { withType(MavenPublication) { - pom.withXml { XmlProvider xml-> - project.plugins.withType(JavaBasePlugin) { - def dependencies = xml.asNode()?.dependencies?.dependency - dependencies?.each { Node dep -> - String scope = dep.scope.text() - String group = dep.groupId.text() - String name = dep.artifactId.text() - - ModuleVersionIdentifier mvid - if (scope == 'provided') { - scope = 'runtime' - mvid = selectedModuleVersion(project, scope, group, name) - if (!mvid) { - scope = 'compileOnly' - mvid = selectedModuleVersion(project, scope, group, name) - } - } else { - mvid = selectedModuleVersion(project, scope, group, name) - } - - if (!mvid) { - return // continue loop if a dependency is not found in dependencyMap - } - - def versionNode = dep.version - if (!versionNode) { - dep.appendNode('version') - } - dep.groupId[0].value = mvid.group - dep.artifactId[0].value = mvid.name - dep.version[0].value = mvid.version - } + versionMapping { + allVariants { + fromResolutionResult() } } } diff --git a/src/test/groovy/nebula/plugin/publishing/maven/MavenPublishPluginIntegrationSpec.groovy b/src/test/groovy/nebula/plugin/publishing/maven/MavenPublishPluginIntegrationSpec.groovy index ecd42075..d149330d 100644 --- a/src/test/groovy/nebula/plugin/publishing/maven/MavenPublishPluginIntegrationSpec.groovy +++ b/src/test/groovy/nebula/plugin/publishing/maven/MavenPublishPluginIntegrationSpec.groovy @@ -15,6 +15,7 @@ */ package nebula.plugin.publishing.maven +import groovy.json.JsonSlurper import nebula.test.IntegrationTestKitSpec import nebula.test.dependencies.DependencyGraphBuilder import nebula.test.dependencies.GradleDependencyGenerator @@ -43,10 +44,11 @@ class MavenPublishPluginIntegrationSpec extends IntegrationTestKitSpec { settingsFile << '''\ rootProject.name = 'mavenpublishingtest' + enableFeaturePreview("GRADLE_METADATA") '''.stripIndent() } - def 'all of the features work together'() { + def 'publish POM with resolved dependencies'() { def graph = new DependencyGraphBuilder() .addModule('test:a:0.0.1') .addModule('test:b:1.9.2') @@ -70,7 +72,7 @@ class MavenPublishPluginIntegrationSpec extends IntegrationTestKitSpec { dependencies { compile 'test:a:0.+' - compileOnly 'test:b:[1.0.0, 2.0.0)' + compile 'test:b:[1.0.0, 2.0.0)' } """.stripIndent() @@ -96,6 +98,57 @@ class MavenPublishPluginIntegrationSpec extends IntegrationTestKitSpec { then: b.version == '1.9.2' - b.scope == 'provided' } + + def 'produces gradle metadata file'() { + def graph = new DependencyGraphBuilder() + .addModule('test:a:0.0.1') + .addModule('test:b:1.9.2') + .build() + File mavenrepo = new GradleDependencyGenerator(graph, "${projectDir}/testrepogen").generateTestMavenRepo() + + buildFile << """\ + apply plugin: 'java' + apply plugin: 'nebula.contacts' + apply plugin: 'nebula.info' + + repositories { + maven { url '${mavenrepo.absolutePath}' } + } + + contacts { + 'nebula@example.test' { + moniker 'Nebula' + } + } + + dependencies { + compile 'test:a:0.+' + compile 'test:b:[1.0.0, 2.0.0)' + } + """.stripIndent() + + when: + runTasks('generateMetadataFileForNebulaPublication') + + + then: + def moduleJson = new JsonSlurper().parse(new File(projectDir, 'build/publications/nebula/module.json')) + def runtimeVariant = moduleJson.variants.find { it.name == 'runtimeElements'} + def dependencies = runtimeVariant.dependencies + dependencies.size() == 2 + + when: + def a = dependencies.find { it.module == 'a' } + + then: + a.version.requires == '0.0.1' + + when: + def b = dependencies.find { it.module == 'b' } + + then: + b.version.requires == '1.9.2' + } + } diff --git a/src/test/groovy/nebula/plugin/publishing/maven/MavenResolvedDependenciesPluginIntegrationSpec.groovy b/src/test/groovy/nebula/plugin/publishing/maven/MavenResolvedDependenciesPluginIntegrationSpec.groovy index d5e9d5f8..f81d9bda 100644 --- a/src/test/groovy/nebula/plugin/publishing/maven/MavenResolvedDependenciesPluginIntegrationSpec.groovy +++ b/src/test/groovy/nebula/plugin/publishing/maven/MavenResolvedDependenciesPluginIntegrationSpec.groovy @@ -214,32 +214,6 @@ class MavenResolvedDependenciesPluginIntegrationSpec extends IntegrationTestKitS d.version == '18.0' } - def 'module replacements reflected in published metadata'() { - buildFile << """\ - apply plugin: 'java' - - repositories { - jcenter() - } - - dependencies { - compile 'com.google.collections:google-collections:1.0' - compile 'com.google.truth:truth:0.28' - modules { - module('com.google.collections:google-collections') { - replacedBy('com.google.guava:guava') - } - } - } -""" - when: - runTasks('publishNebulaPublicationToTestLocalRepository') - - then: - def d = findDependency('guava') - d.version == '18.0' - } - def 'works with java-library plugin and dependency-recommender'() { buildFile.text = '''\ plugins { @@ -283,35 +257,6 @@ class MavenResolvedDependenciesPluginIntegrationSpec extends IntegrationTestKitS findDependencyInScope('google-collections', 'runtime').version == '1.0' } - def 'excluded first order dependencies fail the build'() { - def graph = new DependencyGraphBuilder().addModule('test.resolved:a:1.0.0') - .addModule(new ModuleBuilder('test.resolved:b:1.0.0').addDependency('test.resolved:a:1.0.0').build()) - .build() - File mavenrepo = new GradleDependencyGenerator(graph, "${projectDir}/testrepogen").generateTestMavenRepo() - - buildFile << """\ - apply plugin: 'java' - - repositories { maven { url '${mavenrepo.absolutePath}' } } - - configurations.all { - exclude group: 'test.resolved', module: 'a' - } - - dependencies { - compile 'test.resolved:b:1.0.0' - compile 'test.resolved:a' - } - """.stripIndent() - - when: - def results = runTasks('publishNebulaPublicationToTestLocalRepository') - - then: - UnexpectedBuildFailure ex = thrown() - ex.message.contains 'Direct dependency "test.resolved:a" is excluded, delete direct dependency or stop excluding it' - } - def 'dependency with no changes copied through'() { def graph = new DependencyGraphBuilder().addModule('test.resolved:a:1.0.0') .build()