xref: /aosp_15_r20/external/opencensus-java/build.gradle (revision a24ffb47c3166327784aa05b149974e82e8f71b8)
1buildscript {
2    repositories {
3        mavenCentral()
4        mavenLocal()
5        maven {
6            url "https://plugins.gradle.org/m2/"
7        }
8    }
9    dependencies {
10        classpath 'ru.vyarus:gradle-animalsniffer-plugin:1.4.6'
11        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.5.RELEASE")
12        classpath 'net.ltgt.gradle:gradle-errorprone-plugin:0.0.16'
13        classpath "net.ltgt.gradle:gradle-apt-plugin:0.18"
14        classpath 'com.github.ben-manes:gradle-versions-plugin:0.20.0'
15        classpath "gradle.plugin.com.github.sherter.google-java-format:google-java-format-gradle-plugin:0.8"
16        classpath "me.champeau.gradle:jmh-gradle-plugin:0.4.8"
17        classpath "gradle.plugin.io.morethan.jmhreport:gradle-jmh-report:0.9.0"
18    }
19}
20
21// Display the version report using: ./gradlew dependencyUpdates
22// Also see https://github.com/ben-manes/gradle-versions-plugin.
23apply plugin: 'com.github.ben-manes.versions'
24
25subprojects {
26    // OC-Agent exporter depends on grpc-core which depends on errorprone annotations, and it
27    // doesn't work with checker framework.
28    def projectsDependOnGrpcCore = ["opencensus-exporter-metrics-ocagent", "opencensus-exporter-trace-ocagent"]
29
30    // Don't use the Checker Framework by default, since it interferes with Error Prone.
31    def useCheckerFramework = rootProject.hasProperty('checkerFramework') && !(project.name in projectsDependOnGrpcCore)
32    def useErrorProne = !useCheckerFramework
33
34    apply plugin: "checkstyle"
35    apply plugin: 'maven'
36    apply plugin: 'idea'
37    apply plugin: 'eclipse'
38    apply plugin: 'java'
39    apply plugin: "signing"
40    apply plugin: "jacoco"
41    // The plugin only has an effect if a signature is specified
42    apply plugin: 'ru.vyarus.animalsniffer'
43    apply plugin: 'findbugs'
44    apply plugin: 'net.ltgt.apt'
45    apply plugin: 'net.ltgt.apt-idea'
46    apply plugin: "me.champeau.gradle.jmh"
47    apply plugin: "io.morethan.jmhreport"
48    apply plugin: 'com.github.sherter.google-java-format'
49    if (useErrorProne) {
50        apply plugin: "net.ltgt.errorprone"
51    }
52
53    group = "io.opencensus"
54    version = "0.32.0-SNAPSHOT" // CURRENT_OPENCENSUS_VERSION
55
56    sourceCompatibility = 1.7
57    targetCompatibility = 1.7
58
59    repositories {
60        mavenCentral()
61        mavenLocal()
62    }
63
64    if (useCheckerFramework) {
65        configurations {
66            checkerFrameworkJavac {
67                description = 'a customization of the Open JDK javac compiler with additional support for type annotations'
68            }
69            checkerFrameworkAnnotatedJDK {
70                description = 'a copy of JDK classes with Checker Framework type qualifiers inserted'
71            }
72        }
73    }
74
75    [compileJava, compileTestJava, compileJmhJava].each() {
76        // We suppress the "try" warning because it disallows managing an auto-closeable with
77        // try-with-resources without referencing the auto-closeable within the try block.
78        // We suppress the "processing" warning as suggested in
79        // https://groups.google.com/forum/#!topic/bazel-discuss/_R3A9TJSoPM
80        it.options.compilerArgs += ["-Xlint:all", "-Xlint:-try", "-Xlint:-processing"]
81        if (useErrorProne) {
82            it.options.compilerArgs += ["-XepAllDisabledChecksAsWarnings", "-XepDisableWarningsInGeneratedCode"]
83
84            // -XepDisableWarningsInGeneratedCode doesn't suppress all warnings in generated
85            // protocol buffer code.
86            it.options.compilerArgs += ["-XepExcludedPaths:.*/gen_gradle/.*"]
87
88            // MutableMethodReturnType can suggest returning Guava types from
89            // API methods (https://github.com/google/error-prone/issues/982).
90            it.options.compilerArgs += ["-Xep:MutableMethodReturnType:OFF"]
91
92            // ReturnMissingNullable conflicts with Checker Framework null analysis.
93            it.options.compilerArgs += ["-Xep:ReturnMissingNullable:OFF"]
94
95            // OpenCensus doesn't currently use Var annotations.
96            it.options.compilerArgs += ["-Xep:Var:OFF"]
97
98            // ImmutableRefactoring suggests using com.google.errorprone.annotations.Immutable,
99            // but OpenCensus currently uses javax.annotation.concurrent.Immutable
100            it.options.compilerArgs += ["-Xep:ImmutableRefactoring:OFF"]
101
102            // This check causes a NullPointerException
103            // (https://github.com/google/error-prone/issues/1138).
104            it.options.compilerArgs += ["-Xep:NullableDereference:OFF"]
105
106            // ExpectedExceptionRefactoring and TestExceptionRefactoring suggest using
107            // assertThrows, but assertThrows only works well with lambdas.
108            it.options.compilerArgs += ["-Xep:ExpectedExceptionRefactoring:OFF"]
109            it.options.compilerArgs += ["-Xep:TestExceptionRefactoring:OFF"]
110        }
111        if (useCheckerFramework) {
112            it.options.compilerArgs += [
113                    '-processor',
114                    'com.google.auto.value.processor.AutoValueProcessor,org.checkerframework.checker.nullness.NullnessChecker',
115                    "-Astubs=$rootDir/checker-framework/stubs"
116            ]
117        }
118        it.options.encoding = "UTF-8"
119        // Protobuf-generated code produces some warnings.
120        // https://github.com/google/protobuf/issues/2718
121        it.options.compilerArgs += ["-Xlint:-cast"]
122        if (!JavaVersion.current().isJava9() && !useErrorProne) {
123            // TODO(sebright): Enable -Werror for Java 9 once we upgrade AutoValue (issue #1017).
124            it.options.compilerArgs += ["-Werror"]
125        }
126        if (JavaVersion.current().isJava9()) {
127            // TODO(sebright): Currently, building with Java 9 produces the following "options" warnings:
128            //
129            // :opencensus-api:compileJavawarning: [options] bootstrap class path not set in conjunction with -source 1.6
130            // warning: [options] source value 1.6 is obsolete and will be removed in a future release
131            // warning: [options] target value 1.6 is obsolete and will be removed in a future release
132            it.options.compilerArgs += ["-Xlint:-options"]
133        }
134    }
135
136    compileTestJava {
137        // serialVersionUID is basically guaranteed to be useless in tests
138        options.compilerArgs += ["-Xlint:-serial"]
139        // It undeprecates DoubleSubject.isEqualTo(Double).
140        options.compilerArgs += ["-Xlint:-deprecation"]
141    }
142
143    jar.manifest {
144        attributes('Implementation-Title': name,
145                'Implementation-Version': version,
146                'Built-By': System.getProperty('user.name'),
147                'Built-JDK': System.getProperty('java.version'),
148                'Source-Compatibility': sourceCompatibility,
149                'Target-Compatibility': targetCompatibility)
150    }
151
152    javadoc.options {
153        encoding = 'UTF-8'
154        links 'https://docs.oracle.com/javase/8/docs/api/'
155    }
156
157    ext {
158        appengineVersion = '1.9.71'
159        aspectjVersion = '1.8.11'
160        autoValueVersion = '1.4'
161        findBugsAnnotationsVersion = '3.0.1'
162        findBugsJsr305Version = '3.0.2'
163        errorProneVersion = '2.3.2'
164        grpcVersion = '1.27.2'
165        guavaVersion = '29.0-android'
166        googleAuthVersion = '0.20.0'
167        googleCloudBetaVersion = '0.100.0-beta'
168        googleCloudGaVersion = '1.82.0'
169        signalfxVersion = '0.0.48'
170        springBoot2Version = '2.1.5.RELEASE'
171        springBootVersion = '1.5.15.RELEASE'
172        springBootTestVersion = '2.1.1.RELEASE'
173        springCloudVersion = '1.3.4.RELEASE'
174        springVersion = '4.3.12.RELEASE'
175        prometheusVersion = '0.6.0'
176        protobufVersion = '3.11.4'
177        zipkinReporterVersion = '2.7.14'
178        jaegerReporterVersion = '0.33.1'
179        opencensusProtoVersion = '0.2.0'
180        gsonVersion = '2.8.6'
181        dropwizardVersion = '3.1.2'
182        dropwizard5Version = '5.0.0'
183        javaxServletVersion = "3.1.0"
184        httpcomponentsVersion = "4.5.8"
185
186        libraries = [
187                appengine_api: "com.google.appengine:appengine-api-1.0-sdk:${appengineVersion}",
188                aspectj: "org.aspectj:aspectjrt:${aspectjVersion}",
189                auto_value: "com.google.auto.value:auto-value:${autoValueVersion}",
190                auto_service: 'com.google.auto.service:auto-service:1.0-rc3',
191                byte_buddy: 'net.bytebuddy:byte-buddy:1.8.22',
192                config: 'com.typesafe:config:1.2.1',
193                disruptor: 'com.lmax:disruptor:3.4.2',
194                errorprone: "com.google.errorprone:error_prone_annotations:${errorProneVersion}",
195                findbugs_annotations: "com.google.code.findbugs:annotations:${findBugsAnnotationsVersion}",
196                google_auth: "com.google.auth:google-auth-library-credentials:${googleAuthVersion}",
197                google_cloud_logging: "com.google.cloud:google-cloud-logging:${googleCloudGaVersion}",
198                google_cloud_trace: "com.google.cloud:google-cloud-trace:${googleCloudBetaVersion}",
199                zipkin_reporter: "io.zipkin.reporter2:zipkin-reporter:${zipkinReporterVersion}",
200                zipkin_urlconnection: "io.zipkin.reporter2:zipkin-sender-urlconnection:${zipkinReporterVersion}",
201                jaeger_reporter: "io.jaegertracing:jaeger-client:${jaegerReporterVersion}",
202                google_cloud_monitoring: "com.google.cloud:google-cloud-monitoring:${googleCloudGaVersion}",
203                grpc_auth: "io.grpc:grpc-auth:${grpcVersion}",
204                grpc_context: "io.grpc:grpc-context:${grpcVersion}",
205                grpc_core: "io.grpc:grpc-core:${grpcVersion}",
206                grpc_netty: "io.grpc:grpc-netty:${grpcVersion}",
207                grpc_netty_shaded: "io.grpc:grpc-netty-shaded:${grpcVersion}",
208                grpc_stub: "io.grpc:grpc-stub:${grpcVersion}",
209                guava: "com.google.guava:guava:${guavaVersion}",
210                jsr305: "com.google.code.findbugs:jsr305:${findBugsJsr305Version}",
211                signalfx_java: "com.signalfx.public:signalfx-java:${signalfxVersion}",
212                spring_aspects: "org.springframework:spring-aspects:${springVersion}",
213                spring_boot_starter_web: "org.springframework.boot:spring-boot-starter-web:${springBootVersion}",
214                spring_boot_starter_web2: "org.springframework.boot:spring-boot-starter-web:${springBoot2Version}",
215                spring_cloud_build: "org.springframework.cloud:spring-cloud-build:${springCloudVersion}",
216                spring_cloud_starter_sleuth: "org.springframework.cloud:spring-cloud-starter-sleuth:${springCloudVersion}",
217                spring_context: "org.springframework:spring-context:${springVersion}",
218                spring_context_support: "org.springframework:spring-context-support:${springVersion}",
219                prometheus_simpleclient: "io.prometheus:simpleclient:${prometheusVersion}",
220                protobuf: "com.google.protobuf:protobuf-java:${protobufVersion}",
221                opencensus_proto: "io.opencensus:opencensus-proto:${opencensusProtoVersion}",
222                gson: "com.google.code.gson:gson:${gsonVersion}",
223                httpcomponents: "org.apache.httpcomponents:httpclient:${httpcomponentsVersion}",
224
225                // Test dependencies.
226                guava_testlib: "com.google.guava:guava-testlib:${guavaVersion}",
227                junit: 'junit:junit:4.12',
228                mockito: 'org.mockito:mockito-core:2.28.1',
229                spring_test: "org.springframework:spring-test:${springVersion}",
230                truth: 'com.google.truth:truth:1.0',
231                spring_boot_test: "org.springframework.boot:spring-boot-starter-test:${springBootTestVersion}",
232                spring_boot_test2: "org.springframework.boot:spring-boot-starter-test:${springBoot2Version}",
233                dropwizard: "io.dropwizard.metrics:metrics-core:${dropwizardVersion}",
234                dropwizard5: "io.dropwizard.metrics5:metrics-core:${dropwizard5Version}",
235                sprint_boot_starter_tomcat: "org.springframework.boot:spring-boot-starter-tomcat:${springBoot2Version}",
236                javax_servlet: "javax.servlet:javax.servlet-api:${javaxServletVersion}",
237
238        ]
239    }
240
241    configurations {
242        compile {
243            // Detect Maven Enforcer's dependencyConvergence failures. We only
244            // care for artifacts used as libraries by others.
245            if (!(project.name in ['benchmarks', 'opencensus-all'])) {
246                resolutionStrategy.failOnVersionConflict()
247            }
248        }
249    }
250
251    dependencies {
252        if (useCheckerFramework) {
253            ext.checkerFrameworkVersion = '2.10.1'
254
255            // 2.4.0 is the last version of the Checker Framework compiler that supports annotations
256            // in comments, though it should continue to work with newer versions of the Checker Framework.
257            // See
258            // https://github.com/census-instrumentation/opencensus-java/pull/1112#issuecomment-381366366.
259            ext.checkerFrameworkCompilerVersion = '2.4.0'
260
261            ext.jdkVersion = 'jdk8'
262            checkerFrameworkAnnotatedJDK "org.checkerframework:${jdkVersion}:${checkerFrameworkVersion}"
263            checkerFrameworkJavac "org.checkerframework:compiler:${checkerFrameworkCompilerVersion}"
264            compileOnly "org.checkerframework:checker:${checkerFrameworkVersion}"
265            compile "org.checkerframework:checker-qual:${checkerFrameworkVersion}"
266            compileOnly libraries.auto_value
267        }
268
269        compileOnly libraries.errorprone,
270                libraries.jsr305
271
272        testCompile libraries.guava_testlib,
273                libraries.junit,
274                libraries.mockito,
275                libraries.truth
276
277        if (useErrorProne) {
278            // The ErrorProne plugin defaults to the latest, which would break our
279            // build if error prone releases a new version with a new check
280            errorprone "com.google.errorprone:error_prone_core:${errorProneVersion}"
281        }
282    }
283
284    findbugs {
285        toolVersion = findBugsAnnotationsVersion
286        ignoreFailures = false   // bug free or it doesn't ship!
287        effort = 'max'
288        reportLevel = 'low'      // low = sensitive to even minor mistakes
289        omitVisitors = []        // bugs that we want to ignore
290        excludeFilter = file("$rootDir/findbugs-exclude.xml")
291    }
292    // Generate html report for findbugs.
293    findbugsMain {
294        reports {
295            xml.enabled = false
296            html.enabled = true
297        }
298    }
299    findbugsTest {
300        reports {
301            xml.enabled = false
302            html.enabled = true
303        }
304    }
305    findbugsJmh {
306        reports {
307            xml.enabled = false
308            html.enabled = true
309        }
310    }
311
312    checkstyle {
313        configFile = file("$rootDir/buildscripts/checkstyle.xml")
314        toolVersion = "8.12"
315        ignoreFailures = false
316        if (rootProject.hasProperty("checkstyle.ignoreFailures")) {
317            ignoreFailures = rootProject.properties["checkstyle.ignoreFailures"].toBoolean()
318        }
319        configProperties["rootDir"] = rootDir
320    }
321
322    googleJavaFormat {
323        toolVersion '1.7'
324    }
325
326    afterEvaluate {  // Allow subproject to add more source sets.
327        tasks.googleJavaFormat {
328            source = sourceSets*.allJava
329            include '**/*.java'
330        }
331
332        tasks.verifyGoogleJavaFormat {
333            source = sourceSets*.allJava
334            include '**/*.java'
335        }
336    }
337
338    signing {
339        required false
340        if (rootProject.hasProperty('signingUseGpgCmd')) {
341          useGpgCmd()
342        }
343        sign configurations.archives
344    }
345
346    task javadocJar(type: Jar) {
347        classifier = 'javadoc'
348        from javadoc
349    }
350
351    task sourcesJar(type: Jar) {
352        classifier = 'sources'
353        from sourceSets.main.allSource
354    }
355
356    artifacts {
357        archives javadocJar, sourcesJar
358    }
359
360    jmh {
361        jmhVersion = '1.20'
362        warmupIterations = 10
363        iterations = 10
364        fork = 1
365        failOnError = true
366        resultFormat = 'JSON'
367        // Allow to run single benchmark class like:
368        // ./gradlew -PjmhIncludeSingleClass=StatsTraceContextBenchmark clean :grpc-core:jmh
369        if (project.hasProperty('jmhIncludeSingleClass')) {
370            include = [
371                    project.property('jmhIncludeSingleClass')
372            ]
373        }
374    }
375
376    jmhReport {
377        jmhResultPath = project.file("${project.buildDir}/reports/jmh/results.json")
378        jmhReportOutput = project.file("${project.buildDir}/reports/jmh")
379    }
380
381    tasks.jmh.finalizedBy tasks.jmhReport
382
383    uploadArchives {
384        repositories {
385            mavenDeployer {
386                beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
387
388                def configureAuth = {
389                    if (rootProject.hasProperty('ossrhUsername') && rootProject.hasProperty('ossrhPassword')) {
390                        authentication(userName:rootProject.ossrhUsername, password: rootProject.ossrhPassword)
391                    }
392                }
393
394                repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/", configureAuth)
395
396                snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/", configureAuth)
397
398                pom.project {
399                    name "OpenCensus"
400                    packaging 'jar'
401                    description project.description
402                    url 'https://github.com/census-instrumentation/opencensus-java'
403
404                    scm {
405                        connection 'scm:svn:https://github.com/census-instrumentation/opencensus-java'
406                        developerConnection 'scm:git:[email protected]/census-instrumentation/opencensus-java'
407                        url 'https://github.com/census-instrumentation/opencensus-java'
408                    }
409
410                    licenses {
411                        license {
412                            name 'The Apache License, Version 2.0'
413                            url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
414                        }
415                    }
416
417                    developers {
418                        developer {
419                            id 'io.opencensus'
420                            name 'OpenCensus Contributors'
421                            email '[email protected]'
422                            url 'opencensus.io'
423                            // https://issues.gradle.org/browse/GRADLE-2719
424                            organization = 'OpenCensus Authors'
425                            organizationUrl 'https://www.opencensus.io'
426                        }
427                    }
428                }
429            }
430        }
431    }
432
433    // Upload the following artifacts only:
434    uploadArchives.onlyIf {
435        name in ['opencensus-api',
436                 'opencensus-contrib-agent',
437                 'opencensus-contrib-appengine-standard-util',
438                 'opencensus-contrib-dropwizard',
439                 'opencensus-contrib-dropwizard5',
440                 'opencensus-contrib-exemplar-util',
441                 'opencensus-contrib-grpc-metrics',
442                 'opencensus-contrib-grpc-util',
443                 'opencensus-contrib-http-jaxrs',
444                 'opencensus-contrib-http-jetty-client',
445                 'opencensus-contrib-http-servlet',
446                 'opencensus-contrib-http-util',
447                 'opencensus-contrib-log-correlation-stackdriver',
448                 'opencensus-contrib-observability-ready-util',
449                 'opencensus-contrib-resource-util',
450                 'opencensus-contrib-spring',
451                 'opencensus-contrib-spring-sleuth-v1x',
452                 'opencensus-contrib-spring-starter',
453                 'opencensus-contrib-zpages',
454                 'opencensus-exporter-metrics-ocagent',
455                 'opencensus-exporter-metrics-util',
456                 'opencensus-exporter-stats-prometheus',
457                 'opencensus-exporter-stats-signalfx',
458                 'opencensus-exporter-stats-stackdriver',
459                 'opencensus-exporter-trace-datadog',
460                 'opencensus-exporter-trace-elasticsearch',
461                 'opencensus-exporter-trace-instana',
462                 'opencensus-exporter-trace-logging',
463                 'opencensus-exporter-trace-ocagent',
464                 'opencensus-exporter-trace-stackdriver',
465                 'opencensus-exporter-trace-zipkin',
466                 'opencensus-exporter-trace-jaeger',
467                 'opencensus-exporter-trace-util',
468                 'opencensus-impl-core',
469                 'opencensus-impl-lite',
470                 'opencensus-impl',
471                 'opencensus-testing']
472    }
473
474    // At a test failure, log the stack trace to the console so that we don't
475    // have to open the HTML in a browser.
476    test {
477        testLogging {
478            exceptionFormat = 'full'
479            showExceptions true
480            showCauses true
481            showStackTraces true
482        }
483        maxHeapSize = '1500m'
484    }
485
486    if (useCheckerFramework) {
487        allprojects {
488            tasks.withType(JavaCompile).all { JavaCompile compile ->
489                compile.doFirst {
490                    compile.options.compilerArgs += [
491                            '-Xmaxerrs', '10000',
492                            "-Xbootclasspath/p:${configurations.checkerFrameworkAnnotatedJDK.asPath}",
493                            "-AskipDefs=\\.AutoValue_|^io.opencensus.contrib.appengine.standard.util.TraceIdProto\$|^io.opencensus.contrib.appengine.standard.util.TraceProto\$",
494                            "-AinvariantArrays"
495                    ]
496                    options.fork = true
497                    options.forkOptions.jvmArgs += ["-Xbootclasspath/p:${configurations.checkerFrameworkJavac.asPath}"]
498                }
499            }
500        }
501    }
502
503    // For projects that depend on gRPC during test execution, make sure to
504    // also configure ALPN if running on a platform (e.g. FreeBSD) that is not
505    // supported by io.netty:netty-tcnative-boringssl-static:jar. Also see:
506    // https://github.com/grpc/grpc-java/blob/master/SECURITY.md#tls-with-jdk-jetty-alpnnpn
507    if (project.name in ['opencensus-exporter-stats-stackdriver',
508                         'opencensus-exporter-trace-stackdriver']) {
509        def os = org.gradle.internal.os.OperatingSystem.current()
510        if (!os.isLinux() && !os.isWindows() && !os.isMacOsX()) {
511            configurations {
512                alpn
513            }
514            dependencies {
515                alpn 'org.mortbay.jetty.alpn:jetty-alpn-agent:2.0.7'
516            }
517            test {
518                jvmArgs "-javaagent:${configurations.alpn.asPath}"
519            }
520        }
521    }
522}
523