Refactor global build info plugin to leverage JavaInstallationRegistry (#54026)

This commit removes the configuration time vs execution time distinction
with regards to certain BuildParms properties. Because of the cost of
determining Java versions for configuration JDK locations we deferred
this until execution time. This had two main downsides. First, we had
to implement all this build logic in tasks, which required a bunch of
additional plumbing and complexity. Second, because some information
wasn't known during configuration time, we had to nest any build logic
that depended on this in awkward callbacks.

We now defer to the JavaInstallationRegistry recently added in Gradle.
This utility uses a much more efficient method for probing Java
installations vs our jrunscript implementation. This, combined with some
optimizations to avoid probing the current JVM as well as deferring
some evaluation via Providers when probing installations for BWC builds
we can maintain effectively the same configuration time performance
while removing a bunch of complexity and runtime cost (snapshotting
inputs for the GenerateGlobalBuildInfoTask was very expensive). The end
result should be a much more responsive build execution in almost all
scenarios.

(cherry picked from commit ecdbd37f2e0f0447ed574b306adb64c19adc3ce1)
This commit is contained in:
Mark Vieira 2020-03-23 15:30:10 -07:00 committed by GitHub
parent c97ee4e695
commit 70cfedf542
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
34 changed files with 654 additions and 1032 deletions

View File

@ -103,9 +103,6 @@ be used to test against other JDKs as well, this is not only limited to JDK 8.
> Note: It is also required to have `JAVA8_HOME`, `JAVA9_HOME`, `JAVA10_HOME`
and `JAVA11_HOME`, and `JAVA12_HOME` available so that the tests can pass.
> Warning: do not use `sdkman` for Java installations which do not have proper
`jrunscript` for jdk distributions.
Elasticsearch uses the Gradle wrapper for its build. You can execute Gradle
using the wrapper via the `gradlew` script on Unix systems or `gradlew.bat`
script on Windows in the root of the repository. The examples below show the

View File

@ -31,7 +31,7 @@ import org.gradle.plugins.ide.eclipse.model.SourceFolder
import org.gradle.util.DistributionLocator
import org.gradle.util.GradleVersion
import static org.elasticsearch.gradle.tool.Boilerplate.maybeConfigure
import static org.elasticsearch.gradle.util.GradleUtils.maybeConfigure
plugins {
id 'lifecycle-base'

View File

@ -26,16 +26,15 @@ import groovy.transform.CompileStatic
import org.apache.commons.io.IOUtils
import org.elasticsearch.gradle.info.BuildParams
import org.elasticsearch.gradle.info.GlobalBuildInfoPlugin
import org.elasticsearch.gradle.info.GlobalInfoExtension
import org.elasticsearch.gradle.info.JavaHome
import org.elasticsearch.gradle.precommit.DependencyLicensesTask
import org.elasticsearch.gradle.precommit.PrecommitTasks
import org.elasticsearch.gradle.test.ErrorReportingTestListener
import org.elasticsearch.gradle.testclusters.ElasticsearchCluster
import org.elasticsearch.gradle.testclusters.TestClustersPlugin
import org.gradle.api.Action
import org.elasticsearch.gradle.testclusters.TestDistribution
import org.elasticsearch.gradle.tool.Boilerplate
import org.elasticsearch.gradle.util.GradleUtils
import org.gradle.api.Action
import org.gradle.api.GradleException
import org.gradle.api.InvalidUserDataException
import org.gradle.api.JavaVersion
@ -84,7 +83,7 @@ import org.gradle.util.GradleVersion
import java.nio.charset.StandardCharsets
import java.nio.file.Files
import static org.elasticsearch.gradle.tool.Boilerplate.maybeConfigure
import static org.elasticsearch.gradle.util.GradleUtils.maybeConfigure
/**
* Encapsulates build configuration for elasticsearch projects.
@ -146,7 +145,7 @@ class BuildPlugin implements Plugin<Project> {
// Common config when running with a FIPS-140 runtime JVM
if (inFipsJvm()) {
// This configuration can be removed once system modules are available
Boilerplate.maybeCreate(project.configurations, 'extraJars') {
GradleUtils.maybeCreate(project.configurations, 'extraJars') {
project.dependencies.add('extraJars', "org.bouncycastle:bc-fips:1.0.1")
project.dependencies.add('extraJars', "org.bouncycastle:bctls-fips:1.0.9")
}
@ -156,7 +155,6 @@ class BuildPlugin implements Plugin<Project> {
File securityPolicy = buildResources.copy("fips_java.policy")
File security8Policy = buildResources.copy("fips_java8.policy")
File bcfksKeystore = buildResources.copy("cacerts.bcfks")
GlobalInfoExtension globalInfo = project.rootProject.extensions.getByType(GlobalInfoExtension)
project.pluginManager.withPlugin("elasticsearch.testclusters") {
NamedDomainObjectContainer<ElasticsearchCluster> testClusters = project.extensions.findByName(TestClustersPlugin.EXTENSION_NAME) as NamedDomainObjectContainer<ElasticsearchCluster>
if (testClusters != null) {
@ -165,7 +163,6 @@ class BuildPlugin implements Plugin<Project> {
for (File dep : project.getConfigurations().getByName("extraJars").getFiles()) {
cluster.extraJarFile(dep)
}
globalInfo.ready {
if (BuildParams.runtimeJavaVersion > JavaVersion.VERSION_1_8) {
cluster.extraConfigFile("fips_java.security", securityProperties)
cluster.extraConfigFile("fips_java.policy", securityPolicy)
@ -173,7 +170,6 @@ class BuildPlugin implements Plugin<Project> {
cluster.extraConfigFile("fips_java.security", security8Properties)
cluster.extraConfigFile("fips_java.policy", security8Policy)
}
}
cluster.extraConfigFile("cacerts.bcfks", bcfksKeystore)
cluster.systemProperty('java.security.properties', '=${ES_PATH_CONF}/fips_java.security')
cluster.systemProperty('java.security.policy', '=${ES_PATH_CONF}/fips_java.policy')
@ -188,7 +184,6 @@ class BuildPlugin implements Plugin<Project> {
}
project.tasks.withType(Test).configureEach { Test task ->
task.dependsOn(buildResources)
globalInfo.ready {
// Using the key==value format to override default JVM security settings and policy
// see also: https://docs.oracle.com/javase/8/docs/technotes/guides/security/PolicyFiles.html
if (BuildParams.runtimeJavaVersion > JavaVersion.VERSION_1_8) {
@ -198,7 +193,6 @@ class BuildPlugin implements Plugin<Project> {
task.systemProperty('java.security.properties', String.format(Locale.ROOT, "=%s", security8Properties.toString()))
task.systemProperty('java.security.policy', String.format(Locale.ROOT, "=%s", security8Policy.toString()))
}
}
task.systemProperty('javax.net.ssl.trustStorePassword', 'password')
task.systemProperty('javax.net.ssl.keyStorePassword', 'password')
task.systemProperty('javax.net.ssl.trustStoreType', 'BCFKS')
@ -258,7 +252,7 @@ class BuildPlugin implements Plugin<Project> {
static String getJavaHome(final Task task, final int version) {
requireJavaHome(task, version)
JavaHome java = BuildParams.javaVersions.find { it.version == version }
return java == null ? null : java.javaHome.absolutePath
return java == null ? null : java.javaHome.get().absolutePath
}
/**
@ -407,7 +401,7 @@ class BuildPlugin implements Plugin<Project> {
dependencyNode.appendNode('groupId', dependency.group)
dependencyNode.appendNode('artifactId', dependency.getDependencyProject().convention.getPlugin(BasePluginConvention).archivesBaseName)
dependencyNode.appendNode('version', dependency.version)
dependencyNode.appendNode('scope', 'runtime')
dependencyNode.appendNode('scope', 'compile')
}
}
}
@ -426,14 +420,11 @@ class BuildPlugin implements Plugin<Project> {
/** Adds compiler settings to the project */
static void configureCompile(Project project) {
ExtraPropertiesExtension ext = project.extensions.getByType(ExtraPropertiesExtension)
GlobalInfoExtension globalBuildInfo = project.rootProject.extensions.getByType(GlobalInfoExtension)
globalBuildInfo.ready {
if (BuildParams.compilerJavaVersion < JavaVersion.VERSION_1_10) {
ext.set('compactProfile', 'compact3')
} else {
ext.set('compactProfile', 'full')
}
}
ext.set('compactProfile', 'full')
project.extensions.getByType(JavaPluginExtension).sourceCompatibility = BuildParams.minimumRuntimeVersion
@ -450,14 +441,12 @@ class BuildPlugin implements Plugin<Project> {
compileTask.options.forkOptions.javaHome = BuildParams.compilerJavaHome
}
if (targetCompatibilityVersion == JavaVersion.VERSION_1_8) {
globalBuildInfo.ready {
// compile with compact 3 profile by default
// NOTE: this is just a compile time check: does not replace testing with a compact3 JRE
if (ext.get('compactProfile') != 'full') {
compileTask.options.compilerArgs << '-profile' << ext.get('compactProfile').toString()
}
}
}
/*
* -path because gradle will send in paths that don't always exist.
* -missing because we have tons of missing @returns and @param.
@ -657,16 +646,13 @@ class BuildPlugin implements Plugin<Project> {
project.mkdir(heapdumpDir)
project.mkdir(test.workingDir)
project.mkdir(test.workingDir.toPath().resolve('temp'))
if (BuildParams.runtimeJavaVersion >= JavaVersion.VERSION_1_9) {
test.jvmArgs '--illegal-access=warn'
}
//TODO remove once jvm.options are added to test system properties
if (BuildParams.runtimeJavaVersion == JavaVersion.VERSION_1_8) {
test.systemProperty ('java.locale.providers','SPI,JRE')
} else if (BuildParams.runtimeJavaVersion >= JavaVersion.VERSION_1_9) {
test.systemProperty ('java.locale.providers','SPI,COMPAT')
}
test.jvmArgs '--illegal-access=warn'
}
if (inFipsJvm()) {
project.dependencies.add('testRuntimeOnly', "org.bouncycastle:bc-fips:1.0.1")

View File

@ -143,8 +143,6 @@ class PrecommitTasks {
ExportElasticsearchBuildResourcesTask buildResources = project.tasks.getByName('buildResources')
project.tasks.withType(CheckForbiddenApis).configureEach {
dependsOn(buildResources)
doFirst {
// we need to defer this configuration since we don't know the runtime java version until execution time
targetCompatibility = BuildParams.runtimeJavaVersion.majorVersion
if (BuildParams.runtimeJavaVersion > JavaVersion.VERSION_13) {
project.logger.warn(
@ -153,7 +151,6 @@ class PrecommitTasks {
)
targetCompatibility = JavaVersion.VERSION_13.majorVersion
}
}
bundledSignatures = [
"jdk-unsafe", "jdk-deprecated", "jdk-non-portable", "jdk-system-out"
]

View File

@ -26,7 +26,7 @@ import org.elasticsearch.gradle.docker.DockerSupportPlugin;
import org.elasticsearch.gradle.docker.DockerSupportService;
import org.elasticsearch.gradle.info.BuildParams;
import org.elasticsearch.gradle.info.GlobalBuildInfoPlugin;
import org.elasticsearch.gradle.tool.Boilerplate;
import org.elasticsearch.gradle.util.GradleUtils;
import org.gradle.api.GradleException;
import org.gradle.api.NamedDomainObjectContainer;
import org.gradle.api.Plugin;
@ -53,7 +53,7 @@ import java.util.Map;
import java.util.concurrent.Callable;
import java.util.function.Supplier;
import static org.elasticsearch.gradle.Util.capitalize;
import static org.elasticsearch.gradle.util.Util.capitalize;
/**
* A plugin to manage getting and extracting distributions of Elasticsearch.
@ -78,7 +78,7 @@ public class DistributionDownloadPlugin implements Plugin<Project> {
project.getRootProject().getPluginManager().apply(GlobalBuildInfoPlugin.class);
project.getRootProject().getPluginManager().apply(DockerSupportPlugin.class);
Provider<DockerSupportService> dockerSupport = Boilerplate.getBuildService(
Provider<DockerSupportService> dockerSupport = GradleUtils.getBuildService(
project.getGradle().getSharedServices(),
DockerSupportPlugin.DOCKER_SUPPORT_SERVICE_NAME
);

View File

@ -47,8 +47,8 @@ import java.util.Map;
import java.util.concurrent.Callable;
import java.util.stream.StreamSupport;
import static org.elasticsearch.gradle.tool.Boilerplate.findByName;
import static org.elasticsearch.gradle.tool.Boilerplate.maybeCreate;
import static org.elasticsearch.gradle.util.GradleUtils.findByName;
import static org.elasticsearch.gradle.util.GradleUtils.maybeCreate;
public class JdkDownloadPlugin implements Plugin<Project> {

View File

@ -1,293 +0,0 @@
package org.elasticsearch.gradle.info;
import org.elasticsearch.gradle.OS;
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.JavaVersion;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.tasks.CacheableTask;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputDirectory;
import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.TaskAction;
import org.gradle.internal.jvm.Jvm;
import org.gradle.process.ExecResult;
import javax.inject.Inject;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.Writer;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import static java.nio.charset.StandardCharsets.UTF_8;
@CacheableTask
public class GenerateGlobalBuildInfoTask extends DefaultTask {
private JavaVersion minimumCompilerVersion;
private JavaVersion minimumRuntimeVersion;
private File compilerJavaHome;
private File runtimeJavaHome;
private List<JavaHome> javaVersions;
private final RegularFileProperty outputFile;
private final RegularFileProperty compilerVersionFile;
private final RegularFileProperty runtimeVersionFile;
@Inject
public GenerateGlobalBuildInfoTask(ObjectFactory objectFactory) {
this.outputFile = objectFactory.fileProperty();
this.compilerVersionFile = objectFactory.fileProperty();
this.runtimeVersionFile = objectFactory.fileProperty();
}
@Input
public JavaVersion getMinimumCompilerVersion() {
return minimumCompilerVersion;
}
public void setMinimumCompilerVersion(JavaVersion minimumCompilerVersion) {
this.minimumCompilerVersion = minimumCompilerVersion;
}
@Input
public JavaVersion getMinimumRuntimeVersion() {
return minimumRuntimeVersion;
}
public void setMinimumRuntimeVersion(JavaVersion minimumRuntimeVersion) {
this.minimumRuntimeVersion = minimumRuntimeVersion;
}
@InputDirectory
@PathSensitive(PathSensitivity.RELATIVE)
public File getCompilerJavaHome() {
return compilerJavaHome;
}
public void setCompilerJavaHome(File compilerJavaHome) {
this.compilerJavaHome = compilerJavaHome;
}
@InputDirectory
@PathSensitive(PathSensitivity.RELATIVE)
public File getRuntimeJavaHome() {
return runtimeJavaHome;
}
public void setRuntimeJavaHome(File runtimeJavaHome) {
this.runtimeJavaHome = runtimeJavaHome;
}
@Nested
public List<JavaHome> getJavaVersions() {
return javaVersions;
}
public void setJavaVersions(List<JavaHome> javaVersions) {
this.javaVersions = javaVersions;
}
@OutputFile
public RegularFileProperty getOutputFile() {
return outputFile;
}
@OutputFile
public RegularFileProperty getCompilerVersionFile() {
return compilerVersionFile;
}
@OutputFile
public RegularFileProperty getRuntimeVersionFile() {
return runtimeVersionFile;
}
@TaskAction
public void generate() {
String javaVendorVersion = System.getProperty("java.vendor.version", System.getProperty("java.vendor"));
String gradleJavaVersion = System.getProperty("java.version");
String gradleJavaVersionDetails = javaVendorVersion
+ " "
+ gradleJavaVersion
+ " ["
+ System.getProperty("java.vm.name")
+ " "
+ System.getProperty("java.vm.version")
+ "]";
String compilerJavaVersionDetails = gradleJavaVersionDetails;
JavaVersion compilerJavaVersionEnum = JavaVersion.current();
String runtimeJavaVersionDetails = gradleJavaVersionDetails;
JavaVersion runtimeJavaVersionEnum = JavaVersion.current();
File gradleJavaHome = Jvm.current().getJavaHome();
try {
if (Files.isSameFile(compilerJavaHome.toPath(), gradleJavaHome.toPath()) == false) {
if (compilerJavaHome.exists()) {
compilerJavaVersionDetails = findJavaVersionDetails(compilerJavaHome);
compilerJavaVersionEnum = JavaVersion.toVersion(findJavaSpecificationVersion(compilerJavaHome));
} else {
throw new RuntimeException("Compiler Java home path of '" + compilerJavaHome + "' does not exist");
}
}
if (Files.isSameFile(runtimeJavaHome.toPath(), gradleJavaHome.toPath()) == false) {
if (runtimeJavaHome.exists()) {
runtimeJavaVersionDetails = findJavaVersionDetails(runtimeJavaHome);
runtimeJavaVersionEnum = JavaVersion.toVersion(findJavaSpecificationVersion(runtimeJavaHome));
} else {
throw new RuntimeException("Runtime Java home path of '" + compilerJavaHome + "' does not exist");
}
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile.getAsFile().get()))) {
final String osName = System.getProperty("os.name");
final String osVersion = System.getProperty("os.version");
final String osArch = System.getProperty("os.arch");
final JavaVersion parsedVersion = JavaVersion.toVersion(gradleJavaVersion);
writer.write(" Gradle Version : " + getProject().getGradle().getGradleVersion() + "\n");
writer.write(" OS Info : " + osName + " " + osVersion + " (" + osArch + ")\n");
if (gradleJavaVersionDetails.equals(compilerJavaVersionDetails) == false
|| gradleJavaVersionDetails.equals(runtimeJavaVersionDetails) == false) {
writer.write(" Compiler JDK Version : " + compilerJavaVersionEnum + " (" + compilerJavaVersionDetails + ")\n");
writer.write(" Compiler java.home : " + compilerJavaHome + "\n");
writer.write(" Runtime JDK Version : " + runtimeJavaVersionEnum + " (" + runtimeJavaVersionDetails + ")\n");
writer.write(" Runtime java.home : " + runtimeJavaHome + "\n");
writer.write(" Gradle JDK Version : " + parsedVersion + " (" + gradleJavaVersionDetails + ")\n");
writer.write(" Gradle java.home : " + gradleJavaHome);
} else {
writer.write(" JDK Version : " + parsedVersion + " (" + gradleJavaVersionDetails + ")\n");
writer.write(" JAVA_HOME : " + gradleJavaHome);
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
// enforce Java version
if (compilerJavaVersionEnum.compareTo(minimumCompilerVersion) < 0) {
String message = String.format(
Locale.ROOT,
"The compiler java.home must be set to a JDK installation directory for Java %s but is [%s] " + "corresponding to [%s]",
minimumCompilerVersion,
compilerJavaHome,
compilerJavaVersionEnum
);
throw new GradleException(message);
}
if (runtimeJavaVersionEnum.compareTo(minimumRuntimeVersion) < 0) {
String message = String.format(
Locale.ROOT,
"The runtime java.home must be set to a JDK installation directory for Java %s but is [%s] " + "corresponding to [%s]",
minimumRuntimeVersion,
runtimeJavaHome,
runtimeJavaVersionEnum
);
throw new GradleException(message);
}
for (JavaHome javaVersion : javaVersions) {
File javaHome = javaVersion.getJavaHome();
if (javaHome == null) {
continue;
}
JavaVersion javaVersionEnum = JavaVersion.toVersion(findJavaSpecificationVersion(javaHome));
JavaVersion expectedJavaVersionEnum;
int version = javaVersion.getVersion();
if (version < 9) {
expectedJavaVersionEnum = JavaVersion.toVersion("1." + version);
} else {
expectedJavaVersionEnum = JavaVersion.toVersion(Integer.toString(version));
}
if (javaVersionEnum != expectedJavaVersionEnum) {
String message = String.format(
Locale.ROOT,
"The environment variable JAVA%d_HOME must be set to a JDK installation directory for Java"
+ " %s but is [%s] corresponding to [%s]",
version,
expectedJavaVersionEnum,
javaHome,
javaVersionEnum
);
throw new GradleException(message);
}
}
writeToFile(compilerVersionFile.getAsFile().get(), compilerJavaVersionEnum.name());
writeToFile(runtimeVersionFile.getAsFile().get(), runtimeJavaVersionEnum.name());
}
private void writeToFile(File file, String content) {
try (Writer writer = new FileWriter(file)) {
writer.write(content);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
/**
* Finds printable java version of the given JAVA_HOME
*/
private String findJavaVersionDetails(File javaHome) {
String versionInfoScript = "print("
+ "java.lang.System.getProperty(\"java.vendor.version\", 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 runJavaAsScript(javaHome, versionInfoScript).trim();
}
/**
* Finds the parsable java specification version
*/
private String findJavaSpecificationVersion(File javaHome) {
String versionScript = "print(java.lang.System.getProperty(\"java.specification.version\"));";
return runJavaAsScript(javaHome, versionScript);
}
/**
* Runs the given javascript using jjs from the jdk, and returns the output
*/
private String runJavaAsScript(File javaHome, String script) {
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
ByteArrayOutputStream stderr = new ByteArrayOutputStream();
if (OS.current() == OS.WINDOWS) {
// gradle/groovy does not properly escape the double quote for windows
script = script.replace("\"", "\\\"");
}
File jrunscriptPath = new File(javaHome, "bin/jrunscript");
String finalScript = script;
ExecResult result = getProject().exec(spec -> {
spec.setExecutable(jrunscriptPath);
spec.args("-e", finalScript);
spec.setStandardOutput(stdout);
spec.setErrorOutput(stderr);
spec.setIgnoreExitValue(true);
});
if (result.getExitValue() != 0) {
getLogger().error("STDOUT:");
Arrays.stream(stdout.toString(UTF_8).split(System.getProperty("line.separator"))).forEach(getLogger()::error);
getLogger().error("STDERR:");
Arrays.stream(stderr.toString(UTF_8).split(System.getProperty("line.separator"))).forEach(getLogger()::error);
result.rethrowFailure();
}
return stdout.toString(UTF_8).trim();
}
}

View File

@ -1,18 +1,27 @@
package org.elasticsearch.gradle.info;
import org.elasticsearch.gradle.OS;
import org.elasticsearch.gradle.util.Util;
import org.gradle.api.GradleException;
import org.gradle.api.JavaVersion;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.Provider;
import org.gradle.api.provider.ProviderFactory;
import org.gradle.internal.jvm.Jvm;
import org.gradle.jvm.toolchain.JavaInstallation;
import org.gradle.jvm.toolchain.JavaInstallationRegistry;
import org.gradle.util.GradleVersion;
import javax.inject.Inject;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
@ -33,23 +42,146 @@ import java.util.stream.Collectors;
import java.util.stream.Stream;
public class GlobalBuildInfoPlugin implements Plugin<Project> {
private static final String GLOBAL_INFO_EXTENSION_NAME = "globalInfo";
private static final Logger LOGGER = Logging.getLogger(GlobalBuildInfoPlugin.class);
private static Integer _defaultParallel = null;
private final JavaInstallationRegistry javaInstallationRegistry;
private final ObjectFactory objects;
private final ProviderFactory providers;
@Inject
public GlobalBuildInfoPlugin(JavaInstallationRegistry javaInstallationRegistry, ObjectFactory objects, ProviderFactory providers) {
this.javaInstallationRegistry = javaInstallationRegistry;
this.objects = objects;
this.providers = providers;
}
@Override
public void apply(Project project) {
if (project != project.getRootProject()) {
throw new IllegalStateException(this.getClass().getName() + " can only be applied to the root project.");
}
GlobalInfoExtension extension = project.getExtensions().create(GLOBAL_INFO_EXTENSION_NAME, GlobalInfoExtension.class);
JavaVersion minimumCompilerVersion = JavaVersion.toVersion(getResourceContents("/minimumCompilerVersion"));
JavaVersion minimumRuntimeVersion = JavaVersion.toVersion(getResourceContents("/minimumRuntimeVersion"));
JavaVersion minimumCompilerVersion = JavaVersion.toVersion(Util.getResourceContents("/minimumCompilerVersion"));
JavaVersion minimumRuntimeVersion = JavaVersion.toVersion(Util.getResourceContents("/minimumRuntimeVersion"));
File compilerJavaHome = findCompilerJavaHome();
File runtimeJavaHome = findRuntimeJavaHome(compilerJavaHome);
// Initialize global build parameters
BuildParams.init(params -> {
params.reset();
params.setCompilerJavaHome(compilerJavaHome);
params.setRuntimeJavaHome(runtimeJavaHome);
params.setCompilerJavaVersion(determineJavaVersion("compiler java.home", compilerJavaHome, minimumCompilerVersion));
params.setRuntimeJavaVersion(determineJavaVersion("runtime java.home", runtimeJavaHome, minimumRuntimeVersion));
params.setIsRutimeJavaHomeSet(compilerJavaHome.equals(runtimeJavaHome) == false);
params.setJavaVersions(getAvailableJavaVersions(minimumCompilerVersion));
params.setMinimumCompilerVersion(minimumCompilerVersion);
params.setMinimumRuntimeVersion(minimumRuntimeVersion);
params.setGradleJavaVersion(Jvm.current().getJavaVersion());
params.setGitRevision(gitRevision(project.getRootProject().getRootDir()));
params.setBuildDate(ZonedDateTime.now(ZoneOffset.UTC));
params.setTestSeed(getTestSeed());
params.setIsCi(System.getenv("JENKINS_URL") != null);
params.setIsInternal(GlobalBuildInfoPlugin.class.getResource("/buildSrc.marker") != null);
params.setDefaultParallel(findDefaultParallel(project));
params.setInFipsJvm(Util.getBooleanProperty("tests.fips.enabled", false));
params.setIsSnapshotBuild(Util.getBooleanProperty("build.snapshot", true));
});
// Print global build info header just before task execution
project.getGradle().getTaskGraph().whenReady(graph -> logGlobalBuildInfo());
}
private void logGlobalBuildInfo() {
final String osName = System.getProperty("os.name");
final String osVersion = System.getProperty("os.version");
final String osArch = System.getProperty("os.arch");
final Jvm gradleJvm = Jvm.current();
final String gradleJvmDetails = getJavaInstallation(gradleJvm.getJavaHome()).getImplementationName();
LOGGER.quiet("=======================================");
LOGGER.quiet("Elasticsearch Build Hamster says Hello!");
LOGGER.quiet(" Gradle Version : " + GradleVersion.current().getVersion());
LOGGER.quiet(" OS Info : " + osName + " " + osVersion + " (" + osArch + ")");
if (Jvm.current().getJavaVersion().equals(BuildParams.getCompilerJavaVersion()) == false || BuildParams.getIsRuntimeJavaHomeSet()) {
String compilerJvmDetails = getJavaInstallation(BuildParams.getCompilerJavaHome()).getImplementationName();
String runtimeJvmDetails = getJavaInstallation(BuildParams.getRuntimeJavaHome()).getImplementationName();
LOGGER.quiet(" Compiler JDK Version : " + BuildParams.getCompilerJavaVersion() + " (" + compilerJvmDetails + ")");
LOGGER.quiet(" Compiler java.home : " + BuildParams.getCompilerJavaHome());
LOGGER.quiet(" Runtime JDK Version : " + BuildParams.getRuntimeJavaVersion() + " (" + runtimeJvmDetails + ")");
LOGGER.quiet(" Runtime java.home : " + BuildParams.getRuntimeJavaHome());
LOGGER.quiet(" Gradle JDK Version : " + gradleJvm.getJavaVersion() + " (" + gradleJvmDetails + ")");
LOGGER.quiet(" Gradle java.home : " + gradleJvm.getJavaHome());
} else {
LOGGER.quiet(" JDK Version : " + gradleJvm.getJavaVersion() + " (" + gradleJvmDetails + ")");
LOGGER.quiet(" JAVA_HOME : " + gradleJvm.getJavaHome());
}
LOGGER.quiet(" Random Testing Seed : " + BuildParams.getTestSeed());
LOGGER.quiet(" In FIPS 140 mode : " + BuildParams.isInFipsJvm());
LOGGER.quiet("=======================================");
}
private JavaVersion determineJavaVersion(String description, File javaHome, JavaVersion requiredVersion) {
JavaInstallation installation = getJavaInstallation(javaHome);
JavaVersion actualVersion = installation.getJavaVersion();
if (actualVersion.isCompatibleWith(requiredVersion) == false) {
throwInvalidJavaHomeException(
description,
javaHome,
Integer.parseInt(requiredVersion.getMajorVersion()),
Integer.parseInt(actualVersion.getMajorVersion())
);
}
return actualVersion;
}
private JavaInstallation getJavaInstallation(File javaHome) {
JavaInstallation installation;
if (isCurrentJavaHome(javaHome)) {
installation = javaInstallationRegistry.getInstallationForCurrentVirtualMachine().get();
} else {
installation = javaInstallationRegistry.installationForDirectory(objects.directoryProperty().fileValue(javaHome)).get();
}
return installation;
}
private List<JavaHome> getAvailableJavaVersions(JavaVersion minimumCompilerVersion) {
final List<JavaHome> javaVersions = new ArrayList<>();
for (int v = 8; v <= Integer.parseInt(minimumCompilerVersion.getMajorVersion()); v++) {
int version = v;
String javaHomeEnvVarName = getJavaHomeEnvVarName(Integer.toString(version));
if (System.getenv(javaHomeEnvVarName) != null) {
File javaHomeDirectory = new File(findJavaHome(Integer.toString(version)));
Provider<JavaInstallation> javaInstallationProvider = javaInstallationRegistry.installationForDirectory(
objects.directoryProperty().fileValue(javaHomeDirectory)
);
JavaHome javaHome = JavaHome.of(version, providers.provider(() -> {
int actualVersion = Integer.parseInt(javaInstallationProvider.get().getJavaVersion().getMajorVersion());
if (actualVersion != version) {
throwInvalidJavaHomeException("env variable " + javaHomeEnvVarName, javaHomeDirectory, version, actualVersion);
}
return javaHomeDirectory;
}));
javaVersions.add(javaHome);
}
}
return javaVersions;
}
private static boolean isCurrentJavaHome(File javaHome) {
try {
return Files.isSameFile(javaHome.toPath(), Jvm.current().getJavaHome().toPath());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
private static String getTestSeed() {
String testSeedProperty = System.getProperty("tests.seed");
final String testSeed;
if (testSeedProperty == null) {
@ -58,77 +190,20 @@ public class GlobalBuildInfoPlugin implements Plugin<Project> {
} else {
testSeed = testSeedProperty;
}
return testSeed;
}
final String buildSnapshotSystemProperty = System.getProperty("build.snapshot", "true");
final boolean isSnapshotBuild;
switch (buildSnapshotSystemProperty) {
case "true":
isSnapshotBuild = true;
break;
case "false":
isSnapshotBuild = false;
break;
default:
throw new IllegalArgumentException(
"build.snapshot was set to [" + buildSnapshotSystemProperty + "] but can only be unset or [true|false]"
private static void throwInvalidJavaHomeException(String description, File javaHome, int expectedVersion, int actualVersion) {
String message = String.format(
Locale.ROOT,
"The %s must be set to a JDK installation directory for Java %d but is [%s] corresponding to [%s]",
description,
expectedVersion,
javaHome,
actualVersion
);
}
final List<JavaHome> javaVersions = new ArrayList<>();
for (int version = 8; version <= Integer.parseInt(minimumCompilerVersion.getMajorVersion()); version++) {
if (System.getenv(getJavaHomeEnvVarName(Integer.toString(version))) != null) {
javaVersions.add(JavaHome.of(version, new File(findJavaHome(Integer.toString(version)))));
}
}
GenerateGlobalBuildInfoTask generateTask = project.getTasks()
.create("generateGlobalBuildInfo", GenerateGlobalBuildInfoTask.class, task -> {
task.setJavaVersions(javaVersions);
task.setMinimumCompilerVersion(minimumCompilerVersion);
task.setMinimumRuntimeVersion(minimumRuntimeVersion);
task.setCompilerJavaHome(compilerJavaHome);
task.setRuntimeJavaHome(runtimeJavaHome);
task.getOutputFile().set(new File(project.getBuildDir(), "global-build-info"));
task.getCompilerVersionFile().set(new File(project.getBuildDir(), "java-compiler-version"));
task.getRuntimeVersionFile().set(new File(project.getBuildDir(), "java-runtime-version"));
});
PrintGlobalBuildInfoTask printTask = project.getTasks().create("printGlobalBuildInfo", PrintGlobalBuildInfoTask.class, task -> {
task.getBuildInfoFile().set(generateTask.getOutputFile());
task.getCompilerVersionFile().set(generateTask.getCompilerVersionFile());
task.getRuntimeVersionFile().set(generateTask.getRuntimeVersionFile());
task.setGlobalInfoListeners(extension.listeners);
});
// Initialize global build parameters
BuildParams.init(params -> {
params.reset();
params.setCompilerJavaHome(compilerJavaHome);
params.setRuntimeJavaHome(runtimeJavaHome);
params.setIsRutimeJavaHomeSet(compilerJavaHome.equals(runtimeJavaHome) == false);
params.setJavaVersions(javaVersions);
params.setMinimumCompilerVersion(minimumCompilerVersion);
params.setMinimumRuntimeVersion(minimumRuntimeVersion);
params.setGradleJavaVersion(Jvm.current().getJavaVersion());
params.setGitRevision(gitRevision(project.getRootProject().getRootDir()));
params.setBuildDate(ZonedDateTime.now(ZoneOffset.UTC));
params.setTestSeed(testSeed);
params.setIsCi(System.getenv("JENKINS_URL") != null);
params.setIsInternal(GlobalBuildInfoPlugin.class.getResource("/buildSrc.marker") != null);
params.setDefaultParallel(findDefaultParallel(project));
params.setInFipsJvm(isInFipsJvm());
params.setIsSnapshotBuild(isSnapshotBuild);
});
project.allprojects(
p -> {
// Make sure than any task execution generates and prints build info
p.getTasks().configureEach(task -> {
if (task != generateTask && task != printTask) {
task.dependsOn(printTask);
}
});
}
);
throw new GradleException(message);
}
private static File findCompilerJavaHome() {
@ -174,28 +249,6 @@ public class GlobalBuildInfoPlugin implements Plugin<Project> {
return "JAVA" + version + "_HOME";
}
private static boolean isInFipsJvm() {
return Boolean.parseBoolean(System.getProperty("tests.fips.enabled"));
}
private static String getResourceContents(String resourcePath) {
try (
BufferedReader reader = new BufferedReader(new InputStreamReader(GlobalBuildInfoPlugin.class.getResourceAsStream(resourcePath)))
) {
StringBuilder b = new StringBuilder();
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
if (b.length() != 0) {
b.append('\n');
}
b.append(line);
}
return b.toString();
} catch (IOException e) {
throw new UncheckedIOException("Error trying to read classpath resource: " + resourcePath, e);
}
}
private static int findDefaultParallel(Project project) {
// Since it costs IO to compute this, and is done at configuration time we want to cache this if possible
// It's safe to store this in a static variable since it's just a primitive so leaking memory isn't an issue

View File

@ -1,12 +0,0 @@
package org.elasticsearch.gradle.info;
import java.util.ArrayList;
import java.util.List;
public class GlobalInfoExtension {
final List<Runnable> listeners = new ArrayList<>();
public void ready(Runnable block) {
listeners.add(block);
}
}

View File

@ -1,75 +0,0 @@
package org.elasticsearch.gradle.info;
import org.gradle.api.DefaultTask;
import org.gradle.api.JavaVersion;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.resources.TextResource;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.TaskAction;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
public class PrintGlobalBuildInfoTask extends DefaultTask {
private final RegularFileProperty buildInfoFile;
private final RegularFileProperty compilerVersionFile;
private final RegularFileProperty runtimeVersionFile;
private List<Runnable> globalInfoListeners = new ArrayList<>();
@Inject
public PrintGlobalBuildInfoTask(ObjectFactory objectFactory) {
this.buildInfoFile = objectFactory.fileProperty();
this.compilerVersionFile = objectFactory.fileProperty();
this.runtimeVersionFile = objectFactory.fileProperty();
}
@InputFile
public RegularFileProperty getBuildInfoFile() {
return buildInfoFile;
}
@InputFile
public RegularFileProperty getCompilerVersionFile() {
return compilerVersionFile;
}
@InputFile
public RegularFileProperty getRuntimeVersionFile() {
return runtimeVersionFile;
}
public void setGlobalInfoListeners(List<Runnable> globalInfoListeners) {
this.globalInfoListeners = globalInfoListeners;
}
@TaskAction
public void print() {
getLogger().quiet("=======================================");
getLogger().quiet("Elasticsearch Build Hamster says Hello!");
getLogger().quiet(getFileText(getBuildInfoFile()).asString());
getLogger().quiet(" Random Testing Seed : " + BuildParams.getTestSeed());
getLogger().quiet(" In FIPS 140 mode : " + BuildParams.isInFipsJvm());
getLogger().quiet("=======================================");
setGlobalProperties();
globalInfoListeners.forEach(Runnable::run);
// Since all tasks depend on this task, and it always runs for every build, this makes sure that lifecycle tasks will still
// correctly report as UP-TO-DATE, since the convention is a lifecycle task (i.e. assemble, build, etc) will only be marked as
// UP-TO-DATE if all upstream tasks were also UP-TO-DATE.
setDidWork(false);
}
private TextResource getFileText(RegularFileProperty regularFileProperty) {
return getProject().getResources().getText().fromFile(regularFileProperty.getAsFile().get());
}
private void setGlobalProperties() {
BuildParams.init(params -> {
params.setCompilerJavaVersion(JavaVersion.valueOf(getFileText(getCompilerVersionFile()).asString()));
params.setRuntimeJavaVersion(JavaVersion.valueOf(getFileText(getRuntimeVersionFile()).asString()));
});
}
}

View File

@ -28,7 +28,7 @@ import java.util.Set;
import java.util.stream.Collectors;
import org.apache.tools.ant.taskdefs.condition.Os;
import org.elasticsearch.gradle.tool.Boilerplate;
import org.elasticsearch.gradle.util.GradleUtils;
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.file.FileCollection;
@ -80,7 +80,7 @@ public class FilePermissionsTask extends DefaultTask {
@InputFiles
@SkipWhenEmpty
public FileCollection getFiles() {
return Boilerplate.getJavaSourceSets(getProject())
return GradleUtils.getJavaSourceSets(getProject())
.stream()
.map(sourceSet -> sourceSet.getAllSource().matching(filesFilter))
.reduce(FileTree::plus)

View File

@ -19,7 +19,7 @@
package org.elasticsearch.gradle.precommit;
import groovy.lang.Closure;
import org.elasticsearch.gradle.tool.Boilerplate;
import org.elasticsearch.gradle.util.GradleUtils;
import org.gradle.api.DefaultTask;
import org.gradle.api.NamedDomainObjectContainer;
import org.gradle.api.Task;
@ -65,7 +65,7 @@ public class TestingConventionsTasks extends DefaultTask {
public TestingConventionsTasks() {
setDescription("Tests various testing conventions");
// Run only after everything is compiled
Boilerplate.getJavaSourceSets(getProject()).all(sourceSet -> dependsOn(sourceSet.getOutput().getClassesDirs()));
GradleUtils.getJavaSourceSets(getProject()).all(sourceSet -> dependsOn(sourceSet.getOutput().getClassesDirs()));
naming = getProject().container(TestingConventionRule.class);
}
@ -81,7 +81,7 @@ public class TestingConventionsTasks extends DefaultTask {
@Input
public Map<String, File> getTestClassNames() {
if (testClassNames == null) {
testClassNames = Boilerplate.getJavaSourceSets(getProject())
testClassNames = GradleUtils.getJavaSourceSets(getProject())
.getByName("test")
.getOutput()
.getClassesDirs()
@ -110,7 +110,7 @@ public class TestingConventionsTasks extends DefaultTask {
@Input
public Set<String> getMainClassNamedLikeTests() {
SourceSetContainer javaSourceSets = Boilerplate.getJavaSourceSets(getProject());
SourceSetContainer javaSourceSets = GradleUtils.getJavaSourceSets(getProject());
if (javaSourceSets.findByName(SourceSet.MAIN_SOURCE_SET_NAME) == null) {
// some test projects don't have a main source set
return Collections.emptySet();
@ -351,7 +351,7 @@ public class TestingConventionsTasks extends DefaultTask {
// running the tests.
return getProject().files(
getProject().getConfigurations().getByName("testRuntime").resolve(),
Boilerplate.getJavaSourceSets(getProject())
GradleUtils.getJavaSourceSets(getProject())
.stream()
.flatMap(sourceSet -> sourceSet.getOutput().getClassesDirs().getFiles().stream())
.collect(Collectors.toList())

View File

@ -32,7 +32,7 @@ import org.elasticsearch.gradle.VersionProperties;
import org.elasticsearch.gradle.docker.DockerSupportPlugin;
import org.elasticsearch.gradle.docker.DockerSupportService;
import org.elasticsearch.gradle.info.BuildParams;
import org.elasticsearch.gradle.tool.Boilerplate;
import org.elasticsearch.gradle.util.GradleUtils;
import org.elasticsearch.gradle.vagrant.BatsProgressLogger;
import org.elasticsearch.gradle.vagrant.VagrantBasePlugin;
import org.elasticsearch.gradle.vagrant.VagrantExtension;
@ -88,7 +88,7 @@ public class DistroTestPlugin implements Plugin<Project> {
project.getPluginManager().apply(DistributionDownloadPlugin.class);
project.getPluginManager().apply("elasticsearch.build");
Provider<DockerSupportService> dockerSupport = Boilerplate.getBuildService(
Provider<DockerSupportService> dockerSupport = GradleUtils.getBuildService(
project.getGradle().getSharedServices(),
DockerSupportPlugin.DOCKER_SUPPORT_SERVICE_NAME
);

View File

@ -20,7 +20,7 @@ package org.elasticsearch.gradle.test.rest;
import org.elasticsearch.gradle.VersionProperties;
import org.elasticsearch.gradle.info.BuildParams;
import org.elasticsearch.gradle.tool.Boilerplate;
import org.elasticsearch.gradle.util.GradleUtils;
import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
@ -203,6 +203,6 @@ public class CopyRestApiTask extends DefaultTask {
}
private SourceSet getTestSourceSet() {
return Boilerplate.getJavaSourceSets(getProject()).findByName("test");
return GradleUtils.getJavaSourceSets(getProject()).findByName("test");
}
}

View File

@ -20,7 +20,7 @@ package org.elasticsearch.gradle.test.rest;
import org.elasticsearch.gradle.VersionProperties;
import org.elasticsearch.gradle.info.BuildParams;
import org.elasticsearch.gradle.tool.Boilerplate;
import org.elasticsearch.gradle.util.GradleUtils;
import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
@ -146,6 +146,6 @@ public class CopyRestTestsTask extends DefaultTask {
}
private SourceSet getTestSourceSet() {
return Boilerplate.getJavaSourceSets(getProject()).findByName("test");
return GradleUtils.getJavaSourceSets(getProject()).findByName("test");
}
}

View File

@ -1,6 +1,6 @@
package org.elasticsearch.gradle.testclusters;
import org.elasticsearch.gradle.tool.Boilerplate;
import org.elasticsearch.gradle.util.GradleUtils;
import org.gradle.api.provider.Provider;
import org.gradle.api.services.internal.BuildServiceRegistryInternal;
import org.gradle.api.tasks.CacheableTask;
@ -62,7 +62,7 @@ public class RestTestRunnerTask extends Test implements TestClustersAware {
public List<ResourceLock> getSharedResources() {
List<ResourceLock> locks = new ArrayList<>(super.getSharedResources());
BuildServiceRegistryInternal serviceRegistry = getServices().get(BuildServiceRegistryInternal.class);
Provider<TestClustersThrottle> throttleProvider = Boilerplate.getBuildService(serviceRegistry, THROTTLE_SERVICE_NAME);
Provider<TestClustersThrottle> throttleProvider = GradleUtils.getBuildService(serviceRegistry, THROTTLE_SERVICE_NAME);
SharedResource resource = serviceRegistry.forService(throttleProvider);
int nodeCount = clusters.stream().mapToInt(cluster -> cluster.getNodes().size()).sum();

View File

@ -25,7 +25,7 @@ import org.elasticsearch.gradle.JdkDownloadPlugin;
import org.elasticsearch.gradle.OS;
import org.elasticsearch.gradle.ReaperPlugin;
import org.elasticsearch.gradle.ReaperService;
import org.elasticsearch.gradle.tool.Boilerplate;
import org.elasticsearch.gradle.util.GradleUtils;
import org.gradle.api.NamedDomainObjectContainer;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
@ -40,7 +40,7 @@ import org.gradle.api.tasks.TaskState;
import java.io.File;
import static org.elasticsearch.gradle.tool.Boilerplate.noop;
import static org.elasticsearch.gradle.util.GradleUtils.noop;
public class TestClustersPlugin implements Plugin<Project> {
@ -121,7 +121,7 @@ public class TestClustersPlugin implements Plugin<Project> {
throw new IllegalStateException(this.getClass().getName() + " can only be applied to the root project.");
}
Provider<TestClustersRegistry> registryProvider = Boilerplate.getBuildService(
Provider<TestClustersRegistry> registryProvider = GradleUtils.getBuildService(
project.getGradle().getSharedServices(),
REGISTRY_SERVICE_NAME
);

View File

@ -29,7 +29,7 @@ import org.elasticsearch.gradle.docker.DockerSupportPlugin;
import org.elasticsearch.gradle.docker.DockerSupportService;
import org.elasticsearch.gradle.info.BuildParams;
import org.elasticsearch.gradle.precommit.TestingConventionsTasks;
import org.elasticsearch.gradle.tool.Boilerplate;
import org.elasticsearch.gradle.util.GradleUtils;
import org.gradle.api.Action;
import org.gradle.api.DefaultTask;
import org.gradle.api.Plugin;
@ -67,7 +67,7 @@ public class TestFixturesPlugin implements Plugin<Project> {
.getSharedServices()
.registerIfAbsent(DOCKER_COMPOSE_THROTTLE, DockerComposeThrottle.class, spec -> spec.getMaxParallelUsages().set(1));
Provider<DockerSupportService> dockerSupport = Boilerplate.getBuildService(
Provider<DockerSupportService> dockerSupport = GradleUtils.getBuildService(
project.getGradle().getSharedServices(),
DockerSupportPlugin.DOCKER_SUPPORT_SERVICE_NAME
);

View File

@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.gradle.tool;
package org.elasticsearch.gradle.util;
import org.gradle.api.Action;
import org.gradle.api.GradleException;
@ -36,7 +36,7 @@ import org.gradle.api.tasks.TaskProvider;
import java.util.Optional;
public abstract class Boilerplate {
public abstract class GradleUtils {
public static <T> Action<T> noop() {
return t -> {};

View File

@ -17,10 +17,15 @@
* under the License.
*/
package org.elasticsearch.gradle;
package org.elasticsearch.gradle.util;
import org.elasticsearch.gradle.info.GlobalBuildInfoPlugin;
import org.gradle.api.GradleException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.util.Locale;
public class Util {
@ -39,6 +44,24 @@ public class Util {
}
}
public static String getResourceContents(String resourcePath) {
try (
BufferedReader reader = new BufferedReader(new InputStreamReader(GlobalBuildInfoPlugin.class.getResourceAsStream(resourcePath)))
) {
StringBuilder b = new StringBuilder();
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
if (b.length() != 0) {
b.append('\n');
}
b.append(line);
}
return b.toString();
} catch (IOException e) {
throw new UncheckedIOException("Error trying to read classpath resource: " + resourcePath, e);
}
}
public static String capitalize(String s) {
return s.substring(0, 1).toUpperCase(Locale.ROOT) + s.substring(1);
}

View File

@ -23,7 +23,7 @@ import org.apache.commons.io.output.TeeOutputStream;
import org.elasticsearch.gradle.LoggedExec;
import org.elasticsearch.gradle.LoggingOutputStream;
import org.elasticsearch.gradle.ReaperService;
import org.elasticsearch.gradle.Util;
import org.elasticsearch.gradle.util.Util;
import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.internal.logging.progress.ProgressLogger;

View File

@ -3,11 +3,6 @@ package org.elasticsearch.gradle.info;
import org.gradle.api.JavaVersion;
import java.io.File;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Modifier;
import java.time.ZonedDateTime;
import java.util.Arrays;
@ -75,12 +70,10 @@ public class BuildParams {
return value(gradleJavaVersion);
}
@ExecutionTime
public static JavaVersion getCompilerJavaVersion() {
return value(compilerJavaVersion);
}
@ExecutionTime
public static JavaVersion getRuntimeJavaVersion() {
return value(runtimeJavaVersion);
}
@ -120,22 +113,13 @@ public class BuildParams {
private static <T> T value(T object) {
if (object == null) {
String callingMethod = Thread.currentThread().getStackTrace()[2].getMethodName();
boolean executionTime;
try {
executionTime = BuildParams.class.getMethod(callingMethod).getAnnotation(ExecutionTime.class) != null;
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
String message = "Build parameter '" + propertyName(callingMethod) + "' has not been initialized. ";
if (executionTime) {
message += "This property is initialized at execution time, "
+ "please ensure you are not attempting to access it during project configuration.";
} else {
message += "Perhaps the plugin responsible for initializing this property has not been applied.";
}
throw new IllegalStateException(message);
throw new IllegalStateException(
"Build parameter '"
+ propertyName(callingMethod)
+ "' has not been initialized.\n"
+ "Perhaps the plugin responsible for initializing this property has not been applied."
);
}
return object;
@ -236,14 +220,4 @@ public class BuildParams {
}
}
/**
* Indicates that a build parameter is initialized at task execution time and is not available at project configuration time.
* Attempts to read an uninitialized parameter wil result in an {@link IllegalStateException}.
*/
@Target({ ElementType.METHOD, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ExecutionTime {
}
}

View File

@ -1,35 +1,27 @@
package org.elasticsearch.gradle.info;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputDirectory;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.provider.Provider;
import java.io.File;
public class JavaHome {
private Integer version;
private File javaHome;
private Provider<File> javaHome;
private JavaHome(int version, File javaHome) {
private JavaHome(int version, Provider<File> javaHome) {
this.version = version;
this.javaHome = javaHome;
}
public static JavaHome of(int version, File javaHome) {
public static JavaHome of(int version, Provider<File> javaHome) {
return new JavaHome(version, javaHome);
}
@Input
public Integer getVersion() {
return version;
}
@InputDirectory
@Optional
@PathSensitive(PathSensitivity.RELATIVE)
public File getJavaHome() {
public Provider<File> getJavaHome() {
return javaHome;
}
}

View File

@ -49,13 +49,11 @@ if (!isEclipse) {
}
forbiddenApisJava9 {
rootProject.globalInfo.ready {
if (BuildParams.runtimeJavaVersion < JavaVersion.VERSION_1_9) {
targetCompatibility = JavaVersion.VERSION_1_9.getMajorVersion()
}
replaceSignatureFiles 'jdk-signatures'
}
}
jar {
metaInf {

View File

@ -142,8 +142,7 @@ thirdPartyAudit.ignoreMissingClasses(
)
// jarhell with jdk (intentionally, because jaxb was removed from default modules in java 9)
rootProject.globalInfo.ready {
if (BuildParams.runtimeJavaVersion <= JavaVersion.VERSION_1_8) {
if (BuildParams.runtimeJavaVersion <= JavaVersion.VERSION_1_8) {
thirdPartyAudit.ignoreJarHellWithJDK(
'javax.xml.bind.Binder',
'javax.xml.bind.ContextFinder$1',
@ -247,7 +246,7 @@ rootProject.globalInfo.ready {
'javax.xml.bind.util.Messages',
'javax.xml.bind.util.ValidationEventCollector'
)
} else {
} else {
thirdPartyAudit.ignoreMissingClasses(
'javax.activation.ActivationDataFlavor',
'javax.activation.DataContentHandler',
@ -258,5 +257,4 @@ rootProject.globalInfo.ready {
'javax.activation.MimeType',
'javax.activation.MimeTypeParseException',
)
}
}

View File

@ -117,11 +117,9 @@ thirdPartyAudit.ignoreMissingClasses(
'org.apache.log.Logger'
)
rootProject.globalInfo.ready {
if (BuildParams.runtimeJavaVersion > JavaVersion.VERSION_1_8) {
if (BuildParams.runtimeJavaVersion > JavaVersion.VERSION_1_8) {
thirdPartyAudit.ignoreMissingClasses(
'javax.xml.bind.DatatypeConverter',
'javax.xml.bind.JAXBContext'
)
}
}

View File

@ -114,12 +114,10 @@ for (String fixtureName : ['hdfsFixture', 'haHdfsFixture', 'secureHdfsFixture',
// If it's a secure fixture, then depend on Kerberos Fixture and principals + add the krb5conf to the JVM options
if (fixtureName.equals('secureHdfsFixture') || fixtureName.equals('secureHaHdfsFixture')) {
miniHDFSArgs.add("-Djava.security.krb5.conf=${project(':test:fixtures:krb5kdc-fixture').ext.krb5Conf("hdfs")}");
rootProject.globalInfo.ready {
if (BuildParams.runtimeJavaVersion == JavaVersion.VERSION_1_9) {
miniHDFSArgs.add('--add-opens=java.security.jgss/sun.security.krb5=ALL-UNNAMED')
}
}
}
// If it's an HA fixture, set a nameservice to use in the JVM options
if (fixtureName.equals('haHdfsFixture') || fixtureName.equals('secureHaHdfsFixture')) {
miniHDFSArgs.add("-Dha-nameservice=ha-hdfs")

View File

@ -311,8 +311,7 @@ thirdPartyAudit.ignoreMissingClasses(
)
// jarhell with jdk (intentionally, because jaxb was removed from default modules in java 9)
rootProject.globalInfo.ready {
if (BuildParams.runtimeJavaVersion <= JavaVersion.VERSION_1_8) {
if (BuildParams.runtimeJavaVersion <= JavaVersion.VERSION_1_8) {
thirdPartyAudit.ignoreJarHellWithJDK(
'javax.xml.bind.Binder',
'javax.xml.bind.ContextFinder$1',
@ -416,7 +415,6 @@ rootProject.globalInfo.ready {
'javax.xml.bind.util.Messages',
'javax.xml.bind.util.ValidationEventCollector'
)
} else {
} else {
thirdPartyAudit.ignoreMissingClasses 'javax.activation.DataHandler'
}
}

View File

@ -317,10 +317,8 @@ thirdPartyAudit.ignoreMissingClasses(
'com.google.common.geometry.S2LatLng'
)
rootProject.globalInfo.ready {
if (BuildParams.runtimeJavaVersion > JavaVersion.VERSION_1_8) {
if (BuildParams.runtimeJavaVersion > JavaVersion.VERSION_1_8) {
thirdPartyAudit.ignoreMissingClasses 'javax.xml.bind.DatatypeConverter'
}
}
dependencyLicenses {

View File

@ -455,8 +455,7 @@ thirdPartyAudit {
)
}
rootProject.globalInfo.ready {
if (BuildParams.runtimeJavaVersion > JavaVersion.VERSION_1_8) {
if (BuildParams.runtimeJavaVersion > JavaVersion.VERSION_1_8) {
thirdPartyAudit.ignoreMissingClasses(
'javax.xml.bind.JAXBContext',
'javax.xml.bind.JAXBElement',
@ -470,7 +469,6 @@ rootProject.globalInfo.ready {
'javax.activation.FileDataSource',
'javax.activation.FileTypeMap'
)
}
}
test {

View File

@ -84,8 +84,7 @@ forbiddenPatterns {
}
// pulled in as external dependency to work on java 9
rootProject.globalInfo.ready {
if (BuildParams.runtimeJavaVersion <= JavaVersion.VERSION_1_8) {
if (BuildParams.runtimeJavaVersion <= JavaVersion.VERSION_1_8) {
thirdPartyAudit.ignoreJarHellWithJDK(
// pulled in as external dependency to work on java 9
'com.sun.activation.registries.LineTokenizer',
@ -124,7 +123,6 @@ rootProject.globalInfo.ready {
'javax.activation.URLDataSource',
'javax.activation.UnsupportedDataTypeException'
)
}
}
test {

View File

@ -39,11 +39,9 @@ testClusters.integTest {
setting 'reindex.ssl.truststore.password', 'password'
// Workaround for JDK-8212885
rootProject.globalInfo.ready {
if (BuildParams.runtimeJavaVersion.isJava12Compatible() == false) {
setting 'reindex.ssl.supported_protocols', 'TLSv1.2'
}
}
extraConfigFile 'roles.yml', file('roles.yml')
user username: "test_admin", password: 'x-pack-test-password', role: "superuser"

View File

@ -181,8 +181,7 @@ thirdPartyAudit.ignoreMissingClasses(
'javax.servlet.ServletContextListener'
)
rootProject.globalInfo.ready {
if (BuildParams.runtimeJavaVersion <= JavaVersion.VERSION_1_8) {
if (BuildParams.runtimeJavaVersion <= JavaVersion.VERSION_1_8) {
thirdPartyAudit.ignoreJarHellWithJDK(
'javax.xml.bind.Binder',
'javax.xml.bind.ContextFinder$1',
@ -286,9 +285,8 @@ rootProject.globalInfo.ready {
'javax.xml.bind.util.Messages',
'javax.xml.bind.util.ValidationEventCollector'
)
} else {
} else {
thirdPartyAudit.ignoreMissingClasses 'javax.activation.DataHandler'
}
}
thirdPartyAudit.ignoreViolations(