Skip to content

Commit 0170a93

Browse files
authored
Fixes to enable publishing the library with gradle (#496)
1 parent d13a199 commit 0170a93

File tree

4 files changed

+99
-89
lines changed

4 files changed

+99
-89
lines changed

buildSrc/src/main/groovy/com.google.api-ads.java-conventions.gradle

+80
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import java.util.regex.Matcher
2424
plugins {
2525
id 'java-library'
2626
id 'maven-publish'
27+
id 'signing'
2728
}
2829

2930
// The order of the repositories here is important. If mavenLocal() is first,
@@ -48,12 +49,91 @@ javadoc {
4849
options.addStringOption('Xdoclint:none', '-quiet')
4950
}
5051

52+
final def sonatypeCredentialsConfig = file(System.properties['user.home'] + '/.sonatype-creds')
53+
5154
publishing {
5255
publications {
5356
maven(MavenPublication) {
5457
from(components.java)
5558
}
5659
}
60+
repositories {
61+
maven {
62+
url 'https://oss.sonatype.org/service/local/staging/deploy/maven2'
63+
name = "sonatype"
64+
65+
if (sonatypeCredentialsConfig.exists()) {
66+
def props = new Properties()
67+
sonatypeCredentialsConfig.withInputStream { props.load(it) }
68+
credentials {
69+
username props['username']
70+
password props['password']
71+
}
72+
}
73+
}
74+
}
75+
}
76+
77+
// Provide a helpful error message for missing sonatype credentials.
78+
tasks.findAll {
79+
if (it.name.matches("publish.+ToSonatypeRepository")) {
80+
it.doFirst {
81+
if (!sonatypeCredentialsConfig.exists()) {
82+
throw new GradleException("Unable to load Sonatype credentials from ${sonatypeCredentialsConfig}.")
83+
}
84+
}
85+
}
86+
}
87+
88+
ext.configurePom = { publication, nameToSet, descriptionToSet ->
89+
publication.pom {
90+
name = nameToSet
91+
description = descriptionToSet
92+
packaging 'jar'
93+
url = 'https://github.com/googleads/google-ads-java'
94+
licenses {
95+
license {
96+
name = 'The Apache License, Version 2.0'
97+
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
98+
}
99+
}
100+
scm {
101+
connection = 'scm:git:https://github.com/googleads/google-ads-java'
102+
developerConnection = 'scm:git:https://github.com/googleads/google-ads-java'
103+
url = 'https://github.com/googleads/google-ads-java'
104+
}
105+
developers {
106+
developer {
107+
id = 'josh'
108+
name = 'Josh Radcliff'
109+
110+
organization = 'Google'
111+
organizationUrl = 'https://www.google.com'
112+
}
113+
developer {
114+
id = 'nick'
115+
name = 'Nick Birnie'
116+
117+
organization = 'Google'
118+
organizationUrl = 'https://www.google.com'
119+
}
120+
developer {
121+
id = 'devin'
122+
name = 'Devin Chasanoff'
123+
124+
organization = 'Google'
125+
organizationUrl = 'https://www.google.com'
126+
}
127+
}
128+
}
129+
}
130+
131+
// Disables signing tasks except on sonatype deploy. Avoids failing build for
132+
// users without GPG configured.
133+
if (project.properties.containsKey("release")) {
134+
signing {
135+
sign publishing.publications
136+
}
57137
}
58138

59139
tasks.withType(JavaCompile) {

google-ads-annotation-processing/build.gradle

+9
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,12 @@ dependencies {
2929

3030
description = 'Google Ads API client library for Java annotation processor'
3131

32+
publishing {
33+
publications {
34+
maven(MavenPublication) { publication ->
35+
configurePom(publication,
36+
"Google Ads API client library for Java - main library",
37+
"Annotations + processor for code generation")
38+
}
39+
}
40+
}

google-ads/build.gradle

+5-87
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import com.google.common.collect.Sets
2222

2323
plugins {
2424
id 'com.google.api-ads.java-conventions'
25-
id 'signing'
2625
id 'com.google.protobuf' version '0.8.15'
2726
id "com.github.hierynomus.license-report" version "0.15.0"
2827
id 'com.github.johnrengelman.shadow' version '6.1.0'
@@ -311,52 +310,13 @@ task testShadowJar {
311310
}
312311
}
313312

314-
def configurePom(publication, nameToSet, descriptionToSet) {
315-
publication.pom {
316-
name = nameToSet
317-
description = descriptionToSet
318-
url = 'https://github.com/googleads/google-ads-java'
319-
licenses {
320-
license {
321-
name = 'The Apache License, Version 2.0'
322-
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
323-
}
324-
}
325-
scm {
326-
connection = 'scm:git:https://github.com/googleads/google-ads-java'
327-
developerConnection = 'scm:git:https://github.com/googleads/google-ads-java'
328-
url = 'https://github.com/googleads/google-ads-java'
329-
}
330-
developers {
331-
developer {
332-
id = 'josh'
333-
name = 'Josh Radcliff'
334-
335-
organization = 'Google'
336-
organizationUrl = 'https://www.google.com'
337-
}
338-
developer {
339-
id = 'nick'
340-
name = 'Nick Birnie'
341-
342-
organization = 'Google'
343-
organizationUrl = 'https://www.google.com'
344-
}
345-
developer {
346-
id = 'devin'
347-
name = 'Devin Chasanoff'
348-
349-
organization = 'Google'
350-
organizationUrl = 'https://www.google.com'
351-
}
352-
}
353-
}
354-
}
355-
356-
final def sonatypeCredentialsConfig = file(System.properties['user.home'] + '/.sonatype-creds')
357-
358313
publishing {
359314
publications {
315+
maven(MavenPublication) { publication ->
316+
configurePom(publication,
317+
"Google Ads API client library for Java - main library",
318+
"Main library for the Google Ads API client library for Java")
319+
}
360320
shadow(MavenPublication) { publication ->
361321
project.shadow.component(publication)
362322
// Publishes the javadoc + sources with the shadowjar.
@@ -375,46 +335,4 @@ publishing {
375335
"Provides a jar with all dependencies")
376336
}
377337
}
378-
379-
repositories {
380-
maven {
381-
url 'https://oss.sonatype.org/service/local/staging/deploy/maven2'
382-
name = "sonatype"
383-
384-
if (sonatypeCredentialsConfig.exists()) {
385-
def props = new Properties()
386-
sonatypeCredentialsConfig.withInputStream { props.load(it) }
387-
credentials {
388-
username props['username']
389-
password props['password']
390-
}
391-
}
392-
}
393-
}
394-
}
395-
396-
signing {
397-
sign publishing.publications.shadow
398-
}
399-
400-
// Disables signing tasks except on sonatype deploy. Avoids failing build for
401-
// users without GPG configured.
402-
gradle.taskGraph.whenReady {
403-
if (gradle.taskGraph.allTasks
404-
.findAll { it.name.matches("publish.+ToSonatypeRepository") }
405-
.isEmpty()) {
406-
tasks.findByName("signShadowPublication").enabled = false
407-
}
408-
}
409-
410-
// Provide a helpful error message for missing sonatype credentials.
411-
tasks.findAll {
412-
if (it.name.matches("publish.+ToSonatypeRepository")) {
413-
it.doFirst {
414-
if (!sonatypeCredentialsConfig.exists()) {
415-
throw new GradleException("Unable to load Sonatype credentials from ${sonatypeCredentialsConfig}.")
416-
}
417-
}
418-
}
419338
}
420-

settings.gradle

+5-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ rootProject.name = 'google-ads-parent'
2121

2222
include ':google-ads-annotation-processing'
2323
include ':google-ads'
24-
include ':google-ads-examples'
25-
include ':google-ads-migration-examples'
2624

25+
// Enables the examples project only if we're not deploying to Sonatype.
26+
if (!startParameter.projectProperties.containsKey("release")) {
27+
include ':google-ads-examples'
28+
include ':google-ads-migration-examples'
29+
}

0 commit comments

Comments
 (0)