From fce0052fd914544993dc3dea340a555b95ba4acd Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Thu, 19 Nov 2015 02:07:15 -0800 Subject: [PATCH] Build: Add jar hell check before tests run Because jar hell checks run during static initialization of tests, a failure will result in all tests failing. However, only the first test within each jvm shows the jarhell failure, and later tests get a class not found exception for the class that failed to load when static init failed. This change adds a task to run as part of precommit, which checks the test runtime classpath for jarhell. closes #14721 --- .../gradle/precommit/PrecommitTasks.groovy | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/precommit/PrecommitTasks.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/precommit/PrecommitTasks.groovy index 619cfe1ac24..69931a02c34 100644 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/precommit/PrecommitTasks.groovy +++ b/buildSrc/src/main/groovy/org/elasticsearch/gradle/precommit/PrecommitTasks.groovy @@ -18,9 +18,12 @@ */ package org.elasticsearch.gradle.precommit +import org.gradle.api.GradleException import org.gradle.api.Project import org.gradle.api.Task +import org.gradle.api.file.FileCollection import org.gradle.api.plugins.JavaBasePlugin +import org.gradle.api.tasks.Exec import org.gradle.api.tasks.TaskContainer /** @@ -32,7 +35,8 @@ class PrecommitTasks { static void configure(Project project) { List precommitTasks = [ configureForbiddenApis(project), - configureForbiddenPatterns(project.tasks)] + configureForbiddenPatterns(project.tasks), + configureJarHell(project)] Map precommitOptions = [ name: 'precommit', @@ -90,4 +94,31 @@ class PrecommitTasks { rule name: 'tab', pattern: /\t/ } } + + static Task configureJarHell(Project project) { + File successMarker = new File(project.buildDir, 'markers/jarHell') + Exec task = project.tasks.create(name: 'jarHell', type: Exec) + FileCollection testClasspath = project.sourceSets.test.runtimeClasspath + task.dependsOn(testClasspath) + task.inputs.files(testClasspath) + task.outputs.file(successMarker) + task.executable = new File(project.javaHome, 'bin/java') + task.doFirst({ + task.args('-cp', testClasspath.asPath, 'org.elasticsearch.bootstrap.JarHell') + }) + if (task.logger.isInfoEnabled() == false) { + task.standardOutput = new ByteArrayOutputStream() + task.errorOutput = task.standardOutput + task.ignoreExitValue = true + task.doLast({ + if (execResult.exitValue != 0) { + logger.error(standardOutput.toString()) + throw new GradleException("JarHell failed") + } + successMarker.parentFile.mkdirs() + successMarker.setText("", 'UTF-8') + }) + } + return task + } }