Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Gradle JDK Automanagement] Install only used JDKs #479

Open
wants to merge 31 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion gradle-jdks-setup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ jdks {
jdks {
daemonTarget = <version eg. 11/17/21>
daemonJdkOnly() # optional, only installs & configures the daemon JDK version
jdkVersionsToUse = [11, 17, 21, 23] # optional, the set of java versions to install, by default it will contain all the major versions configured in step 3.
jdkMajorVersionsToUse = [11, 17, 21, 23] # optional, the set of java versions to install, by default it will contain all the major versions configured in step 3.
}
```
5. Enable the jdk setup by adding the following to `gradle.properties`:
Expand Down Expand Up @@ -211,6 +211,29 @@ The plugin registers the following tasks:
- `checkGradleJdkConfigs` - checks that all the `gradle/` configurations are up-to-date. E.g. if the `jdks-latest` plugin is updated, we need to make sure the `gradle/jdks` files reflect the jdk versions.
- `setupJdks` - task that triggers `wrapperJdkPatcher` and `generateGradleJdkConfigs` and runs the patched `./gradlew` script.


## Using a new java version

We are only generating the jdk configuration files for the jdk versions that are used see: [jdkMajorVersionsToUse](https://github.com/palantir/gradle-jdks/blob/26d54605b7827b232024ec7e0c34a87983804492/gradle-jdks/src/main/java/com/palantir/gradle/jdks/JdksExtension.java#L44)
Using a new java version is a 2-step process:
1. Firstly, we need to generate the gradle jdk configuration files for the new java version that we are going to use
2. Secondly, once the new files are generated, the javaVersion can be configured in the gradle build configuration.

### Palantir specific, if using `baseline-java-version` through excavators
1. Run `./gradlew generateGradleJdkConfigs --includeAllJdks`. The command will generate the Gradle jdk configuration files for all jdks the plugin is aware of.
2. Update the javaVersion(s)/jdks extension values with the desired java major version.
3. Run `./gradlew setupJdks` to only keep the Gradle jdk configuration files for the used Gradle JDKs.

### Manual bumps of the java versions
1. Run `./gradlew generateGradleJdkConfigs --includeVersion=<newVersion> [--includeVersion=<otherNewVersion>]`. The command will generate the Gradle jdk configuration files for the major versions passed as parameters.
2. Update the javaVersion(s)/jdks extension values with the desired java major version.
Copy link
Contributor

@CRogers CRogers Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still need to run ./gradlew setupJdks even with this approach as the user may stop using the old version, and we need to remove it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed, I thought I had the logic in the check task to test if there are any extra configuration files in gradle/jdks. I added now that logic in: 1710e98#diff-53f3309ab573fe65d62f8b5bf40e9bb94478d82b4d6434cb6bcf9b25516813a4R134

3. Run `./gradlew setupJdks` to only keep the Gradle jdk configuration files for the used Gradle JDKs.

### Gradle Toolchains workflow
1. Update `jdkMajorVersionsToUse` with the required jdk versions
2. Run `./gradlew setupJdks`


## Unsupported

- This workflow is disabled on `Windows` at the moment.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,25 @@

package com.palantir.gradle.jdks;

import com.google.common.collect.Sets;
import com.palantir.gradle.jdks.setup.common.CurrentArch;
import com.palantir.gradle.jdks.setup.common.CurrentOs;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import org.gradle.api.DefaultTask;
import org.gradle.api.file.Directory;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.MapProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.options.Option;
import org.gradle.jvm.toolchain.JavaLanguageVersion;

/**
Expand All @@ -43,6 +48,22 @@ public abstract class GradleJdksConfigs extends DefaultTask {
public static final String GRADLE_JDKS_SETUP_SCRIPT = "gradle-jdks-setup.sh";
public static final String GRADLE_JDKS_FUNCTIONS_SCRIPT = "gradle-jdks-functions.sh";

@Input
@Option(
option = "includeAllJdks",
description = "Generates the configuration directories for all configured Java major versions.")
public abstract Property<Boolean> getIncludeAllJdks();

@Option(
option = "includeVersion",
description = "Generates the configuration directory for the java major versions passed in as parameters.")
public final void setIncludeVersionsFromCli(List<String> versions) {
getIncludeJavaMajorVersions().set(versions);
}

@Input
public abstract ListProperty<String> getIncludeJavaMajorVersions();

@Nested
public abstract MapProperty<JavaLanguageVersion, List<JdkDistributionConfig>> getJavaVersionToJdkDistros();

Expand All @@ -65,6 +86,11 @@ protected abstract void applyGradleJdkFileAction(

protected abstract void maybePrepareForAction(List<Path> targetPaths);

public GradleJdksConfigs() {
getIncludeAllJdks().convention(false);
getIncludeJavaMajorVersions().convention(List.of());
}

@TaskAction
public final void action() {
Path gradleJdksDir = gradleDirectory().dir("jdks").getAsFile().toPath();
Expand Down Expand Up @@ -99,6 +125,19 @@ public final void action() {
+ " the readme: https://github.com/palantir/gradle-jdks#usage");
}

Set<String> configuredJavaMajorVersions = GradleJdksConfigsUtils.getConfiguredJavaMajorVersions(gradleJdksDir);
Set<String> unexpectedConfiguredJavaVersions = Sets.difference(
configuredJavaMajorVersions,
getJavaVersionToJdkDistros().get().keySet().stream()
.map(JavaLanguageVersion::toString)
.collect(Collectors.toSet()));
if (!unexpectedConfiguredJavaVersions.isEmpty()) {
throw new RuntimeException(String.format(
"Unexpected Java versions configured: %s. Please run `./gradlew setupJdks` to regenerate the used"
+ " JDKS.",
unexpectedConfiguredJavaVersions));
}

String gradleJdkDaemonVersion = getDaemonJavaVersion().get().toString();
String os = CurrentOs.get().toString();
String arch = CurrentArch.get().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

package com.palantir.gradle.jdks;

import com.palantir.gradle.jdks.setup.common.Arch;
import com.palantir.gradle.jdks.setup.common.CurrentArch;
import com.palantir.gradle.jdks.setup.common.CurrentOs;
import com.palantir.gradle.jdks.setup.common.Os;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand All @@ -30,6 +34,8 @@
import java.nio.file.attribute.PosixFilePermission;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public final class GradleJdksConfigsUtils {

Expand Down Expand Up @@ -116,5 +122,20 @@ public static void createDirectories(Path directory) {
}
}

public static Set<String> getConfiguredJavaMajorVersions(Path gradleJdksLocalDirectory) {
Os os = CurrentOs.get();
Arch arch = CurrentArch.get();
try (Stream<Path> stream = Files.list(gradleJdksLocalDirectory).filter(Files::isDirectory)) {
return stream.filter(path -> path.resolve(os.toString())
.resolve(arch.toString())
.toFile()
.exists())
.map(path -> path.getFileName().toString())
.collect(Collectors.toSet());
} catch (IOException e) {
throw new RuntimeException("Unable to list the configured gradle/jdks major versions", e);
}
}

private GradleJdksConfigsUtils() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.palantir.gradle.jdks;

import com.google.common.collect.Streams;
import com.palantir.gradle.jdks.setup.common.Arch;
import com.palantir.gradle.jdks.setup.common.Os;
import java.util.Arrays;
Expand All @@ -25,6 +26,7 @@
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.gradle.api.JavaVersion;
import org.gradle.api.Project;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
Expand All @@ -36,8 +38,17 @@ public final class JdkDistributionConfigurator {
private static final JavaLanguageVersion MINIMUM_SUPPORTED_JAVA_VERSION = JavaLanguageVersion.of(11);

public static Map<JavaLanguageVersion, List<JdkDistributionConfig>> getJavaVersionToJdkDistros(
Project project, JdkDistributions jdkDistributions, JdksExtension jdksExtension) {
Set<JavaLanguageVersion> javaVersions = jdksExtension.getJdkMajorVersionsToUse().get().stream()
Project project,
JdkDistributions jdkDistributions,
JdksExtension jdksExtension,
Boolean includeAllJdks,
List<String> includeSpecificJdks) {
Set<JavaLanguageVersion> javaVersions = (includeAllJdks
? Arrays.stream(JavaVersion.values())
.map(javaVersion -> JavaLanguageVersion.of(javaVersion.getMajorVersion()))
: Streams.concat(
includeSpecificJdks.stream().map(JavaLanguageVersion::of),
jdksExtension.jdkMajorVersionsToUse().get().stream()))
.filter(javaLanguageVersion -> javaLanguageVersion.canCompileOrRun(MINIMUM_SUPPORTED_JAVA_VERSION))
.collect(Collectors.toSet());
return javaVersions.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,13 @@ public final void setDaemonTarget(int value) {
getDaemonTarget().set(JavaLanguageVersion.of(value));
}

public final SetProperty<JavaLanguageVersion> getJdkMajorVersionsToUse() {
public final SetProperty<JavaLanguageVersion> jdkMajorVersionsToUse() {
return this.jdkMajorVersionsToUse;
}

public final void setJdkMajorVersionsToUse(Set<JavaLanguageVersion> javaLanguageVersions) {
this.jdkMajorVersionsToUse.set(javaLanguageVersions);
public final void setJdkMajorVersionsToUse(Set<String> jdkMajorVersions) {
jdkMajorVersionsToUse()
.set(jdkMajorVersions.stream().map(JavaLanguageVersion::of).collect(Collectors.toSet()));
}

public final void daemonJdkOnly() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@

package com.palantir.gradle.jdks;

import com.palantir.baseline.plugins.javaversions.BaselineJavaVersionExtension;
import com.palantir.baseline.plugins.javaversions.BaselineJavaVersionsExtension;
import com.palantir.baseline.plugins.javaversions.ChosenJavaVersion;
import com.palantir.gradle.jdks.GradleWrapperPatcher.GradleWrapperPatcherTask;
import com.palantir.gradle.jdks.enablement.GradleJdksEnablement;
import com.palantir.gradle.jdks.flow.ToolchainFailureFlowActionsPlugin;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.api.tasks.wrapper.Wrapper;
import org.gradle.language.base.plugins.LifecycleBasePlugin;
Expand All @@ -47,6 +53,9 @@ public void apply(Project rootProject) {
+ "Gradle version in order to use the JDK setup.",
GradleJdksEnablement.MINIMUM_SUPPORTED_GRADLE_VERSION));
}
if (areFlowActionsSupported()) {
rootProject.getPluginManager().apply(ToolchainFailureFlowActionsPlugin.class);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the error message will only be available for repos >= 8.6

}
rootProject.getPluginManager().apply(LifecycleBasePlugin.class);
rootProject.getPluginManager().apply(PalantirGradleJdksIdeaPlugin.class);
rootProject
Expand All @@ -58,12 +67,32 @@ public void apply(Project rootProject) {
JdksExtension jdksExtension = JdksPlugin.extension(rootProject, jdkDistributions);

rootProject.getPluginManager().withPlugin("com.palantir.baseline-java-versions", unused -> {
rootProject
.getExtensions()
.getByType(BaselineJavaVersionsExtension.class)
.getSetupJdkToolchains()
.set(false);
BaselineJavaVersionsExtension baselineJavaVersionsExtension =
rootProject.getExtensions().getByType(BaselineJavaVersionsExtension.class);
baselineJavaVersionsExtension.getSetupJdkToolchains().set(false);

jdksExtension.jdkMajorVersionsToUse().set(rootProject.provider(() -> Stream.of(
jdksExtension.getDaemonTarget(),
baselineJavaVersionsExtension.libraryTarget(),
baselineJavaVersionsExtension.runtime().map(ChosenJavaVersion::javaLanguageVersion),
baselineJavaVersionsExtension
.distributionTarget()
.map(ChosenJavaVersion::javaLanguageVersion))
.map(Provider::get)
.collect(Collectors.toSet())));
});

rootProject.subprojects(
proj -> proj.getPluginManager().withPlugin("com.palantir.baseline-java-version", unused -> {
BaselineJavaVersionExtension projectVersions =
proj.getExtensions().getByType(BaselineJavaVersionExtension.class);

jdksExtension.jdkMajorVersionsToUse().addAll(rootProject.provider(() -> Stream.of(
projectVersions.target(), projectVersions.runtime())
.map(Provider::get)
.map(ChosenJavaVersion::javaLanguageVersion)
.collect(Collectors.toSet())));
}));
TaskProvider<Wrapper> wrapperTask = rootProject.getTasks().named("wrapper", Wrapper.class);

TaskProvider<GenerateGradleJdksConfigsTask> generateGradleJdkConfigs = rootProject
Expand All @@ -89,7 +118,11 @@ public void apply(Project rootProject) {
task.getDaemonJavaVersion().set(jdksExtension.getDaemonTarget());
task.getJavaVersionToJdkDistros()
.putAll(rootProject.provider(() -> JdkDistributionConfigurator.getJavaVersionToJdkDistros(
rootProject, jdkDistributions, jdksExtension)));
rootProject,
jdkDistributions,
jdksExtension,
task.getIncludeAllJdks().get(),
task.getIncludeJavaMajorVersions().get())));
task.getCaCerts().putAll(jdksExtension.getCaCerts());
});

Expand Down Expand Up @@ -144,4 +177,8 @@ private static boolean isGradleVersionSupported() {
.compareTo(GradleVersion.version(GradleJdksEnablement.MINIMUM_SUPPORTED_GRADLE_VERSION))
>= 0;
}

private static boolean areFlowActionsSupported() {
return GradleVersion.current().compareTo(GradleVersion.version("8.6")) >= 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* (c) Copyright 2025 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.gradle.jdks.flow;

import com.palantir.gradle.jdks.GradleJdksConfigsUtils;
import com.palantir.gradle.jdks.enablement.GradleJdksEnablement;
import javax.inject.Inject;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.flow.FlowProviders;
import org.gradle.api.flow.FlowScope;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class ToolchainFailureFlowActionsPlugin implements Plugin<Project> {

private static final Logger log = LoggerFactory.getLogger(ToolchainFailureFlowActionsPlugin.class);

@Inject
protected abstract FlowScope getFlowScope();

@Inject
protected abstract FlowProviders getFlowProviders();

@Override
public final void apply(Project project) {
if (!GradleJdksEnablement.isGradleJdkSetupEnabled(
project.getRootProject().getProjectDir().toPath())) {
throw new RuntimeException(
"Cannot apply `ToolchainFailureFlowActionsPlugin` without enabling palantir.jdk.setup.enabled");
}
getFlowScope().always(ToolchainFlowAction.class, spec -> {
spec.getParameters().getBuildResult().set(getFlowProviders().getBuildWorkResult());
spec.getParameters()
.getConfiguredJavaMajorVersions()
.set(project.provider(() -> GradleJdksConfigsUtils.getConfiguredJavaMajorVersions(
project.getRootProject().file("gradle/jdks").toPath())));
});
}
}
Loading