-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathcom.google.api-ads.java-conventions.gradle
More file actions
447 lines (395 loc) · 14.3 KB
/
com.google.api-ads.java-conventions.gradle
File metadata and controls
447 lines (395 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* These are shared settings common to all Google Ads Java library subprojects.
*/
import org.gradle.api.DefaultTask
import groovy.io.FileType
import groovy.io.FileVisitResult
import java.util.regex.Matcher
plugins {
id 'java-library'
id 'maven-publish'
id 'java-test-fixtures'
id 'signing'
id 'org.jreleaser'
}
jreleaser {
project {
gitRootSearch = true // Allow JReleaser to search upwards for .git
}
signing {
active = 'ALWAYS'
armored = true
}
deploy {
maven {
mavenCentral {
sonatype {
/** Warning: In your JReleaser config file set
* JRELEASER_MAVENCENTRAL_STAGE to 'UPLOAD' in order to
* prevent the deployment from automatically publishing
* without any UI intervention. */
active = 'ALWAYS'
url = 'https://central.sonatype.com/api/v1/publisher'
stagingRepository(layout.buildDirectory.dir("staging-deploy").get().toString())
stage = 'UPLOAD'
}
}
release {
github {
skipRelease = true
skipTag = true
}
}
}
}
}
// The order of the repositories here is important. If mavenLocal() is first,
// then when we attempt to resolve artifacts like sources for the third_party
// directory, gradle will not even attempt to look for them in mavenCentral.
repositories {
mavenCentral()
mavenLocal()
}
java.sourceCompatibility = JavaVersion.VERSION_1_8
java.targetCompatibility = JavaVersion.VERSION_1_8
java {
withSourcesJar()
withJavadocJar()
}
jar {
manifest {
attributes(
'Implementation-Title': 'Google Ads API - client library for Java',
'Implementation-Version': archiveVersion)
}
// Adds license information to the META-INF directory in the jar.
into('META-INF') {
from("${project.rootProject.rootDir}") {
include "LICENSE"
include "THIRD-PARTY-LICENSE.txt"
}
}
}
javadoc {
options.addStringOption('Xdoclint:none', '-quiet')
// Sets the input (source) file encoding to UTF-8. The javadoc tool will also
// use this for the output (HTML) file encoding. Without this option, the
// javadoc tool will use the default encoding for the platform.
options.encoding = 'UTF-8'
}
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
publishing {
publications {
maven(MavenPublication) {
from(components.java)
// Ensures that the version of each dependency is explicitly included in the
// published POM files. The Cloud Client Libraries (https://cloud.google.com/java/docs/bom)
// follow a similar approach.
//
// See also:
// https://docs.gradle.org/7.6/dsl/org.gradle.api.publish.maven.MavenPublication.html#org.gradle.api.publish.maven.MavenPublication:versionMapping(org.gradle.api.Action)
versionMapping {
usage('java-api') {
fromResolutionOf('runtimeClasspath')
}
usage('java-runtime') {
fromResolutionResult()
}
}
repositories {
maven {
name = 'sonatype'
url = uri(layout.buildDirectory.dir("staging-deploy"))
}
}
}
}
}
// Deactivates publishing of test fixtures. Test fixtures are an implementation/testing
// detail that's not needed by users of the published library.
// https://docs.gradle.org/7.6.2/userguide/java_testing.html#publishing_test_fixtures
components.java.withVariantsFromConfiguration(configurations.testFixturesApiElements) { skip() }
components.java.withVariantsFromConfiguration(configurations.testFixturesRuntimeElements) { skip() }
ext.configurePom = { publication, nameToSet, descriptionToSet ->
publication.pom {
name = nameToSet
description = descriptionToSet
packaging 'jar'
url = 'https://github.com/googleads/google-ads-java'
licenses {
license {
name = 'The Apache License, Version 2.0'
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
scm {
connection = 'scm:git:https://github.com/googleads/google-ads-java'
developerConnection = 'scm:git:https://github.com/googleads/google-ads-java'
url = 'https://github.com/googleads/google-ads-java'
}
developers {
developer {
id = 'josh'
name = 'Josh Radcliff'
email = 'jradcliff@users.noreply.github.com'
organization = 'Google'
organizationUrl = 'https://www.google.com'
}
developer {
id = 'nick'
name = 'Nick Birnie'
email = 'nwbirnie@users.noreply.github.com'
organization = 'Google'
organizationUrl = 'https://www.google.com'
}
developer {
id = 'devin'
name = 'Devin Chasanoff'
email = 'devchas@users.noreply.github.com'
organization = 'Google'
organizationUrl = 'https://www.google.com'
}
developer {
id = 'sarah'
name = 'Sarah Pollack'
email = 'sarahbot@users.noreply.github.com'
organization = 'Google'
organizationUrl = 'https://www.google.com'
}
}
}
}
// Disables signing tasks except on sonatype deploy. Avoids failing build for
// users without GPG configured.
if (project.properties.containsKey("release")) {
signing {
sign publishing.publications
}
}
dependencies {
api 'com.google.api:gax'
api 'com.google.api:gax-grpc'
api 'com.google.protobuf:protobuf-java'
implementation 'com.google.protobuf:protobuf-java-util'
api 'io.grpc:grpc-stub'
api 'io.grpc:grpc-protobuf'
api 'com.google.auth:google-auth-library-oauth2-http'
api 'com.google.auth:google-auth-library-credentials'
api platform('com.google.cloud:google-cloud-shared-dependencies:3.56.0')
implementation 'com.google.guava:guava'
implementation 'com.google.auto.service:auto-service:1.0.1'
implementation 'javax.annotation:javax.annotation-api'
annotationProcessor 'com.google.auto.service:auto-service:1.0.1'
testImplementation 'junit:junit:4.13.1'
}
// Disables a task unless we're publishing something and we're not doing
// a fast build.
def disableUnlessPublishing(taskToDisable) {
// Disables the task.
taskToDisable.enabled = false
// Leaves the task disabled if the 'fastbuild' property was set.
if (project.properties.containsKey('fastbuild')) {
logger.warn('Skipping {} because "fastbuild" property is set', taskToDisable)
return
}
// Once the task graph is ready, checks if it contains publishing tasks. If it
// does, rolls back the change above and re-enables `taskToDisable`.
gradle.taskGraph.whenReady {
gradle.taskGraph.allTasks
.findAll { it.group == "publishing" }
.any { taskToDisable.enabled = true }
}
}
disableUnlessPublishing(tasks.javadocJar)
disableUnlessPublishing(tasks.javadoc)
disableUnlessPublishing(tasks.sourcesJar)
task listFlattenedRuntimeDependencies() {
description = "Lists resolved runtime dependencies"
doLast {
println "========================================="
println "${project.name} Runtime dependencies"
println "========================================="
def selectedDeps = project.configurations.runtimeClasspath.incoming.resolutionResult.allDependencies.collect { dep ->
"${dep.selected}"
}
selectedDeps.unique().sort().each { println it}
}
}
class ExampleRunnerTask extends JavaExec {
// The base package for all examples.
@Internal
private String basePackage = 'com.google.ads.googleads.examples.'
// Hints for failed executions.
private final String exampleInvocation = 'For example, ' +
'\033[0;35m--example="basicoperations.GetCampaigns"\033[0m.\n' +
'Run \033[0;35m./gradlew -q listExamples\033[0m to list all examples, or ' +
'\033[0;35m./gradlew -q listExamples --subdirectory="basicoperations"\033[0m ' +
'to list examples in a specific subdirectory.\n\n'
// The package.ClassName and any arguments required to launch an example,
// for example "basicoperations.GetCampaigns --customerId 1234567890".
@Optional
@Input
private String exampleArguments
ExampleRunnerTask() {
group = 'Execution'
description = 'Run a Google Ads API example.'
errorOutput = System.err
}
String getBasePackage() {
return this.basePackage
}
void setBasePackage(String basePackage) {
this.basePackage = basePackage
}
String getExampleArguments() {
return this.exampleArguments
}
@Option(option = 'example', description = 'Sets the example to launch and' +
' any arguments. Required for execution. For example, ' +
'"basicoperations.GetCampaigns --customerId 1234567890"')
void setExampleArguments(String exampleArguments) {
this.exampleArguments = exampleArguments.trim()
int firstSpaceIndex = this.exampleArguments.indexOf(' ')
// No additional arguments were passed, just the example name.
if (firstSpaceIndex == -1) {
main = this.basePackage + this.exampleArguments
}
// Otherwise, separate the input and set the arguments to pass to the
// main class.
else {
main = this.basePackage + this.exampleArguments[0..firstSpaceIndex - 1]
argsString(exampleArguments[(firstSpaceIndex + 1)..-1])
}
}
@TaskAction
@Override
void exec() {
if (!(exampleArguments?.trim())) {
throw new GradleException('\033[0;31mMissing example!\033[0m ' +
'Please rerun with one provided. ' + exampleInvocation)
}
logQuietMessage('Running example: ' + main + ', args: ' +
args.toString())
try {
super.exec()
}
catch (Exception e) {
logQuietMessage('\n\033[0;31mrunExample exception!\033[0m Did ' +
'you provide a valid example identifier? ' + exampleInvocation +
e.message)
throw e
}
}
// Prints a message to the console, even in quiet mode.
private void logQuietMessage(String message) {
logger.quiet(message)
}
}
class ListExamplesTask extends DefaultTask {
// Regex for extracting descriptions. Isolates the Javadoc comment immediately
// preceeding the class definition.
private static final String COMMENT_REGEX =
'(?s)(/\\*\\*(?:(?!/\\*\\*).)*?\\*/)(?:(?:(?!\\*/).)*?)(?=public class )'
// Regex for comment markers.
private static final String COMMENT_MARKERS = '(/\\*\\*\\s+|\\*\\s|\\*/)'
// The base directory containing all examples.
private static final String MAIN_EXAMPLES_BASE_DIRECTORY =
'src/main/java/com/google/ads/googleads/examples/'
// A list of subdirectories to exclude from the printout.
@Input
List<String> directoriesToExclude = ['utils']
// An optional subdirectory; if non-null, only examples in the specified
// subdirectory will be printed.
@Optional
@Input
private String subdirectory = null
// An optional case-sensitive search string; if non-null, only examples whose
// name or description contains the specified term will be printed.
@Optional
@Input
private String searchTerm = null
ListExamplesTask() {
description = 'List all available code examples with package name ' +
'and description.'
}
String getSubdirectory() {
return this.subdirectory
}
List<String> getDirectoriesToExclude() {
return this.directoriesToExclude
}
String getSearchTerm() {
return this.searchTerm
}
@Option(option = 'subdirectory', description = 'Sets a subdirectory of ' +
'google-ads-examples, for example "basicoperations". Only examples in this ' +
'subdirectory will be printed. If not specified, all the examples ' +
'will be printed.')
void setSubdirectory(String subdirectory) {
this.subdirectory = subdirectory.trim()
}
@Option(option = 'searchTerm', description = 'Sets a case-sensitive search term, ' +
'for example "conversion". If set, only examples whose name or description ' +
'contains the specified term will be printed.')
void setSearchTerm(String searchTerm) {
this.searchTerm = searchTerm.trim()
}
// Traverse a given directory, printing the information for each example.
int traverseDirectory(String dir, int offset) {
int count = 0
// Determine the number of leading characters in the directory path. This
// value is different depending on which subproject we are traversing.
int baseLength = (subdirectory ?
project.file(dir).absolutePath.lastIndexOf('/') :
project.file(dir).absolutePath.length()) + 1
project.file(dir).traverse(
type : FileType.FILES,
preDir : { if (it.name in directoriesToExclude)
return FileVisitResult.SKIP_SUBTREE },
excludeNameFilter : { it in directoriesToExclude },
nameFilter : { it.endsWith('.java') },
sort : { a, b -> a.name <=> b.name }
) {
// Strip out the base directory, truncate the .java file extension,
// and replace the / with . to indicate package name.
String className = it.path.substring(baseLength,
it.path.length()-5).replaceAll('/','.')
// Fetch the example description.
Matcher matcher = it.text =~ COMMENT_REGEX
String description =
matcher ? matcher[0][0] : "*description missing*"
if (!searchTerm || className.contains(searchTerm) ||
description.contains(searchTerm)) {
// Print the count, package.class name, and description (without
// comment markers and trailing newlines).
println '' + (++count + offset) + ': ' + className + ' - ' +
description.replaceAll(COMMENT_MARKERS,'').strip()
}
}
return count
}
@TaskAction
void exec() {
String mainExamplesDirectory = subdirectory ?
MAIN_EXAMPLES_BASE_DIRECTORY + subdirectory + '/' : MAIN_EXAMPLES_BASE_DIRECTORY
int count = 0
count += traverseDirectory(mainExamplesDirectory, count)
println "\nTotal examples: $count"
}
}