|
| 1 | +import java.time.Duration |
| 2 | + |
| 3 | +apply plugin: 'java-library' |
| 4 | +apply plugin: 'groovy' |
| 5 | +apply plugin: 'org.gradle.test-retry' |
| 6 | + |
| 7 | +// Version to use to compile code and run tests. |
| 8 | +def DEFAULT_JAVA_VERSION = 11 |
| 9 | + |
| 10 | +jar { |
| 11 | + /* |
| 12 | + Make Jar build fail on duplicate files |
| 13 | +
|
| 14 | + By default Gradle Jar task can put multiple files with the same name |
| 15 | + into a Jar. This may lead to confusion. For example if auto-service |
| 16 | + annotation processing creates files with same name in `scala` and |
| 17 | + `java` directory this would result in Jar having two files with the |
| 18 | + same name in it. Which in turn would result in only one of those |
| 19 | + files being actually considered when that Jar is used leading to very |
| 20 | + confusing failures. |
| 21 | +
|
| 22 | + Instead we should 'fail early' and avoid building such Jars. |
| 23 | + */ |
| 24 | + duplicatesStrategy = 'fail' |
| 25 | +} |
| 26 | + |
| 27 | +repositories { |
| 28 | + mavenLocal() |
| 29 | + mavenCentral() |
| 30 | + jcenter() |
| 31 | + maven { |
| 32 | + url "https://repo.typesafe.com/typesafe/releases" |
| 33 | + } |
| 34 | + // this is only needed for the working against unreleased otel-java snapshots |
| 35 | + maven { |
| 36 | + url "https://oss.jfrog.org/artifactory/oss-snapshot-local" |
| 37 | + content { |
| 38 | + includeGroup "io.opentelemetry" |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +ext { |
| 44 | + deps = [ |
| 45 | + spock : [ |
| 46 | + dependencies.create("org.spockframework:spock-core:1.3-groovy-2.5", { |
| 47 | + exclude group: 'org.codehaus.groovy', module: 'groovy-all' |
| 48 | + }), |
| 49 | + // Used by Spock for mocking: |
| 50 | + dependencies.create(group: 'org.objenesis', name: 'objenesis', version: '3.1') |
| 51 | + ], |
| 52 | + groovy : "org.codehaus.groovy:groovy-all:2.5.11", |
| 53 | + testLogging: [ |
| 54 | + dependencies.create(group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'), |
| 55 | + dependencies.create(group: 'org.slf4j', name: 'log4j-over-slf4j', version: '1.7.30'), |
| 56 | + dependencies.create(group: 'org.slf4j', name: 'jcl-over-slf4j', version: '1.7.30'), |
| 57 | + dependencies.create(group: 'org.slf4j', name: 'jul-to-slf4j', version: '1.7.30'), |
| 58 | + ] |
| 59 | + ] |
| 60 | +} |
| 61 | + |
| 62 | +dependencies { |
| 63 | + compileOnly group: 'org.checkerframework', name: 'checker-qual', version: '3.6.1' |
| 64 | + |
| 65 | + testImplementation enforcedPlatform(group: 'org.junit', name: 'junit-bom', version: '5.7.0-M1') |
| 66 | + testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api' |
| 67 | + testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-params' |
| 68 | + testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine' |
| 69 | + testRuntimeOnly group: 'org.junit.vintage', name: 'junit-vintage-engine' |
| 70 | + |
| 71 | + testImplementation deps.spock |
| 72 | + testImplementation deps.groovy |
| 73 | + testImplementation deps.testLogging |
| 74 | + testImplementation group: 'info.solidsoft.spock', name: 'spock-global-unroll', version: '0.5.1' |
| 75 | + testImplementation group: 'com.github.stefanbirkner', name: 'system-rules', version: '1.19.0' |
| 76 | +} |
| 77 | + |
| 78 | +jar { |
| 79 | + manifest { |
| 80 | + attributes( |
| 81 | + "Implementation-Title": project.name, |
| 82 | + "Implementation-Version": project.version, |
| 83 | + "Implementation-Vendor": "Hypertace", |
| 84 | + "Implementation-URL": "https://github.com/hypertrace/hypertrace", |
| 85 | + ) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +normalization { |
| 90 | + runtimeClasspath { |
| 91 | + metaInf { |
| 92 | + ignoreAttribute("Implementation-Version") |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +javadoc { |
| 98 | + options.addStringOption('Xdoclint:none', '-quiet') |
| 99 | + |
| 100 | + doFirst { |
| 101 | + if (project.ext.has("apiLinks")) { |
| 102 | + options.links(*project.apiLinks) |
| 103 | + } |
| 104 | + } |
| 105 | + source = sourceSets.main.allJava |
| 106 | + classpath = configurations.compileClasspath |
| 107 | + |
| 108 | + options { |
| 109 | + encoding = "utf-8" |
| 110 | + docEncoding = "utf-8" |
| 111 | + charSet = "utf-8" |
| 112 | + |
| 113 | + setMemberLevel JavadocMemberLevel.PUBLIC |
| 114 | + setAuthor true |
| 115 | + |
| 116 | + links "https://docs.oracle.com/javase/8/docs/api/" |
| 117 | + source = 8 |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +project.afterEvaluate { |
| 122 | + if (project.plugins.hasPlugin('org.unbroken-dome.test-sets') && configurations.hasProperty("latestDepTestRuntime")) { |
| 123 | + tasks.withType(Test).configureEach { |
| 124 | + doFirst { |
| 125 | + def testArtifacts = configurations.testRuntimeClasspath.resolvedConfiguration.resolvedArtifacts |
| 126 | + def latestTestArtifacts = configurations.latestDepTestRuntimeClasspath.resolvedConfiguration.resolvedArtifacts |
| 127 | + assert testArtifacts != latestTestArtifacts: "latestDepTest dependencies are identical to test" |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +def isJavaVersionAllowed(JavaVersion version) { |
| 134 | + if (project.hasProperty('minJavaVersionForTests') && project.getProperty('minJavaVersionForTests').compareTo(version) > 0) { |
| 135 | + return false |
| 136 | + } |
| 137 | + if (project.hasProperty('maxJavaVersionForTests') && project.getProperty('maxJavaVersionForTests').compareTo(version) < 0) { |
| 138 | + return false |
| 139 | + } |
| 140 | + return true |
| 141 | +} |
| 142 | + |
| 143 | +def testJavaVersion = rootProject.findProperty('testJavaVersion') |
| 144 | +if (testJavaVersion != null) { |
| 145 | + def requestedJavaVersion = JavaVersion.toVersion(testJavaVersion) |
| 146 | + tasks.withType(Test).all { |
| 147 | + javaLauncher = javaToolchains.launcherFor { |
| 148 | + languageVersion = JavaLanguageVersion.of(requestedJavaVersion.majorVersion) |
| 149 | + } |
| 150 | + enabled = isJavaVersionAllowed(requestedJavaVersion) |
| 151 | + } |
| 152 | +} else { |
| 153 | + // We default to testing with Java 11 for most tests, but some tests don't support it, where we change |
| 154 | + // the default test task's version so commands like `./gradlew check` can test all projects regardless |
| 155 | + // of Java version. |
| 156 | + if (!isJavaVersionAllowed(JavaVersion.toVersion(DEFAULT_JAVA_VERSION))) { |
| 157 | + tasks.withType(Test) { |
| 158 | + javaLauncher = javaToolchains.launcherFor { |
| 159 | + languageVersion = JavaLanguageVersion.of(project.getProperty('maxJavaVersionForTests').majorVersion) |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +tasks.withType(Test).configureEach { |
| 166 | + useJUnitPlatform() |
| 167 | + |
| 168 | + // All tests must complete within 30 minutes. |
| 169 | + // This value is quite big because with lower values (3 mins) we were experiencing large number of false positives |
| 170 | + timeout = Duration.ofMinutes(30) |
| 171 | + |
| 172 | + retry { |
| 173 | + // You can see tests that were retried by this mechanism in the collected test reports and build scans. |
| 174 | + maxRetries = System.getenv("CI") != null ? 5 : 0 |
| 175 | + } |
| 176 | + |
| 177 | + reports { |
| 178 | + junitXml.outputPerTestCase = true |
| 179 | + } |
| 180 | + |
| 181 | + testLogging { |
| 182 | + exceptionFormat = 'full' |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +tasks.withType(AbstractArchiveTask) { |
| 187 | + preserveFileTimestamps = false |
| 188 | + reproducibleFileOrder = true |
| 189 | +} |
| 190 | + |
| 191 | +plugins.withId('net.ltgt.errorprone') { |
| 192 | + dependencies { |
| 193 | + annotationProcessor group: "com.uber.nullaway", name: "nullaway", version: versions.nullaway |
| 194 | + errorprone group: "com.google.errorprone", name: "error_prone_core", version: versions.errorprone |
| 195 | + } |
| 196 | + |
| 197 | + tasks.withType(JavaCompile) { |
| 198 | + if (!name.toLowerCase().contains("test")) { |
| 199 | + options.errorprone { |
| 200 | + error("NullAway") |
| 201 | + |
| 202 | + // Doesn't work well with Java 8 |
| 203 | + disable("FutureReturnValueIgnored") |
| 204 | + |
| 205 | + option("NullAway:AnnotatedPackages", "io.opentelemetry,com.linecorp.armeria,com.google.common") |
| 206 | + } |
| 207 | + } |
| 208 | + } |
| 209 | +} |
0 commit comments