Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use modern features of Gradle instead of custom scripts where possible #70

Merged
merged 1 commit into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 15 additions & 93 deletions lib/bom.gradle
Original file line number Diff line number Diff line change
@@ -1,110 +1,32 @@
import groovy.xml.QName

configure(projectsWithFlags('bom')) {
apply plugin: 'base'

dependencyManagement {
generatedPomCustomization {
enabled = true
}
}
apply plugin: 'java-platform'
apply plugin: 'maven-publish'

publishing {
publications {
bom(MavenPublication) {
artifactId project.ext.artifactId
pom.packaging = 'pom'
// Generate the POM.
pom.withXml {
findChildNode(asNode(), 'packaging') + {
resolveStrategy = DELEGATE_FIRST

// Write the elements required by OSSRH.
name "${project.ext.projectName} (${project.ext.artifactId})"
description "${project.ext.projectDescription} (${project.ext.artifactId})"
url "${project.ext.projectUrl}"
inceptionYear "${project.ext.inceptionYear}"

licenses {
license {
name "${project.ext.licenseName}"
url "${project.ext.licenseUrl}"
distribution 'repo'
}
}

developers {
developer {
name "${project.ext.authorName}"
email "${project.ext.authorEmail}"
url "${project.ext.authorUrl}"
}
}

scm {
url "${project.ext.scmUrl}"
connection "${project.ext.scmConnection}"
developerConnection "${project.ext.scmDeveloperConnection}"
}
}
from components.javaPlatform

// Find (or create) the 'dependencyManagement' section.
Node dependencyMgmt = findChildNode(asNode(), 'dependencyManagement')
if (dependencyMgmt == null) {
findChildNode(asNode(), 'scm') + {
resolveStrategy = DELEGATE_FIRST
dependencyManagement {}
}
dependencyMgmt = findChildNode(asNode(), 'dependencyManagement')
}

// Replace the `dependencyManagement` section populated by dependency-management-plugin
// or created by ourselves above.
dependencyMgmt.replaceNode {
resolveStrategy = DELEGATE_FIRST
dependencyManagement {
dependencies {
projectsWithFlags('java', 'publish').toList().sort { a, b ->
def groupComparison = "${a.group}".compareTo("${b.group}")
if (groupComparison != 0) {
return groupComparison
}
return "${a.ext.artifactId}".compareTo("${b.ext.artifactId}")
}.each { p ->
dependency {
groupId "${p.group}"
artifactId "${p.ext.artifactId}"
version "${p.version}"
}
}
}
}
}
}
pom.packaging = 'pom'
}
}
}

if (project.ext.isSigning()) {
signing {
sign publishing.publications
dependencies {
constraints {
projectsWithFlags('java', 'publish').toList().sort { a, b ->
def groupComparison = "${a.group}".compareTo("${b.group}")
if (groupComparison != 0) {
return groupComparison
}
return "${a.ext.artifactId}".compareTo("${b.ext.artifactId}")
}.each { p ->
api "${p.group}:${p.ext.artifactId}:${p.version}"
}
}
}

tasks.assemble.dependsOn {
tasks.generatePomFileForBomPublication
}
}

private static Node findChildNode(Node parent, String childName) {
return parent.children().find {
def name = it.name()
if (name instanceof QName) {
name = name.localPart
} else {
name = name.toString()
}

return name == childName
} as Node
}
100 changes: 76 additions & 24 deletions lib/common-dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ buildscript {
dependencies {
// These should be the only dependencies that need hard-coded versions.
classpath 'com.github.ben-manes:gradle-versions-plugin:0.27.0'
classpath 'io.spring.gradle:dependency-management-plugin:1.0.8.RELEASE'
classpath 'org.yaml:snakeyaml:1.25'
}
}
Expand All @@ -25,45 +24,98 @@ rootProject.ext {
}
}

def managedDependencyVersions = [:]
def managedDependencyExclusions = [:].withDefault { [] }
rootProject.ext.dependenciesYaml.forEach { String key, value ->
if (key == 'boms') {
value.each {
def parsed = it.split(':')
managedDependencyVersions["${parsed[0]}:${parsed[1]}"] = parsed[2]
}
} else {
def groupId = key
def artifact = value as Map
artifact.forEach { String artifactId, Map props ->
if (props.containsKey('version')) {
managedDependencyVersions["${groupId}:${artifactId}"] = "${props['version']}"
}
if (props.containsKey('exclusions')) {
props['exclusions'].each { String spec ->
def parsed = spec.split(':')
managedDependencyExclusions["${groupId}:${artifactId}"].add([
group: parsed[0],
module: parsed[1],
])
}
}
}
}
}

allprojects {
apply plugin: 'io.spring.dependency-management'
ext {
managedVersions = managedDependencyVersions
}
}

dependencyManagement {
generatedPomCustomization {
enabled = false
}
def dependencyManagementProjects = projectsWithFlags('dependencyManagement')
assert !dependencyManagementProjects.isEmpty() // Guaranteed by settings-flags.gradle
def dependencyManagementProject = dependencyManagementProjects[0]

imports {
if (rootProject.ext.dependenciesYaml.containsKey('boms')) {
dependenciesYaml['boms'].each {
mavenBom "${it}"
}
configure(dependencyManagementProject) {
apply plugin: 'java-platform'

javaPlatform {
allowDependencies()
}

dependencies {
if (rootProject.ext.dependenciesYaml.containsKey('boms')) {
dependenciesYaml['boms'].each {
api platform("${it}")
}
}

dependencies {
constraints {
rootProject.ext.dependenciesYaml.forEach { String key, value ->
if (key != 'boms') {
def groupId = key
def artifact = value as Map
artifact.forEach { String artifactId, Map props ->
if (props.containsKey('version')) {
dependency("${groupId}:${artifactId}:${props['version']}") {
if (props.containsKey('exclusions')) {
props['exclusions'].each { String spec ->
exclude spec
}
}
}
api "${groupId}:${artifactId}:${props['version']}"
}
}
}
}
}
}
}

ext {
managedVersions = dependencyManagement.managedVersions
configure(projectsWithFlags('java')) {
evaluationDependsOn(dependencyManagementProject.path)

configurations.configureEach { configuration ->
configuration.dependencies.whenObjectAdded { dep ->
if (dep instanceof org.gradle.api.artifacts.ModuleDependency) {
managedDependencyExclusions["${dep.group}:${dep.name}"].each {
exclude it
}
}
}
}

// We need to use afterEvaluate because there is no way to guarantee configuration.canBeResolved and
// canBeConsumed are set otherwise.
afterEvaluate {
dependencies {
configurations.configureEach { configuration ->
// Add to resolvable configurations
if (configuration.canBeResolved && !configuration.canBeConsumed) {
add(configuration.name, platform(dependencyManagementProject))
}
}
}
}
}

Expand Down Expand Up @@ -116,7 +168,7 @@ rootProject.ext {
def groupId = entry.key
entry.value.forEach { String artifactId, Map props ->
if (props.containsKey('javadocs')) {
def version = String.valueOf(managedVersions["${groupId}:${artifactId}"])
def version = String.valueOf(managedDependencyVersions["${groupId}:${artifactId}"])
props['javadocs'].each { url ->
list.add([
groupId: groupId,
Expand Down Expand Up @@ -144,12 +196,12 @@ configure(rootProject) {
task managedVersions(
group: 'Build',
description: 'Generates the file that contains dependency versions.') {
inputs.properties dependencyManagement.managedVersions
inputs.properties managedDependencyVersions
doLast {
def f = file("${project.buildDir}/managed_versions.yml")
f.parentFile.mkdir()
f.withWriter('UTF-8') {
new Yaml().dump(dependencyManagement.managedVersions, it)
new Yaml().dump(managedDependencyVersions, it)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/java-alpn.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ configure(projectsWithFlags('java')) {
configurations {
alpnAgent {
visible = false
canBeConsumed = false
}
}

Expand Down
Loading