Skip to content

Commit

Permalink
Use bundled jdk as default runtime jdk instead of build jvm (elastic#…
Browse files Browse the repository at this point in the history
…111197)

* Fallback to bundled jdk for runtime jdk instead of build jvm
* Rework bundled jdk resolution to be lazy
* Use fixed runtime jdk for gradle build integ tests
  • Loading branch information
breskeby authored Jul 31, 2024
1 parent 3090438 commit 1a52716
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,23 @@ abstract class AbstractGradleInternalPluginFuncTest extends AbstractJavaGradleFu
abstract <T extends Plugin> Class<T> getPluginClassUnderTest();

def setup() {
settingsFile.text = """
plugins {
id 'elasticsearch.java-toolchain'
}
""" + settingsFile.text

buildFile << """
import ${getPluginClassUnderTest().getName()}
plugins {
// bring in build-tools-internal onto the classpath
id 'elasticsearch.global-build-info'
}
// internally used plugins do not have a plugin id as they are
// not intended to be used directly from build scripts
// not intended to be used directly from build scripts
plugins.apply(${getPluginClassUnderTest().getSimpleName()})
"""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@ class ElasticsearchJavadocPluginFuncTest extends AbstractGradleFuncTest {
given:
someLibProject()
subProject("some-depending-lib") {
buildFile << """
buildFile << """
plugins {
id 'elasticsearch.java-doc'
id 'java'
}
group = 'org.acme.depending'
dependencies {
implementation project(':some-lib')
}
"""
classFile('org.acme.depending.SomeDepending') << """
package org.acme.depending;
import org.acme.Something;
public class SomeDepending {
public Something createSomething() {
return new Something();
Expand Down Expand Up @@ -66,26 +66,27 @@ class ElasticsearchJavadocPluginFuncTest extends AbstractGradleFuncTest {

def "sources of shadowed dependencies are added to projects javadoc"() {
given:
settingsFile.text = ""
someLibProject() << """version = 1.0"""
subProject("some-depending-lib") {
buildFile << """
buildFile << """
plugins {
id 'elasticsearch.java-doc'
id 'com.github.johnrengelman.shadow' version '7.1.2'
id 'java'
}
group = 'org.acme.depending'
dependencies {
implementation project(':some-lib')
shadow project(':some-shadowed-lib')
}
"""
classFile('org.acme.depending.SomeDepending') << """
package org.acme.depending;
import org.acme.Something;
public class SomeDepending {
public Something createSomething() {
return new Something();
Expand All @@ -94,9 +95,9 @@ class ElasticsearchJavadocPluginFuncTest extends AbstractGradleFuncTest {
"""
classFile('org.acme.depending.SomeShadowedDepending') << """
package org.acme.depending;
import org.acme.shadowed.Shadowed;
public class SomeShadowedDepending {
public Shadowed createShadowed() {
return new Shadowed();
Expand All @@ -114,7 +115,7 @@ class ElasticsearchJavadocPluginFuncTest extends AbstractGradleFuncTest {
"""
classFile('org.acme.shadowed.Shadowed') << """
package org.acme.shadowed;
public class Shadowed {
}
"""
Expand Down Expand Up @@ -145,22 +146,22 @@ class ElasticsearchJavadocPluginFuncTest extends AbstractGradleFuncTest {
tasks.named("javadoc").configure { enabled = false }
"""
subProject("some-depending-lib") {
buildFile << """
buildFile << """
plugins {
id 'elasticsearch.java-doc'
id 'java'
}
group = 'org.acme.depending'
dependencies {
implementation project(':some-lib')
}
"""
classFile('org.acme.depending.SomeDepending') << """
package org.acme.depending;
import org.acme.Something;
public class SomeDepending {
public Something createSomething() {
return new Something();
Expand Down Expand Up @@ -264,7 +265,7 @@ class ElasticsearchJavadocPluginFuncTest extends AbstractGradleFuncTest {

classFile('org.acme.Something') << """
package org.acme;
public class Something {
}
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import org.elasticsearch.gradle.fixtures.AbstractGradleFuncTest
import org.elasticsearch.gradle.fixtures.AbstractGradleInternalPluginFuncTest
import org.elasticsearch.gradle.internal.conventions.precommit.LicenseHeadersPrecommitPlugin
import org.elasticsearch.gradle.internal.conventions.precommit.PrecommitPlugin
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome


Expand Down Expand Up @@ -211,6 +212,10 @@ class ThirdPartyAuditTaskFuncTest extends AbstractGradleInternalPluginFuncTest {
loggingDynamicType.toJar(targetFile(dir("${baseGroupFolderPath}/broken-log4j/0.0.1/"), "broken-log4j-0.0.1.jar"))
}

GradleRunner gradleRunner(Object... arguments) {
return super.gradleRunner(arguments).withEnvironment([RUNTIME_JAVA_HOME: System.getProperty("java.home")])
}

static File targetFile(File dir, String fileName) {
new File(dir, fileName)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import spock.lang.IgnoreIf

import org.elasticsearch.gradle.VersionProperties
import org.elasticsearch.gradle.fixtures.AbstractRestResourcesFuncTest
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome

@IgnoreIf({ os.isWindows() })
Expand Down Expand Up @@ -205,4 +206,8 @@ echo "Running elasticsearch \$0"
}
"""
}

GradleRunner gradleRunner(Object... arguments) {
return super.gradleRunner(arguments).withEnvironment([RUNTIME_JAVA_HOME: System.getProperty("java.home")])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ configure(allprojects) {
JvmVendorSpec.ORACLE :
JvmVendorSpec.matching(VersionProperties.bundledJdkVendor)
}

project.tasks.withType(Test).configureEach { Test test ->
if (BuildParams.getIsRuntimeJavaHomeSet()) {
test.executable = "${BuildParams.runtimeJavaHome}/bin/java" +
Expand All @@ -47,12 +46,4 @@ configure(allprojects) {
}
}
}
project.plugins.withType(ThirdPartyAuditPrecommitPlugin) {
project.getTasks().withType(ThirdPartyAuditTask.class).configureEach {
if (BuildParams.getIsRuntimeJavaHomeSet() == false) {
javaHome.set(launcher.map { it.metadata.installationPath.asFile.path })
targetCompatibility.set(providers.provider(() -> JavaVersion.toVersion(launcher.map { it.metadata.javaRuntimeVersion }.get())))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@
import static java.util.Objects.requireNonNull;

public class BuildParams {
private static File runtimeJavaHome;
private static Provider<File> runtimeJavaHome;
private static Boolean isRuntimeJavaHomeSet;
private static List<JavaHome> javaVersions;
private static JavaVersion minimumCompilerVersion;
private static JavaVersion minimumRuntimeVersion;
private static JavaVersion gradleJavaVersion;
private static JavaVersion runtimeJavaVersion;
private static Provider<JavaVersion> runtimeJavaVersion;
private static Provider<? extends Action<JavaToolchainSpec>> javaToolChainSpec;
private static String runtimeJavaDetails;
private static Provider<String> runtimeJavaDetails;
private static Boolean inFipsJvm;
private static String gitRevision;
private static String gitOrigin;
Expand All @@ -58,7 +58,7 @@ public static void init(Consumer<MutableBuildParams> initializer) {
}

public static File getRuntimeJavaHome() {
return value(runtimeJavaHome);
return value(runtimeJavaHome).get();
}

public static Boolean getIsRuntimeJavaHomeSet() {
Expand All @@ -82,11 +82,11 @@ public static JavaVersion getGradleJavaVersion() {
}

public static JavaVersion getRuntimeJavaVersion() {
return value(runtimeJavaVersion);
return value(runtimeJavaVersion.get());
}

public static String getRuntimeJavaDetails() {
return value(runtimeJavaDetails);
return value(runtimeJavaDetails.get());
}

public static Boolean isInFipsJvm() {
Expand Down Expand Up @@ -126,7 +126,7 @@ public static Boolean isCi() {
}

public static Boolean isGraalVmRuntime() {
return value(runtimeJavaDetails.toLowerCase().contains("graalvm"));
return value(runtimeJavaDetails.get().toLowerCase().contains("graalvm"));
}

public static Integer getDefaultParallel() {
Expand Down Expand Up @@ -182,16 +182,18 @@ public void reset() {
});
}

public void setRuntimeJavaHome(File runtimeJavaHome) {
try {
BuildParams.runtimeJavaHome = requireNonNull(runtimeJavaHome).getCanonicalFile();
} catch (IOException e) {
throw new RuntimeException(e);
}
public void setRuntimeJavaHome(Provider<File> runtimeJavaHome) {
BuildParams.runtimeJavaHome = runtimeJavaHome.map(javaHome -> {
try {
return javaHome.getCanonicalFile();
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}

public void setIsRuntimeJavaHomeSet(boolean isRutimeJavaHomeSet) {
BuildParams.isRuntimeJavaHomeSet = isRutimeJavaHomeSet;
public void setIsRuntimeJavaHomeSet(boolean isRuntimeJavaHomeSet) {
BuildParams.isRuntimeJavaHomeSet = isRuntimeJavaHomeSet;
}

public void setJavaVersions(List<JavaHome> javaVersions) {
Expand All @@ -210,11 +212,11 @@ public void setGradleJavaVersion(JavaVersion gradleJavaVersion) {
BuildParams.gradleJavaVersion = requireNonNull(gradleJavaVersion);
}

public void setRuntimeJavaVersion(JavaVersion runtimeJavaVersion) {
public void setRuntimeJavaVersion(Provider<JavaVersion> runtimeJavaVersion) {
BuildParams.runtimeJavaVersion = requireNonNull(runtimeJavaVersion);
}

public void setRuntimeJavaDetails(String runtimeJavaDetails) {
public void setRuntimeJavaDetails(Provider<String> runtimeJavaDetails) {
BuildParams.runtimeJavaDetails = runtimeJavaDetails;
}

Expand Down
Loading

0 comments on commit 1a52716

Please sign in to comment.