From d872e2c5279a8a9827c0851218b1f4a641265639 Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Mon, 16 Nov 2015 09:15:31 -0800 Subject: [PATCH 1/4] Build: Use JDK at JAVA_HOME for compiling/testing, and improve build info output We currently enforce JAVA_HOME is set, but use gradle's java version to compile, and JAVA_HOME version to test. This change makes the compile and test both use JAVA_HOME, and clarifies which java version is used by gradle and that used for the compile/test. --- .../elasticsearch/gradle/BuildPlugin.groovy | 100 ++++++++++++++---- 1 file changed, 80 insertions(+), 20 deletions(-) diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy index 8e1dce66f41..7ed566bc4e2 100644 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy +++ b/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy @@ -63,18 +63,35 @@ class BuildPlugin implements Plugin { PrecommitTasks.configure(project) } + /** Performs checks on the build environment and prints information about the build environment. */ static void globalBuildInfo(Project project) { if (project.rootProject.ext.has('buildChecksDone') == false) { - String javaHome = System.getenv('JAVA_HOME') + String javaHome = findJavaHome() + File gradleJavaHome = Jvm.current().javaHome + String gradleJavaVersionDetails = "${System.getProperty('java.vendor')} ${System.getProperty('java.version')}" + + " [${System.getProperty('java.vm.name')} ${System.getProperty('java.vm.version')}]" + + String javaVersionDetails = gradleJavaVersionDetails + String javaVersion = System.getProperty('java.version') + JavaVersion javaVersionEnum = JavaVersion.current() + if (new File(javaHome).canonicalPath != gradleJavaHome.canonicalPath) { + javaVersionDetails = findJavaVersionDetails(project, javaHome) + javaVersionEnum = JavaVersion.toVersion(findJavaSpecificationVersion(project, javaHome)) + javaVersion = findJavaVersion(project, javaHome) + } // Build debugging info println '=======================================' println 'Elasticsearch Build Hamster says Hello!' println '=======================================' - println " Gradle Version : ${project.gradle.gradleVersion}" - println " JDK Version : ${System.getProperty('java.runtime.version')} (${System.getProperty('java.vendor')})" - println " JAVA_HOME : ${javaHome == null ? 'not set' : javaHome}" - println " OS Info : ${System.getProperty('os.name')} ${System.getProperty('os.version')} (${System.getProperty('os.arch')})" + println " Gradle Version : ${project.gradle.gradleVersion}" + println " OS Info : ${System.getProperty('os.name')} ${System.getProperty('os.version')} (${System.getProperty('os.arch')})" + if (gradleJavaVersionDetails != javaVersionDetails) { + println " JDK Version (gradle) : ${gradleJavaVersionDetails}" + println " JDK Version (compile) : ${javaVersionDetails}" + } else { + println " JDK Version : ${gradleJavaVersionDetails}" + } // enforce gradle version GradleVersion minGradle = GradleVersion.version('2.8') @@ -83,31 +100,71 @@ class BuildPlugin implements Plugin { } // enforce Java version - if (JavaVersion.current() < minimumJava) { + if (javaVersionEnum < minimumJava) { throw new GradleException("Java ${minimumJava} or above is required to build Elasticsearch") } - // find java home so eg tests can use it to set java to run with - if (javaHome == null) { - if (System.getProperty("idea.active") != null) { - // intellij doesn't set JAVA_HOME, so we use the jdk gradle was run with - javaHome = Jvm.current().javaHome - } else { - throw new GradleException('JAVA_HOME must be set to build Elasticsearch') - } - } project.rootProject.ext.javaHome = javaHome + project.rootProject.ext.javaVersion = javaVersion project.rootProject.ext.buildChecksDone = true } project.targetCompatibility = minimumJava project.sourceCompatibility = minimumJava // set java home for each project, so they dont have to find it in the root project project.ext.javaHome = project.rootProject.ext.javaHome + project.ext.javaVersion = project.rootProject.ext.javaVersion } - /** Return the name - */ - static String transitiveDepConfigName(String groupId, String artifactId, String version) { + /** Finds and enforces JAVA_HOME is set */ + private static String findJavaHome() { + String javaHome = System.getenv('JAVA_HOME') + if (javaHome == null) { + if (System.getProperty("idea.active") != null) { + // intellij doesn't set JAVA_HOME, so we use the jdk gradle was run with + javaHome = Jvm.current().javaHome + } else { + throw new GradleException('JAVA_HOME must be set to build Elasticsearch') + } + } + return javaHome + } + + /** Finds printable java version of the given JAVA_HOME */ + private static String findJavaVersionDetails(Project project, String javaHome) { + String versionInfoScript = 'print(' + + 'java.lang.System.getProperty("java.vendor") + " " + java.lang.System.getProperty("java.version") + ' + + '" [" + java.lang.System.getProperty("java.vm.name") + " " + java.lang.System.getProperty("java.vm.version") + "]");' + return runJavascript(project, javaHome, versionInfoScript).trim() + } + + /** Finds the parsable java specification version */ + private static String findJavaSpecificationVersion(Project project, String javaHome) { + String versionScript = 'print(java.lang.System.getProperty("java.specification.version"));' + return runJavascript(project, javaHome, versionScript) + } + + /** Finds the parsable java specification version */ + private static String findJavaVersion(Project project, String javaHome) { + String versionScript = 'print(java.lang.System.getProperty("java.version"));' + return runJavascript(project, javaHome, versionScript) + } + + /** Runs the given javascript using jjs from the jdk, and returns the output */ + private static String runJavascript(Project project, String javaHome, String script) { + File tmpScript = File.createTempFile('es-gradle-tmp', '.js'); + tmpScript.setText(script, 'UTF-8') + ByteArrayOutputStream output = new ByteArrayOutputStream() + project.exec { + executable = new File(javaHome, 'bin/jjs') + args tmpScript.toString() + standardOutput = output + errorOutput = new ByteArrayOutputStream() + } + return output.toString('UTF-8').trim() + } + + /** Return the configuration name used for finding transitive deps of the given dependency. */ + private static String transitiveDepConfigName(String groupId, String artifactId, String version) { return "_transitive_${groupId}:${artifactId}:${version}" } @@ -224,7 +281,9 @@ class BuildPlugin implements Plugin { project.afterEvaluate { // fail on all javac warnings project.tasks.withType(JavaCompile) { - options.compilerArgs << '-Werror' << '-Xlint:all' << '-Xdoclint:all/private' << '-Xdoclint:-missing' + options.fork = true + options.forkOptions.executable = new File(project.javaHome, 'bin/javac') + options.compilerArgs << '-Werror' << '-Xlint:all' << '-Xdoclint:all' << '-Xdoclint:-missing' options.encoding = 'UTF-8' } } @@ -239,7 +298,8 @@ class BuildPlugin implements Plugin { jarTask.manifest.attributes( 'X-Compile-Elasticsearch-Version': VersionProperties.elasticsearch, 'X-Compile-Lucene-Version': VersionProperties.lucene, - 'Build-Date': ZonedDateTime.now(ZoneOffset.UTC)) + 'Build-Date': ZonedDateTime.now(ZoneOffset.UTC), + 'Build-Java-Version': project.javaVersion) } } } From d1a27aed16db521cdc43f57ed5e43773d57c25df Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Thu, 19 Nov 2015 10:33:31 -0800 Subject: [PATCH 2/4] Make the tmp file created for jjs scripts be deleted when gradle exits --- .../main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy index a0a6c1cb7bc..013cdcc43ba 100644 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy +++ b/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy @@ -151,7 +151,8 @@ class BuildPlugin implements Plugin { /** Runs the given javascript using jjs from the jdk, and returns the output */ private static String runJavascript(Project project, String javaHome, String script) { - File tmpScript = File.createTempFile('es-gradle-tmp', '.js'); + File tmpScript = File.createTempFile('es-gradle-tmp', '.js') + tmpScript.deleteOnExit() tmpScript.setText(script, 'UTF-8') ByteArrayOutputStream output = new ByteArrayOutputStream() project.exec { From e832fc6000f4abdab788ce024add766ef56f0bd4 Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Thu, 19 Nov 2015 11:24:31 -0800 Subject: [PATCH 3/4] Make tmp file handling for jjs scripts better --- .../groovy/org/elasticsearch/gradle/BuildPlugin.groovy | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy index 013cdcc43ba..b5c4630ddd5 100644 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy +++ b/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy @@ -18,6 +18,8 @@ */ package org.elasticsearch.gradle +import org.gradle.process.ExecResult + import java.time.ZonedDateTime import java.time.ZoneOffset @@ -152,15 +154,18 @@ class BuildPlugin implements Plugin { /** Runs the given javascript using jjs from the jdk, and returns the output */ private static String runJavascript(Project project, String javaHome, String script) { File tmpScript = File.createTempFile('es-gradle-tmp', '.js') - tmpScript.deleteOnExit() + println "TMP: ${tmpScript.toString()}" tmpScript.setText(script, 'UTF-8') ByteArrayOutputStream output = new ByteArrayOutputStream() - project.exec { + ExecResult result = project.exec { executable = new File(javaHome, 'bin/jjs') args tmpScript.toString() standardOutput = output errorOutput = new ByteArrayOutputStream() + ignoreExitValue = true // we do not fail so we can first cleanup the tmp file } + java.nio.file.Files.delete(tmpScript.toPath()) + result.assertNormalExitValue() return output.toString('UTF-8').trim() } From 6b734dc0091f87eb8097129fccd46e6aaa23e7cf Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Fri, 20 Nov 2015 11:31:46 -0800 Subject: [PATCH 4/4] Remove debugging output from running jjs scripts --- .../src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy | 1 - 1 file changed, 1 deletion(-) diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy index 6fcd7d4c25e..5ffe673ef1e 100644 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy +++ b/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy @@ -154,7 +154,6 @@ class BuildPlugin implements Plugin { /** Runs the given javascript using jjs from the jdk, and returns the output */ private static String runJavascript(Project project, String javaHome, String script) { File tmpScript = File.createTempFile('es-gradle-tmp', '.js') - println "TMP: ${tmpScript.toString()}" tmpScript.setText(script, 'UTF-8') ByteArrayOutputStream output = new ByteArrayOutputStream() ExecResult result = project.exec {