Skip to content

Commit

Permalink
[Gradle Jdk Automanagement] Generate Gradle Jdks Configuration & setu…
Browse files Browse the repository at this point in the history
…p Intellij (#360)

[Gradle Jdk Automanagement] Generate Gradle Jdks Configuration
  • Loading branch information
crogoz authored Jul 3, 2024
1 parent 07d8bb4 commit 6f0392f
Show file tree
Hide file tree
Showing 74 changed files with 2,951 additions and 926 deletions.
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,9 @@ New JDK distributions are easily added - you just need to:
4. Write some tests to check the path is being built correctly - [Azul Zulu example](https://github.com/palantir/gradle-jdks/blob/develop/gradle-jdks/src/test/groovy/com/palantir/gradle/jdks/AzulZuluJdkDistributionTest.java).
5. Make a PR.

## What does this not do?

1. **Run the Gradle wrapper/daemon with a certain JDK**
* This plugin will only run common tasks that require JDKs, like `JavaCompile`, `Test`, `JavaExec`, `GroovyCompile` etc (the aforementioned `com.palantir.baseline-java-versions` does this).
* The daemon itself still requires a preinstalled JDK.
* In future, we hope to implement this feature, although it is not currently scheduled to be worked on.
## [Beta Feature] Run the Gradle wrapper/daemon with a certain JDK
* More details in [grade-jdks-setup](gradle-jdks-setup/README.md)
* Disabled by default for now

## How can I see what JDK tasks are running with?

Expand Down
10 changes: 10 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ buildscript {
classpath 'com.palantir.gradle.externalpublish:gradle-external-publish-plugin:1.15.0'
classpath 'com.palantir.javaformat:gradle-palantir-java-format:2.47.0'
classpath 'com.palantir.gradle.consistentversions:gradle-consistent-versions:2.23.0'
classpath 'com.palantir.gradle.shadow-jar:gradle-shadow-jar:2.8.0'
classpath 'com.palantir.baseline:gradle-baseline-java:5.61.0'
classpath 'com.palantir.gradle.gitversion:gradle-git-version:3.1.0'
classpath 'gradle.plugin.org.inferred:gradle-processors:3.7.0'
classpath 'com.gradle.publish:plugin-publish-plugin:1.2.1'
}
}

plugins {
id "org.jetbrains.intellij" version "1.17.3" apply false
id 'org.jetbrains.gradle.plugin.idea-ext' version "1.1.1"
}

apply plugin: 'com.palantir.external-publish'
apply plugin: 'com.palantir.git-version'
apply plugin: 'com.palantir.consistent-versions'
Expand Down Expand Up @@ -48,3 +54,7 @@ allprojects {
apply plugin: 'com.palantir.java-format'
apply plugin: 'com.palantir.jakarta-package-alignment'
}

// workaround for:
// `Error during resolution of the dependency graph of configuration configuration ':gradle-jdks-settings:unshadedCopy'`
checkUnusedConstraints.enabled = false
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-360.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: improvement
improvement:
description: '[Gradle Jdk Automanagement] Generate Gradle Jdks Configuration & Intellij
setup'
links:
- https://github.com/palantir/gradle-jdks/pull/360
2 changes: 1 addition & 1 deletion gradle-jdks-distributions/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ dependencies {

testImplementation 'org.junit.jupiter:junit-jupiter'
testImplementation 'org.assertj:assertj-core'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.palantir.gradle.jdks.setup.common.UiNames;
import java.util.Optional;

public enum JdkDistributionName {
Expand Down
5 changes: 5 additions & 0 deletions gradle-jdks-enablement/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apply plugin: 'com.palantir.external-publish-jar'

dependencies {
implementation project(':gradle-jdks-setup-common')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* (c) Copyright 2024 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.enablement;

import com.palantir.gradle.jdks.setup.common.CurrentOs;
import com.palantir.gradle.jdks.setup.common.Os;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Optional;
import java.util.Properties;

public class GradleJdksEnablement {

public static final String MINIMUM_SUPPORTED_GRADLE_VERSION = "7.6";

public static boolean isGradleJdkSetupEnabled(Path projectDir) {
return !CurrentOs.get().equals(Os.WINDOWS) && isGradleJdkPropertyEnabled(projectDir);
}

private static boolean isGradleJdkPropertyEnabled(Path projectDir) {
File gradlePropsFile = projectDir.resolve("gradle.properties").toFile();
if (!gradlePropsFile.exists()) {
return false;
}
try {
Properties properties = new Properties();
properties.load(new FileInputStream(gradlePropsFile));
return Optional.ofNullable(properties.getProperty("palantir.jdk.setup.enabled"))
.map(Boolean::parseBoolean)
.orElse(false);
} catch (IOException e) {
throw new RuntimeException("Failed to read gradle.properties file", e);
}
}

private GradleJdksEnablement() {}
}
3 changes: 3 additions & 0 deletions gradle-jdks-groovy/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
apply plugin: 'java-gradle-plugin'
apply plugin: 'groovy'
apply plugin: 'com.palantir.external-publish-jar'
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* (c) Copyright 2024 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

class ConfigureJdksIdeaPluginXml {

static void configureExternalDependencies(Node rootNode) {
def externalDependencies = matchOrCreateChild(rootNode, 'component', [name: 'ExternalDependencies'])
matchOrCreateChild(externalDependencies, 'plugin', [id: 'palantir-gradle-jdks'])
}

private static Node matchOrCreateChild(Node base, String name, Map attributes = [:], Map defaults = [:]) {
matchChild(base, name, attributes).orElseGet {
base.appendNode(name, attributes + defaults)
}
}

private static Optional<Node> matchChild(Node base, String name, Map attributes = [:]) {
def child = base[name].find { it.attributes().entrySet().containsAll(attributes.entrySet()) }
return Optional.ofNullable(child)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.palantir.gradle.jdks.JdkDistributionName;
import com.palantir.gradle.jdks.Os;
import com.palantir.gradle.jdks.setup.common.Os;
import java.util.Map;
import org.immutables.value.Value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.palantir.gradle.jdks.Arch;
import com.palantir.gradle.jdks.setup.common.Arch;
import java.util.Map;
import org.immutables.value.Value;

Expand Down
46 changes: 46 additions & 0 deletions gradle-jdks-settings/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
apply plugin: 'java-gradle-plugin'
apply plugin: 'groovy'
apply plugin: 'com.palantir.external-publish-jar'
apply plugin: 'com.palantir.external-publish-gradle-plugin'
apply plugin: 'com.palantir.shadow-jar'

dependencies {
// DO NOT ADD directly implementation/api dependencies here. We need to avoid overlapping dependencies between the
// settings classLoader and the build classLoaders.
shadeTransitively project(':gradle-jdks-setup-common')
shadeTransitively project(':gradle-jdks-enablement')
}

gradlePlugin {
plugins {
patchJdks {
id = 'com.palantir.jdks.settings'
displayName = 'Palantir Gradle JDK Settings Plugin'
description = 'Sets the Gradle properties for the Gradle JDK setup'
implementationClass = 'com.palantir.gradle.jdks.settings.ToolchainJdksSettingsPlugin'
tags.addAll("java", "jdks")
}
}
website = 'https://github.com/palantir/gradle-jdks'
vcsUrl = 'https://github.com/palantir/gradle-jdks'
description = 'Gradle JDK settings plugins'
}

// When adding the shadeTransitively dependency, the pom file includes `gradle-jdks-root` as a dependency.
// This is broken and it will lead to classpath configuration failures when the settings plugin is applied. As a
// workaround, we are removing the artifact from the pom file.
publishing.publications {
withType(MavenPublication) {
pom.withXml {
var projectDependencyNodes = []
asNode().depthFirst { node ->
if (node.name().localPart == 'dependency' && node.get("groupId").text() == "com.palantir.gradle.jdks" && node.get("artifactId").text() == "gradle-jdks-root") {
projectDependencyNodes << node
}
}
projectDependencyNodes.each { node ->
node.parent().remove(node)
}
}
}
}
Loading

0 comments on commit 6f0392f

Please sign in to comment.