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

Configurable java toolchains #2193

Merged
merged 31 commits into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
a96512d
An entirely plain refactor
CRogers Mar 25, 2022
533d5f7
Pass in project only
CRogers Mar 25, 2022
e56c43e
Write some code
CRogers Mar 25, 2022
40e7ad7
Add jvm versions to extensions
CRogers Apr 4, 2022
6e6d0ef
Get test working for jdk downloader
CRogers Apr 7, 2022
1aa5cfd
Extract jdk on osx
CRogers Apr 8, 2022
09c6d4f
Wire it all together
CRogers Apr 11, 2022
7e68898
Fixes
CRogers Apr 11, 2022
51b834a
Put a root plugin in so we can configure all the repos at the right time
CRogers Apr 11, 2022
1c35750
be english good
CRogers Apr 11, 2022
299e8c2
Clean up sstuff
CRogers Apr 11, 2022
d66ed8a
Fix test
CRogers Apr 12, 2022
bf928eb
Shorten paths
CRogers Apr 12, 2022
edd23d0
Merge branch 'develop' of github.com:palantir/gradle-baseline into ca…
CRogers Apr 12, 2022
edd8d6a
Add generated changelog entries
svc-changelog Apr 12, 2022
612189f
Move code to BaselineJavaVersionsExtensions
CRogers Apr 12, 2022
a747e9f
Flail around trying to understand artifact transforms
CRogers Apr 19, 2022
ad42636
Less nots
CRogers Apr 19, 2022
7330ac8
ArtifactView!!!!!!!
CRogers Apr 19, 2022
bd4efc7
Give up on copyspec madness
CRogers Apr 19, 2022
a402dc7
Use downloader only everywhere
CRogers Apr 19, 2022
8457837
Simplify to just being configured with specific jdks
CRogers Apr 21, 2022
5ef6fd4
Fix all those copyright notices
CRogers Apr 21, 2022
3c996ea
Remove jarchivelib
CRogers Apr 21, 2022
6c3df93
Tidy up diff
CRogers Apr 21, 2022
5816d1f
Add generated changelog entries
svc-changelog Apr 21, 2022
fe0bf39
Merge branch 'develop' of github.com:palantir/gradle-baseline into ca…
CRogers Apr 28, 2022
d492489
Tidy up code
CRogers Apr 28, 2022
e19d229
Write a test
CRogers Apr 28, 2022
f6d2b22
Try reenabling fork
CRogers Apr 28, 2022
1517462
Note one why fork is needed
CRogers Apr 28, 2022
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ out/
docs/node_modules/
generated_src
generated_testSrc
generated
generated_tests
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-2193.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: feature
feature:
description: Add a configuration point for providing your own JDKs to all java requiring
tasks.
links:
- https://github.com/palantir/gradle-baseline/pull/2193
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@

import javax.inject.Inject;
import org.gradle.api.Project;
import org.gradle.api.provider.MapProperty;
import org.gradle.api.provider.Property;
import org.gradle.jvm.toolchain.JavaInstallationMetadata;
import org.gradle.jvm.toolchain.JavaLanguageVersion;

