import java.time.Duration apply plugin: 'java-library' apply plugin: 'groovy' apply from: "$rootDir/gradle/spotless.gradle" apply from: "$rootDir/gradle/checkstyle.gradle" apply from: "$rootDir/gradle/codenarc.gradle" apply from: "$rootDir/gradle/spotbugs.gradle" def applyCodeCoverage = !( project.path.startsWith(":smoke-tests") || //TODO why some tests fail on java 11 if jacoco is present? project.path == ":opentelemetry-javaagent" || project.path == ":load-generator" || project.path.startsWith(":benchmark") || project.path.startsWith(":instrumentation")) if (applyCodeCoverage) { apply from: "$rootDir/gradle/jacoco.gradle" } if (project.hasProperty("minJavaVersionForTests")) { project.ext.release = project.getProperty("minJavaVersionForTests") } else { project.ext.release = JavaVersion.VERSION_1_7 } java { sourceCompatibility = JavaVersion.toVersion(project.ext.release) targetCompatibility = JavaVersion.toVersion(project.ext.release) // See https://docs.gradle.org/current/userguide/upgrading_version_5.html, Automatic target JVM version disableAutoTargetJvm() withJavadocJar() withSourcesJar() } sourceSets { main { java { srcDir 'src/main/java' } } } tasks.withType(JavaCompile) { options.compilerArgs.addAll(['--release', project.ext.release.majorVersion]) } tasks.withType(GroovyCompile) { options.compilerArgs.addAll(['--release', project.ext.release.majorVersion]) } tasks.withType(ScalaCompile) { options.compilerArgs.addAll(['--release', project.ext.release.majorVersion]) } apply plugin: "eclipse" eclipse { classpath { downloadSources = true downloadJavadoc = true } } if (configurations.find { it.name == 'jmh' }) { eclipse.classpath.plusConfigurations += [configurations.jmh] } jar { /* Make Jar build fail on duplicate files By default Gradle Jar task can put multiple files with the same name into a Jar. This may lead to confusion. For example if auto-service annotation processing creates files with same name in `scala` and `java` directory this would result in Jar having two files with the same name in it. Which in turn would result in only one of those files being actually considered when that Jar is used leading to very confusing failures. Instead we should 'fail early' and avoid building such Jars. */ duplicatesStrategy = 'fail' } repositories { mavenLocal() jcenter() mavenCentral() maven { url "https://repo.typesafe.com/typesafe/releases" } // this is only needed for the working against unreleased otel-java snapshots maven { url "https://oss.jfrog.org/artifactory/oss-snapshot-local" content { includeGroup "io.opentelemetry" } } } dependencies { testImplementation deps.spock testImplementation deps.groovy testImplementation deps.testLogging testImplementation group: 'info.solidsoft.spock', name: 'spock-global-unroll', version: '0.5.1' testImplementation group: 'com.github.stefanbirkner', name: 'system-rules', version: '1.19.0' } jar { manifest { attributes( "Implementation-Title": project.name, "Implementation-Version": project.version, "Implementation-Vendor": "OpenTelemetry", "Implementation-URL": "https://github.com/open-telemetry/opentelemetry-java-instrumentation", ) } } javadoc { options.addStringOption('Xdoclint:none', '-quiet') doFirst { if (project.ext.has("apiLinks")) { options.links(*project.apiLinks) } } source = sourceSets.main.allJava classpath = configurations.compileClasspath options { encoding = "utf-8" docEncoding = "utf-8" charSet = "utf-8" setMemberLevel JavadocMemberLevel.PUBLIC setAuthor true links "https://docs.oracle.com/javase/8/docs/api/" source = 8 } } project.afterEvaluate { if (project.plugins.hasPlugin('org.unbroken-dome.test-sets') && configurations.hasProperty("latestDepTestRuntime")) { tasks.withType(Test).configureEach { doFirst { def testArtifacts = configurations.testRuntimeClasspath.resolvedConfiguration.resolvedArtifacts def latestTestArtifacts = configurations.latestDepTestRuntimeClasspath.resolvedConfiguration.resolvedArtifacts assert testArtifacts != latestTestArtifacts: "latestDepTest dependencies are identical to test" } } } } if (project.hasProperty("removeJarVersionNumbers") && removeJarVersionNumbers) { tasks.withType(AbstractArchiveTask).configureEach { version = null } } if (!rootProject.ext.has("javaExecutableVersionCache")) { rootProject.ext.javaExecutableVersionCache = [:] } /** * Returns version of java from a given java home. */ JavaVersion getJavaHomeVersion(String javaHome) { def cache = rootProject.ext.javaExecutableVersionCache if (cache.containsKey(javaHome)) { return cache.get(javaHome) } new ByteArrayOutputStream().withStream { stream -> exec { commandLine = [toExecutable(javaHome), "-version"] errorOutput = stream } def output = stream.toString() for (def line : output.split('\n')) { line = line.trim() def matcher = line =~ /^(?:java|openjdk) version "([^"]+)"/ if (matcher) { def version = JavaVersion.toVersion(matcher.group(1)) cache.put(javaHome, version) return version } } // Getting here means we didn't find a line matching the version pattern. throw new GradleScriptException("Cannot determine java version. Executable: ${javaHome}, output: ${output}", null) } } def isJavaVersionAllowed(JavaVersion version) { if (project.hasProperty('minJavaVersionForTests') && project.getProperty('minJavaVersionForTests').compareTo(version) > 0) { return false } if (project.hasProperty('maxJavaVersionForTests') && project.getProperty('maxJavaVersionForTests').compareTo(version) < 0) { return false } return true } /** * For a given java home return the location of java executable */ static String toExecutable(String javaHome) { return Objects.requireNonNull(javaHome) + "/bin/java" } /** * Returns java home for a given version or {@code null} if not found */ String findJavaHome(JavaVersion version) { def javaHome = System.getenv("JAVA_${version.majorVersion}_HOME") if (javaHome == null) { return null } def foundVersion = getJavaHomeVersion(javaHome) return version == foundVersion ? javaHome : null } ext { findJavaHome = this.&findJavaHome toExecutable = this.&toExecutable } def addTestRule(String testTaskName) { def prefix = testTaskName + "Java" tasks.addRule("Pattern: $prefix: Runs tests using given java version") { String taskName -> if (taskName.startsWith(prefix)) { def requestedJavaVersion = JavaVersion.toVersion(taskName - prefix) def gradleJavaVersion = JavaVersion.current() if (gradleJavaVersion != requestedJavaVersion) { def javaHomeForTests = findJavaHome(requestedJavaVersion) if (javaHomeForTests != null) { tasks.withType(Test).all { executable = toExecutable(javaHomeForTests) enabled = isJavaVersionAllowed(requestedJavaVersion) if (requestedJavaVersion.isJava7()) { // Disable JIT for this method. Sometimes Java7 JVM crashes trying to compile it. jvmArgs '-XX:CompileCommand=exclude,net.bytebuddy.description.type.TypeDescription$Generic$Visitor$Substitutor::onParameterizedType' } } } else { throw new BuildCancelledException("Requested java version $requestedJavaVersion not found") } } task(taskName) { if (project.tasks.findByName(testTaskName) != null) { dependsOn testTaskName } } } } } addTestRule("test") addTestRule("integrationTest") addTestRule("latestDepTest") tasks.withType(Test).configureEach { if (project.findProperty("enableJunitPlatform") == true) { useJUnitPlatform() } // All tests must complete within 15 minutes. // This value is quite big because with lower values (3 mins) we were experiencing large number of false positives timeout = Duration.ofMinutes(15) // Disable all tests if current JVM doesn't match version requirements // Disable all tests if skipTests property was specified enabled = isJavaVersionAllowed(JavaVersion.current()) && !project.rootProject.hasProperty("skipTests") }