-
-
Notifications
You must be signed in to change notification settings - Fork 971
Propagate Java toolchain to JavaExec tasks #15403
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
Merged
jamesfredley
merged 3 commits into
apache:7.0.x
from
jamesfredley:fix/javaexec-toolchain-inheritance
Feb 19, 2026
+504
−0
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
...s-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/core/GradleSpecification.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 | ||
| * | ||
| * https://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 org.grails.gradle.plugin.core | ||
|
|
||
| import org.gradle.testkit.runner.BuildResult | ||
| import org.gradle.testkit.runner.GradleRunner | ||
| import org.gradle.testkit.runner.TaskOutcome | ||
| import spock.lang.Specification | ||
|
|
||
| import java.nio.file.FileVisitResult | ||
| import java.nio.file.Files | ||
| import java.nio.file.Path | ||
| import java.nio.file.SimpleFileVisitor | ||
| import java.nio.file.attribute.BasicFileAttributes | ||
|
|
||
| /** | ||
| * Base class for Gradle plugin functional tests using TestKit. | ||
| * | ||
| * <p>Adapted from the {@code GradleSpecification} in the | ||
| * {@code apache/grails-gradle-publish} project. Handles temp directory | ||
| * management, GradleRunner setup, test resource project copying, and | ||
| * common build assertions.</p> | ||
| * | ||
| * @since 7.0.8 | ||
| */ | ||
| abstract class GradleSpecification extends Specification { | ||
|
|
||
| private static Path basePath | ||
| private static GradleRunner gradleRunner | ||
|
|
||
| /** Project version injected by Gradle test config. */ | ||
| protected static final String PROJECT_VERSION = System.getProperty('projectVersion') | ||
|
|
||
| /** Current JDK major version injected by Gradle test config. */ | ||
| protected static final int CURRENT_JDK = Integer.parseInt(System.getProperty('currentJdk')) | ||
|
|
||
| void setupSpec() { | ||
| basePath = Files.createTempDirectory('gradle-projects') | ||
| Path testKitDir = Files.createDirectories(basePath.resolve('.gradle')) | ||
| gradleRunner = GradleRunner.create() | ||
| .withPluginClasspath() | ||
| .withTestKitDir(testKitDir.toFile()) | ||
| } | ||
|
|
||
| void cleanup() { | ||
| basePath?.toFile()?.listFiles()?.each { | ||
| if (it.name == '.gradle') { | ||
| return | ||
| } | ||
| it.deleteDir() | ||
| } | ||
| } | ||
|
|
||
| void cleanupSpec() { | ||
| basePath?.toFile()?.deleteDir() | ||
| } | ||
|
|
||
| /** | ||
| * Sets up a test project from resource files under | ||
| * {@code src/test/resources/test-projects/{projectName}}. | ||
| * | ||
| * <p>Files are copied to a temp directory. Any occurrence of | ||
| * {@code __CURRENT_JDK__} in {@code .gradle} files is replaced | ||
| * with the actual current JDK version, and {@code __PROJECT_VERSION__} | ||
| * is replaced with the actual project version.</p> | ||
| */ | ||
| protected GradleRunner setupTestResourceProject(String projectName) { | ||
| Path destination = basePath.resolve(projectName) | ||
| Files.createDirectories(destination) | ||
|
|
||
| Path source = Path.of("src/test/resources/test-projects/${projectName}") | ||
| copyDirectory(source, destination) | ||
|
|
||
| gradleRunner.withProjectDir(destination.toFile()) | ||
| } | ||
|
|
||
| /** | ||
| * Executes a Gradle task and returns the build result. | ||
| */ | ||
| protected BuildResult executeTask(String taskName, List<String> otherArgs = []) { | ||
| List<String> args = [taskName, '--stacktrace'] | ||
| args.addAll(otherArgs) | ||
| gradleRunner.withArguments(args).forwardOutput().build() | ||
| } | ||
|
|
||
| /** | ||
| * Asserts that the given task succeeded. | ||
| */ | ||
| protected void assertTaskSuccess(String taskName, BuildResult result) { | ||
| def task = result.tasks.find { it.path.endsWith(":${taskName}") } | ||
| assert task != null : "Task '${taskName}' not found in build result" | ||
| assert task.outcome == TaskOutcome.SUCCESS : "Task '${taskName}' outcome was ${task.outcome}" | ||
| } | ||
|
|
||
| private void copyDirectory(Path source, Path destination) { | ||
| Files.walkFileTree(source, new SimpleFileVisitor<Path>() { | ||
| @Override | ||
| FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { | ||
| Files.createDirectories(destination.resolve(source.relativize(dir))) | ||
| FileVisitResult.CONTINUE | ||
| } | ||
|
|
||
| @Override | ||
| FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { | ||
| Path target = destination.resolve(source.relativize(file)) | ||
| if (file.toString().endsWith('.gradle') || file.toString().endsWith('.properties')) { | ||
| String content = Files.readString(file) | ||
| .replace('__CURRENT_JDK__', String.valueOf(CURRENT_JDK)) | ||
| .replace('__PROJECT_VERSION__', PROJECT_VERSION) | ||
| Files.writeString(target, content) | ||
| } else { | ||
| Files.copy(file, target) | ||
| } | ||
| FileVisitResult.CONTINUE | ||
| } | ||
| }) | ||
| } | ||
| } |
136 changes: 136 additions & 0 deletions
136
...gins/src/test/groovy/org/grails/gradle/plugin/core/GrailsGradlePluginToolchainSpec.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 | ||
| * | ||
| * https://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 org.grails.gradle.plugin.core | ||
|
|
||
| /** | ||
| * Tests that {@link GrailsGradlePlugin} propagates the project's Java toolchain | ||
| * to JavaExec tasks via {@code javaLauncher.convention()}. | ||
| * | ||
| * <p>Without the fix, JavaExec tasks spawned by Grails (dbm-* migration | ||
| * commands, console, shell, application context commands) use the JDK | ||
| * running Gradle instead of the project's configured toolchain.</p> | ||
| * | ||
| * @since 7.0.8 | ||
| * @see GrailsGradlePlugin#configureToolchainForForkTasks | ||
| */ | ||
| class GrailsGradlePluginToolchainSpec extends GradleSpecification { | ||
|
|
||
| // ---------------------------------------------------------------- | ||
| // Toolchain propagation | ||
| // ---------------------------------------------------------------- | ||
|
|
||
| def "JavaExec tasks inherit project toolchain"() { | ||
| given: | ||
| setupTestResourceProject('toolchain-javaexec') | ||
|
|
||
| when: | ||
| def result = executeTask('checkToolchain') | ||
|
|
||
| then: | ||
| result.output.contains("TOOLCHAIN_VERSION=${CURRENT_JDK}") | ||
| } | ||
|
|
||
| def "Test tasks inherit project toolchain"() { | ||
| given: | ||
| setupTestResourceProject('toolchain-test') | ||
|
|
||
| when: | ||
| def result = executeTask('checkTestToolchain') | ||
|
|
||
| then: | ||
| result.output.contains("TEST_TOOLCHAIN_VERSION=${CURRENT_JDK}") | ||
| } | ||
|
|
||
| def "ApplicationContextCommandTask inherits toolchain via grails-web plugin"() { | ||
| given: | ||
| setupTestResourceProject('toolchain-command') | ||
|
|
||
| when: | ||
| def result = executeTask('checkCommandToolchain') | ||
|
|
||
| then: | ||
| result.output.contains("CMD_TOOLCHAIN_VERSION=${CURRENT_JDK}") | ||
| } | ||
|
|
||
| def "convention allows individual task override via set()"() { | ||
| given: | ||
| setupTestResourceProject('toolchain-override') | ||
|
|
||
| when: | ||
| def result = executeTask('checkOverride') | ||
|
|
||
| then: | ||
| result.output.contains("OVERRIDE_VERSION=${CURRENT_JDK}") | ||
| } | ||
|
|
||
| // ---------------------------------------------------------------- | ||
| // Backwards compatibility (no toolchain configured) | ||
| // ---------------------------------------------------------------- | ||
|
|
||
| def "JavaExec tasks work without errors when no toolchain configured"() { | ||
| given: | ||
| setupTestResourceProject('no-toolchain-javaexec') | ||
|
|
||
| when: | ||
| def result = executeTask('checkToolchain') | ||
|
|
||
| then: | ||
| result.output.contains('HAS_LAUNCHER=') | ||
| } | ||
|
|
||
| def "GrailsWebGradlePlugin works without errors when no toolchain configured"() { | ||
| given: | ||
| setupTestResourceProject('no-toolchain-web') | ||
|
|
||
| when: | ||
| def result = executeTask('checkNoError') | ||
|
|
||
| then: | ||
| result.output.contains('WEB_PLUGIN_OK=true') | ||
| } | ||
|
|
||
| // ---------------------------------------------------------------- | ||
| // Fork settings preservation | ||
| // ---------------------------------------------------------------- | ||
|
|
||
| def "configureForkSettings applies system properties and default heap sizes"() { | ||
| given: | ||
| setupTestResourceProject('fork-settings-defaults') | ||
|
|
||
| when: | ||
| def result = executeTask('inspectSysProps') | ||
|
|
||
| then: | ||
| result.output.contains('HAS_ENV=true') | ||
| result.output.contains('MIN_HEAP=768m') | ||
| result.output.contains('MAX_HEAP=768m') | ||
| } | ||
|
|
||
| def "custom heap sizes are not overridden by fork settings"() { | ||
| given: | ||
| setupTestResourceProject('fork-settings-custom') | ||
|
|
||
| when: | ||
| def result = executeTask('inspectHeap') | ||
|
|
||
| then: | ||
| result.output.contains('MIN_HEAP=512m') | ||
| result.output.contains('MAX_HEAP=2g') | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
grails-gradle/plugins/src/test/resources/test-projects/fork-settings-custom/build.gradle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| plugins { | ||
| id 'org.apache.grails.gradle.grails-app' | ||
| } | ||
|
|
||
| tasks.register('customHeap', JavaExec) { | ||
| classpath = files() | ||
| mainClass = 'does.not.Matter' | ||
| minHeapSize = '512m' | ||
| maxHeapSize = '2g' | ||
| } | ||
|
|
||
| tasks.register('inspectHeap') { | ||
| doLast { | ||
| def task = tasks.named('customHeap', JavaExec).get() | ||
| println "MIN_HEAP=${task.minHeapSize}" | ||
| println "MAX_HEAP=${task.maxHeapSize}" | ||
| } | ||
| } |
1 change: 1 addition & 0 deletions
1
...ls-gradle/plugins/src/test/resources/test-projects/fork-settings-custom/gradle.properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| grailsVersion=__PROJECT_VERSION__ |
Empty file.
1 change: 1 addition & 0 deletions
1
grails-gradle/plugins/src/test/resources/test-projects/fork-settings-custom/settings.gradle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| rootProject.name = 'test-toolchain' |
18 changes: 18 additions & 0 deletions
18
grails-gradle/plugins/src/test/resources/test-projects/fork-settings-defaults/build.gradle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| plugins { | ||
| id 'org.apache.grails.gradle.grails-app' | ||
| } | ||
|
|
||
| tasks.register('checkSysProps', JavaExec) { | ||
| classpath = files() | ||
| mainClass = 'does.not.Matter' | ||
| } | ||
|
|
||
| tasks.register('inspectSysProps') { | ||
| doLast { | ||
| def task = tasks.named('checkSysProps', JavaExec).get() | ||
| def sysProps = task.systemProperties | ||
| println "HAS_ENV=${sysProps.containsKey('grails.env')}" | ||
| println "MIN_HEAP=${task.minHeapSize}" | ||
| println "MAX_HEAP=${task.maxHeapSize}" | ||
| } | ||
| } |
1 change: 1 addition & 0 deletions
1
...-gradle/plugins/src/test/resources/test-projects/fork-settings-defaults/gradle.properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| grailsVersion=__PROJECT_VERSION__ |
Empty file.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest we move this to test-config. The additional properties don't hurt and it' sbest to be consistent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Moved
projectVersionandcurrentJdksystem properties intotest-config.gradleand removed the standalone block fromplugins/build.gradle. All 8 tests pass.