/**
* Extension named {@code javaVersions} on the root project used to configure all java modules
* with consistent java toolchains.
*/
public class BaselineJavaVersionsExtension {

private final Property<JavaLanguageVersion> libraryTarget;
private final Property<JavaLanguageVersion> distributionTarget;
private final Property<JavaLanguageVersion> runtime;
private final MapProperty<JavaLanguageVersion, JavaInstallationMetadata> jdks;
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if the map will come back to haunt us? I suspect our jdk repository layouts will be predictable as new language versions are released, we may be able to provide something like a function from JavaLanguageVersion -> Provider<JavaInstallationMetadata> without requiring custom/internal toolchain plugins to be updated for every release.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A map is a function!

But seriously, this is effectively the same as registering a function with this class then calling it to work out the metadata. Pretty sure that's how MapProperty.put(All) works under the scenes.


@Inject
public BaselineJavaVersionsExtension(Project project) {
Expand All @@ -43,6 +45,9 @@ public BaselineJavaVersionsExtension(Project project) {
libraryTarget.finalizeValueOnRead();
distributionTarget.finalizeValueOnRead();
runtime.finalizeValueOnRead();

jdks = project.getObjects().mapProperty(JavaLanguageVersion.class, JavaInstallationMetadata.class);
jdks.finalizeValueOnRead();
}

/** Target {@link JavaLanguageVersion} for compilation of libraries that are published. */
Expand Down Expand Up @@ -74,4 +79,8 @@ public final Property<JavaLanguageVersion> runtime() {
public final void setRuntime(int value) {
runtime.set(JavaLanguageVersion.of(value));
}

public final MapProperty<JavaLanguageVersion, JavaInstallationMetadata> getJdks() {
return jdks;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
package com.palantir.baseline.plugins;

import com.palantir.baseline.extensions.BaselineJavaVersionExtension;
import com.palantir.baseline.extensions.BaselineJavaVersionsExtension;
import com.palantir.baseline.plugins.javaversions.BaselineJavaToolchain;
import com.palantir.baseline.plugins.javaversions.JavaToolchains;
import javax.inject.Inject;
import org.gradle.api.Action;
import org.gradle.api.DefaultTask;
Expand All @@ -38,7 +41,6 @@
import org.gradle.api.tasks.scala.ScalaDoc;
import org.gradle.api.tasks.testing.Test;
import org.gradle.jvm.toolchain.JavaLanguageVersion;
import org.gradle.jvm.toolchain.JavaToolchainService;
import org.gradle.jvm.toolchain.JavaToolchainSpec;

public final class BaselineJavaVersion implements Plugin<Project> {
Expand All @@ -49,6 +51,7 @@ public final class BaselineJavaVersion implements Plugin<Project> {
public void apply(Project project) {
BaselineJavaVersionExtension extension =
project.getExtensions().create(EXTENSION_NAME, BaselineJavaVersionExtension.class, project);

project.getPluginManager().withPlugin("java", unused -> {
JavaPluginExtension javaPluginExtension = project.getExtensions().getByType(JavaPluginExtension.class);

Expand All @@ -62,11 +65,14 @@ public void execute(JavaToolchainSpec javaToolchainSpec) {
}
});

JavaToolchains javaToolchains = new JavaToolchains(
project, project.getRootProject().getExtensions().getByType(BaselineJavaVersionsExtension.class));

// Compilation tasks (using target version)
configureCompilationTasks(project, extension.target());
configureCompilationTasks(project, extension.target(), javaToolchains.forVersion(extension.target()));

// Execution tasks (using the runtime version)
configureExecutionTasks(project, extension.runtime());
configureExecutionTasks(project, javaToolchains.forVersion(extension.runtime()));

// Validation
project.getTasks()
Expand All @@ -82,18 +88,13 @@ public void execute(CheckJavaVersionsTask task) {
}

private static void configureCompilationTasks(
Project project, Provider<JavaLanguageVersion> targetVersionProvider) {
JavaToolchainService javaToolchainService = project.getExtensions().getByType(JavaToolchainService.class);

Project project,
Provider<JavaLanguageVersion> targetVersionProvider,
Provider<BaselineJavaToolchain> javaToolchain) {
project.getTasks().withType(JavaCompile.class, new Action<JavaCompile>() {
@Override
public void execute(JavaCompile javaCompile) {
javaCompile.getJavaCompiler().set(javaToolchainService.compilerFor(new Action<JavaToolchainSpec>() {
@Override
public void execute(JavaToolchainSpec javaToolchainSpec) {
javaToolchainSpec.getLanguageVersion().set(targetVersionProvider);
}
}));
javaCompile.getJavaCompiler().set(javaToolchain.flatMap(BaselineJavaToolchain::javaCompiler));
// Set sourceCompatibility to opt out of '-release', allowing opens/exports to be used.
javaCompile.doFirst(new Action<Task>() {
@Override
Expand All @@ -109,24 +110,14 @@ public void execute(Task task) {
project.getTasks().withType(Javadoc.class, new Action<Javadoc>() {
@Override
public void execute(Javadoc javadoc) {
javadoc.getJavadocTool().set(javaToolchainService.javadocToolFor(new Action<JavaToolchainSpec>() {
@Override
public void execute(JavaToolchainSpec javaToolchainSpec) {
javaToolchainSpec.getLanguageVersion().set(targetVersionProvider);
}
}));
javadoc.getJavadocTool().set(javaToolchain.flatMap(BaselineJavaToolchain::javadocTool));
}
});

project.getTasks().withType(GroovyCompile.class, new Action<GroovyCompile>() {
@Override
public void execute(GroovyCompile groovyCompile) {
groovyCompile.getJavaLauncher().set(javaToolchainService.launcherFor(new Action<JavaToolchainSpec>() {
@Override
public void execute(JavaToolchainSpec javaToolchainSpec) {
javaToolchainSpec.getLanguageVersion().set(targetVersionProvider);
}
}));
groovyCompile.getJavaLauncher().set(javaToolchain.flatMap(BaselineJavaToolchain::javaLauncher));
// Set sourceCompatibility to opt out of '-release', allowing opens/exports to be used.
groovyCompile.doFirst(new Action<Task>() {
@Override
Expand All @@ -142,12 +133,7 @@ public void execute(Task task) {
project.getTasks().withType(ScalaCompile.class, new Action<ScalaCompile>() {
@Override
public void execute(ScalaCompile scalaCompile) {
scalaCompile.getJavaLauncher().set(javaToolchainService.launcherFor(new Action<JavaToolchainSpec>() {
@Override
public void execute(JavaToolchainSpec javaToolchainSpec) {
javaToolchainSpec.getLanguageVersion().set(targetVersionProvider);
}
}));
scalaCompile.getJavaLauncher().set(javaToolchain.flatMap(BaselineJavaToolchain::javaLauncher));
// Set sourceCompatibility to opt out of '-release', allowing opens/exports to be used.
scalaCompile.doFirst(new Action<Task>() {
@Override
Expand All @@ -163,39 +149,23 @@ public void execute(Task task) {
project.getTasks().withType(ScalaDoc.class, new Action<ScalaDoc>() {
@Override
public void execute(ScalaDoc scalaDoc) {
scalaDoc.getJavaLauncher().set(javaToolchainService.launcherFor(new Action<JavaToolchainSpec>() {
@Override
public void execute(JavaToolchainSpec javaToolchainSpec) {
javaToolchainSpec.getLanguageVersion().set(targetVersionProvider);
}
}));
scalaDoc.getJavaLauncher().set(javaToolchain.flatMap(BaselineJavaToolchain::javaLauncher));
}
});
}

private static void configureExecutionTasks(Project project, Provider<JavaLanguageVersion> runtimeVersionProvider) {
JavaToolchainService javaToolchainService = project.getExtensions().getByType(JavaToolchainService.class);
private static void configureExecutionTasks(Project project, Provider<BaselineJavaToolchain> javaToolchain) {
project.getTasks().withType(JavaExec.class, new Action<JavaExec>() {
@Override
public void execute(JavaExec javaExec) {
javaExec.getJavaLauncher().set(javaToolchainService.launcherFor(new Action<JavaToolchainSpec>() {
@Override
public void execute(JavaToolchainSpec javaToolchainSpec) {
javaToolchainSpec.getLanguageVersion().set(runtimeVersionProvider);
}
}));
javaExec.getJavaLauncher().set(javaToolchain.flatMap(BaselineJavaToolchain::javaLauncher));
}
});

project.getTasks().withType(Test.class, new Action<Test>() {
@Override
public void execute(Test test) {
test.getJavaLauncher().set(javaToolchainService.launcherFor(new Action<JavaToolchainSpec>() {
@Override
public void execute(JavaToolchainSpec javaToolchainSpec) {
javaToolchainSpec.getLanguageVersion().set(runtimeVersionProvider);
}
}));
test.getJavaLauncher().set(javaToolchain.flatMap(BaselineJavaToolchain::javaLauncher));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* (c) Copyright 2022 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.baseline.plugins.javaversions;

import org.gradle.api.file.RegularFile;
import org.gradle.jvm.toolchain.JavaCompiler;
import org.gradle.jvm.toolchain.JavaInstallationMetadata;

final class BaselineJavaCompiler implements JavaCompiler {
private final JavaInstallationMetadata javaInstallationMetadata;

BaselineJavaCompiler(JavaInstallationMetadata javaInstallationMetadata) {
this.javaInstallationMetadata = javaInstallationMetadata;
}

@Override
public JavaInstallationMetadata getMetadata() {
return javaInstallationMetadata;
}

@Override
public RegularFile getExecutablePath() {
return JavaInstallationMetadataUtils.findExecutable(javaInstallationMetadata, "javac");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* (c) Copyright 2022 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.baseline.plugins.javaversions;

import org.gradle.api.file.RegularFile;
import org.gradle.jvm.toolchain.JavaInstallationMetadata;
import org.gradle.jvm.toolchain.JavaLauncher;

final class BaselineJavaLauncher implements JavaLauncher {
private final JavaInstallationMetadata javaInstallationMetadata;

BaselineJavaLauncher(JavaInstallationMetadata javaInstallationMetadata) {
this.javaInstallationMetadata = javaInstallationMetadata;
}

@Override
public JavaInstallationMetadata getMetadata() {
return javaInstallationMetadata;
}

@Override
public RegularFile getExecutablePath() {
return JavaInstallationMetadataUtils.findExecutable(javaInstallationMetadata, "java");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* (c) Copyright 2022 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.baseline.plugins.javaversions;

import org.gradle.api.provider.Provider;
import org.gradle.jvm.toolchain.JavaCompiler;
import org.gradle.jvm.toolchain.JavaLauncher;
import org.gradle.jvm.toolchain.JavadocTool;

public interface BaselineJavaToolchain {
Provider<JavaCompiler> javaCompiler();

Provider<JavadocTool> javadocTool();

Provider<JavaLauncher> javaLauncher();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* (c) Copyright 2022 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.baseline.plugins.javaversions;

import org.gradle.api.file.RegularFile;
import org.gradle.jvm.toolchain.JavaInstallationMetadata;
import org.gradle.jvm.toolchain.JavadocTool;

final class BaselineJavadocTool implements JavadocTool {
private final JavaInstallationMetadata javaInstallationMetadata;

BaselineJavadocTool(JavaInstallationMetadata javaInstallationMetadata) {
this.javaInstallationMetadata = javaInstallationMetadata;
}

@Override
public JavaInstallationMetadata getMetadata() {
return javaInstallationMetadata;
}

@Override
public RegularFile getExecutablePath() {
return JavaInstallationMetadataUtils.findExecutable(javaInstallationMetadata, "javadoc");
}
}
Loading