Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
build: refactor Gradle build (#1539)
Browse files Browse the repository at this point in the history
* build: refactor Gradle build
* build: simplify
* Fix local install
  • Loading branch information
chanseokoh authored Oct 20, 2021
1 parent 725414f commit 9f96d14
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 149 deletions.
2 changes: 1 addition & 1 deletion .kokoro/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ cd ${scriptDir}/..
java -version
echo $JOB_TYPE

./gradlew -Pskip.signing build publishToMavenLocal
./gradlew build publishToMavenLocal
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ To build GAX:
To install GAX into the local maven repository:

```sh
./gradlew publishToMavenLocal -Pskip.signing
./gradlew publishToMavenLocal
```

### The small print
Expand Down
176 changes: 88 additions & 88 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,42 @@ plugins {
// TODO: Populate this from dependencies.properties version property (for proper Gradle-Bazel sync)
project.version = "2.6.1-SNAPSHOT" // {x-version-update:gax:current}

ext {
// Project names not used for release
nonReleaseProjects = ['benchmark']
// Project names not using the default publication configuration
noDefaultPublications = ['benchmark', 'gax-bom']
libraryVendor = 'Google'
// Project names not used for release
def nonReleaseProjects = ['benchmark']
// Project names not using the default publication configuration
def noDefaultPublications = ['benchmark', 'gax-bom']
def nonJavaProjects = ['gax-bom']

ext.libraryVendor = 'Google'

allprojects {
group = 'com.google.api'
}

if (project.hasProperty('ossrhUsername') && project.hasProperty('ossrhPassword') &&
!nonReleaseProjects.contains(project.name)) {
nexusPublishing {
packageGroup = 'com.google.api'
repositories {
sonatype { //or custom repository name
nexusUrl.set(uri('https://google.oss.sonatype.org/service/local/'))
snapshotRepositoryUrl.set(uri('https://google.oss.sonatype.org/content/repositories/snapshots/'))
username = ossrhUsername
password = ossrhPassword
}
nexusPublishing {
packageGroup = 'com.google.api'
repositories {
sonatype {
nexusUrl = uri('https://google.oss.sonatype.org/service/local/')
snapshotRepositoryUrl = uri('https://google.oss.sonatype.org/content/repositories/snapshots/')
username = findProperty('ossrhUsername')
password = findProperty('ossrhPassword')
}
}
}

subprojects {
def javaProjects = subprojects.findAll { !nonJavaProjects.contains(it.name) }
configure(javaProjects) {
apply plugin: 'java-library'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'jacoco'
apply plugin: 'maven-publish'
apply plugin: 'signing'

apply plugin: 'com.dorongold.task-tree'
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'com.github.sherter.google-java-format'
apply plugin: 'net.ltgt.apt'

group = 'com.google.api'

sourceCompatibility = 1.8
targetCompatibility = 1.8

Expand Down Expand Up @@ -281,91 +279,93 @@ subprojects {
file('.factorypath')
}
}
}

// Publishing
// ----------
def normalReleasableProjects = subprojects.findAll {
!nonReleaseProjects.contains(it.name) && !noDefaultPublications.contains(it.name)
}
configure(normalReleasableProjects) {
apply plugin: 'maven-publish'
apply plugin: 'signing'

afterEvaluate {
if (!noDefaultPublications.contains(project.name)) {
publishing {
publications {
mavenJava(MavenPublication) {
version = project.version

from components.java

artifact javadocJar
artifact sourcesJar
artifact testlibJar

pom {
name = 'GAX (Google Api eXtensions) for Java'
packaging = 'jar'
artifactId = project.name
description = 'Google Api eXtensions for Java'
url = 'https://github.com/googleapis/gax-java'
publishing {
publications {
mavenJava(MavenPublication) {
version = project.version

scm {
url = 'https://github.com/googleapis/gax-java'
connection = 'scm:git:https://github.com/googleapis/gax-java.git'
}
from components.java

artifact javadocJar
artifact sourcesJar
artifact testlibJar

pom {
name = 'GAX (Google Api eXtensions) for Java'
packaging = 'jar'
artifactId = project.name
description = 'Google Api eXtensions for Java'
url = 'https://github.com/googleapis/gax-java'

scm {
url = 'https://github.com/googleapis/gax-java'
connection = 'scm:git:https://github.com/googleapis/gax-java.git'
}

licenses {
license {
name = 'BSD'
url = 'https://github.com/googleapis/gax-java/blob/master/LICENSE'
}
licenses {
license {
name = 'BSD'
url = 'https://github.com/googleapis/gax-java/blob/master/LICENSE'
}
}

developers {
developer {
id = 'GoogleAPIs'
name = 'GoogleAPIs'
email = 'googleapis@googlegroups.com'
url = 'https://github.com/googleapis/gax-java'
organization = 'Google LLC'
organizationUrl = 'https://www.google.com'
}
developers {
developer {
id = 'GoogleAPIs'
name = 'GoogleAPIs'
email = 'googleapis@googlegroups.com'
url = 'https://github.com/googleapis/gax-java'
organization = 'Google LLC'
organizationUrl = 'https://www.google.com'
}
}
}
}
repositories {
maven {
url 'https://google.oss.sonatype.org/service/local/staging/deploy/maven2/'
credentials {
username = project.findProperty('ossrhUsername')
password = project.findProperty('ossrhPassword')
}
}
repositories {
maven {
url 'https://google.oss.sonatype.org/service/local/staging/deploy/maven2/'
credentials {
username = findProperty('ossrhUsername')
password = findProperty('ossrhPassword')
}
}
}
}

task checkJavaLinkage(type: JavaExec) {
// Example invocation:
// ./gradlew gax-httpjson:checkJavaLinkage
dependsOn project.getTasksByName('publishMavenJavaPublicationToMavenLocal', true)
classpath = configurations.linkageChecker
main = 'com.google.cloud.tools.opensource.classpath.LinkageCheckerMain'

def arguments = ['-a', "com.google.api:${project.name}:${project.version}"]
if (project.name == 'gax-grpc') {
// The exclusion file can be regenerated by '-o' option. See its Wiki for details:
// https://github.com/GoogleCloudPlatform/cloud-opensource-java/wiki/LinkageCheckerMain#exclusion-files
arguments += ['--exclusion-file', 'linkage-checker-exclusion.xml']
}
args arguments
task checkJavaLinkage(type: JavaExec) {
// Example invocation:
// ./gradlew gax-httpjson:checkJavaLinkage
dependsOn project.getTasksByName('publishMavenJavaPublicationToMavenLocal', true)
classpath = configurations.linkageChecker
main = 'com.google.cloud.tools.opensource.classpath.LinkageCheckerMain'

def arguments = ['-a', "com.google.api:${project.name}:${project.version}"]
if (project.name == 'gax-grpc') {
// The exclusion file can be regenerated by '-o' option. See its Wiki for details:
// https://github.com/GoogleCloudPlatform/cloud-opensource-java/wiki/LinkageCheckerMain#exclusion-files
arguments += ['--exclusion-file', 'linkage-checker-exclusion.xml']
}
args arguments
}
}

signing {
if (!project.hasProperty('skip.signing') && !noDefaultPublications.contains(project.name)) {
if (project.hasProperty('signing.gnupg.executable')) {
useGpgCmd()
}
sign publishing.publications.mavenJava
}
signing {
required { gradle.taskGraph.hasTask ":${name}:publishToSonatype" }
if (project.hasProperty('signing.gnupg.executable')) {
useGpgCmd()
}
sign publishing.publications
}
}

Expand Down
98 changes: 39 additions & 59 deletions gax-bom/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
plugins {
id 'maven-publish'
id 'signing'
}

archivesBaseName = 'gax-bom'

project.version = "2.6.1-SNAPSHOT" // {x-version-update:gax-bom:current}

ext {
mavenJavaDir = "$buildDir/publications/mavenJava"
mavenJavaBomOutputFile = file(mavenJavaDir + "/pom-default.xml")
mavenJavaBomAscOutputFile = file(mavenJavaDir + "/pom-default.xml.asc")
}
def mavenJavaDir = "$buildDir/publications/mavenJava"
def mavenJavaBomOutputFile = file(mavenJavaDir + '/pom-default.xml')
def mavenJavaBomAscOutputFile = file(mavenJavaDir + '/pom-default.xml.asc')

// Copy our pom.xml to the location where a generated POM would go
task copyPom() {
Expand All @@ -20,8 +23,6 @@ task copyPom() {
}
}

assemble.dependsOn copyPom

// We want to use our own pom.xml instead of the generated one, so we disable
// the pom.xml generation and have the publish tasks depend on `copyPom` instead.
tasks.whenTaskAdded { task ->
Expand All @@ -36,66 +37,45 @@ tasks.whenTaskAdded { task ->
}
}

jar.enabled = false

// Remove the default jar archive which is added by the 'java' plugin.
// We could avoid this by not applying the 'java' plugin to all submodules of
// gax, but that would create a little bit of a mess, so we hack around it here.
configurations.archives.artifacts.with { archives ->
def artifacts = []
archives.each {
if (it.file =~ 'jar') {
// We can't just call `archives.remove(it)` here because it triggers
// a `ConcurrentModificationException`, so we add matching artifacts
// to another list, then remove those elements outside of this iteration.
artifacts.add(it)
// We can't use the `publishing` section from the main build.gradle because
// we don't want all the Java artifacts, and we want to use our own pom.xml
// instead of the generated one.
publishing {
publications {
mavenJava(MavenPublication) {
version = project.version
artifact mavenJavaBomOutputFile
}
}
artifacts.each {
archives.remove(it)
repositories {
maven {
url 'https://google.oss.sonatype.org/service/local/staging/deploy/maven2/'
credentials {
username = findProperty('ossrhUsername')
password = findProperty('ossrhPassword')
}
}
}
}

artifacts {
archives(mavenJavaBomOutputFile) {
builtBy copyPom
signing {
required { gradle.taskGraph.hasTask ":${name}:publishToSonatype" }
if (project.hasProperty('signing.gnupg.executable')) {
useGpgCmd()
}
sign publishing.publications
}

afterEvaluate {
// We can't use the `publishing` section from the main build.gradle because
// we don't want all the Java artifacts, and we want to use our own pom.xml
// instead of the generated one.
publishing {
publications {
mavenJava(MavenPublication) {
version = project.version
artifact mavenJavaBomOutputFile
}
}
repositories {
maven {
url 'https://google.oss.sonatype.org/service/local/staging/deploy/maven2/'
credentials {
username = project.findProperty('ossrhUsername')
password = project.findProperty('ossrhPassword')
}
}
}
}

signing {
if (!project.hasProperty('skip.signing')) {
if (project.hasProperty('signing.gnupg.executable')) {
useGpgCmd()
}
sign publishing.publications.mavenJava

publishing.publications.mavenJava(MavenPublication) {
artifact(mavenJavaBomAscOutputFile) {
extension 'pom.asc'
}
}
// Unlike other regular subprojects, pom.asc (signature file) generated by
// signing is not automatically picked up. It may be because gax-bom uses a
// pre-written pom.xml instead of using an auto-generated one.
// https://discuss.gradle.org/t/how-to-publish-artifacts-signatures-asc-files-using-maven-publish-plugin/7422
task addPomAscFileToPublication {
onlyIf { mavenJavaBomAscOutputFile.exists() }
doLast {
publishing.publications.mavenJava(MavenPublication) {
artifact(mavenJavaBomAscOutputFile) { extension 'pom.asc' }
}
}
}
signMavenJavaPublication.finalizedBy addPomAscFileToPublication

0 comments on commit 9f96d14

Please sign in to comment.