Skip to content

Commit

Permalink
Merge pull request #127 from nebula-plugins/publish-modules-with-reso…
Browse files Browse the repository at this point in the history
…lved-versions

Leverage Gradle versionMapping to populate POM and Gradle module with proper resolved dependencies
  • Loading branch information
rpalcolea authored Apr 1, 2019
2 parents ab76c26 + bfdb47d commit b662e36
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 96 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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<Project> {
@Override
void apply(Project project) {
project.plugins.apply MavenBasePublishPlugin
Expand All @@ -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()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand All @@ -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()

Expand All @@ -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 {
'[email protected]' {
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'
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit b662e36

Please sign in to comment.