Only configure plugin classpath where it's needed

When spring-boot-gradle-plugin is using GradleRunner, it needs to be
configured with a custom plugin classpath to account for the fact
that our Gradle plugin is on the classpath of the system classloader
but some of the other plugins would only be available on a
Gradle-created classloader. This imbalance cause class loading
problems as code in spring-boot-gradle-plugin can't see types at
runtime that are only available on the Gradle-created classloader.

To overcome this, we need to configure the GradleRunner with a custom
plugin classpath that contains both spring-boot-gradle-plugin and all
of the other plugins that are used in its various integration tests.
Previously, this was done in GradleBuild that's used by both
spring-boot-gradle-plugin and spring-boot-image-tests. This caused
a problem as spring-boot-image-tests does not have the
above-described problem and trying to correct it did not work leaving
tests that use spring-boot-gradle-plugin unable to see other plugins
such that the native image plugin.

This commit reworks the customization of the plugin classpath so that
it's only done in spring-boot-gradle-plugin's integration tests.

Closes gh-42338
This commit is contained in:
Andy Wilkinson
2024-09-17 20:06:23 +01:00
parent dc72e3853b
commit 1240c59482
9 changed files with 132 additions and 77 deletions

View File

@@ -31,6 +31,7 @@ import org.junit.jupiter.api.extension.TestTemplateInvocationContext;
import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider;
import org.junit.platform.commons.util.AnnotationUtils;
import org.springframework.boot.gradle.testkit.PluginClasspathGradleBuild;
import org.springframework.boot.testsupport.gradle.testkit.GradleBuild;
import org.springframework.boot.testsupport.gradle.testkit.GradleBuildExtension;
import org.springframework.boot.testsupport.gradle.testkit.GradleVersions;
@@ -96,7 +97,7 @@ final class GradleCompatibilityExtension implements TestTemplateInvocationContex
@Override
public List<Extension> getAdditionalExtensions() {
GradleBuild gradleBuild = new GradleBuild().gradleVersion(this.gradleVersion);
GradleBuild gradleBuild = new PluginClasspathGradleBuild().gradleVersion(this.gradleVersion);
if (this.configurationCache) {
gradleBuild.configurationCache();
}

View File

@@ -26,6 +26,7 @@ import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestTemplateInvocationContext;
import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider;
import org.springframework.boot.gradle.testkit.PluginClasspathGradleBuild;
import org.springframework.boot.testsupport.gradle.testkit.Dsl;
import org.springframework.boot.testsupport.gradle.testkit.GradleBuild;
import org.springframework.boot.testsupport.gradle.testkit.GradleBuildExtension;
@@ -60,8 +61,8 @@ public class GradleMultiDslExtension implements TestTemplateInvocationContextPro
@Override
public List<Extension> getAdditionalExtensions() {
GradleBuild gradleBuild = new GradleBuild(this.dsl);
gradleBuild.gradleVersion(GradleVersions.minimumCompatible());
GradleBuild gradleBuild = new PluginClasspathGradleBuild(this.dsl)
.gradleVersion(GradleVersions.minimumCompatible());
return Arrays.asList(new GradleBuildFieldSetter(gradleBuild), new GradleBuildExtension());
}

View File

@@ -28,6 +28,7 @@ import org.junit.jupiter.api.condition.DisabledForJreRange;
import org.junit.jupiter.api.condition.JRE;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.gradle.testkit.PluginClasspathGradleBuild;
import org.springframework.boot.testsupport.gradle.testkit.GradleBuild;
import org.springframework.boot.testsupport.gradle.testkit.GradleBuildExtension;
@@ -42,7 +43,7 @@ import static org.assertj.core.api.Assertions.assertThat;
@ExtendWith(GradleBuildExtension.class)
class KotlinPluginActionIntegrationTests {
GradleBuild gradleBuild = new GradleBuild();
GradleBuild gradleBuild = new PluginClasspathGradleBuild();
@Test
void noKotlinVersionPropertyWithoutKotlinPlugin() {

View File

@@ -22,6 +22,7 @@ import org.junit.jupiter.api.condition.DisabledForJreRange;
import org.junit.jupiter.api.condition.JRE;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.gradle.testkit.PluginClasspathGradleBuild;
import org.springframework.boot.testsupport.gradle.testkit.GradleBuild;
import org.springframework.boot.testsupport.gradle.testkit.GradleBuildExtension;
@@ -35,7 +36,7 @@ import static org.assertj.core.api.Assertions.assertThat;
@ExtendWith(GradleBuildExtension.class)
class SpringBootPluginIntegrationTests {
final GradleBuild gradleBuild = new GradleBuild();
final GradleBuild gradleBuild = new PluginClasspathGradleBuild();
@Test
@DisabledForJreRange(min = JRE.JAVA_20)

View File

@@ -0,0 +1,109 @@
/*
* Copyright 2012-2024 the original author or authors.
*
* 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
*
* 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.springframework.boot.gradle.testkit;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.core.Versioned;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import com.sun.jna.Platform;
import io.spring.gradle.dependencymanagement.DependencyManagementPlugin;
import org.antlr.v4.runtime.Lexer;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.hc.client5.http.io.HttpClientConnectionManager;
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http2.HttpVersionPolicy;
import org.gradle.testkit.runner.GradleRunner;
import org.jetbrains.kotlin.gradle.model.KotlinProject;
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilerPluginSupportPlugin;
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformJvmPlugin;
import org.jetbrains.kotlin.project.model.LanguageSettings;
import org.jetbrains.kotlin.tooling.core.KotlinToolingVersion;
import org.tomlj.Toml;
import org.springframework.asm.ClassVisitor;
import org.springframework.boot.buildpack.platform.build.BuildRequest;
import org.springframework.boot.loader.tools.LaunchScript;
import org.springframework.boot.testsupport.gradle.testkit.Dsl;
import org.springframework.boot.testsupport.gradle.testkit.GradleBuild;
/**
* Custom {@link GradleBuild} that configures the
* {@link GradleRunner#withPluginClasspath(Iterable) plugin classpath}.
*
* @author Andy Wilkinson
* @author Scott Frederick
*/
public class PluginClasspathGradleBuild extends GradleBuild {
public PluginClasspathGradleBuild() {
super();
}
public PluginClasspathGradleBuild(Dsl dsl) {
super(dsl);
}
@Override
public GradleRunner prepareRunner(String... arguments) throws IOException {
return super.prepareRunner(arguments).withPluginClasspath(pluginClasspath());
}
private List<File> pluginClasspath() {
return Arrays.asList(new File("bin/main"), new File("build/classes/java/main"),
new File("build/resources/main"), new File(pathOfJarContaining(LaunchScript.class)),
new File(pathOfJarContaining(ClassVisitor.class)),
new File(pathOfJarContaining(DependencyManagementPlugin.class)),
new File(pathOfJarContaining("org.jetbrains.kotlin.cli.common.PropertiesKt")),
new File(pathOfJarContaining(KotlinPlatformJvmPlugin.class)),
new File(pathOfJarContaining(KotlinProject.class)),
new File(pathOfJarContaining(KotlinToolingVersion.class)),
new File(pathOfJarContaining("org.jetbrains.kotlin.daemon.client.KotlinCompilerClient")),
new File(pathOfJarContaining(KotlinCompilerPluginSupportPlugin.class)),
new File(pathOfJarContaining(LanguageSettings.class)),
new File(pathOfJarContaining(ArchiveEntry.class)), new File(pathOfJarContaining(BuildRequest.class)),
new File(pathOfJarContaining(HttpClientConnectionManager.class)),
new File(pathOfJarContaining(HttpRequest.class)),
new File(pathOfJarContaining(HttpVersionPolicy.class)), new File(pathOfJarContaining(Module.class)),
new File(pathOfJarContaining(Versioned.class)),
new File(pathOfJarContaining(ParameterNamesModule.class)),
new File(pathOfJarContaining(JsonView.class)), new File(pathOfJarContaining(Platform.class)),
new File(pathOfJarContaining(Toml.class)), new File(pathOfJarContaining(Lexer.class)),
new File(pathOfJarContaining("org.graalvm.buildtools.gradle.NativeImagePlugin")),
new File(pathOfJarContaining("org.graalvm.reachability.GraalVMReachabilityMetadataRepository")),
new File(pathOfJarContaining("org.graalvm.buildtools.utils.SharedConstants")));
}
private String pathOfJarContaining(String className) {
try {
return pathOfJarContaining(Class.forName(className));
}
catch (ClassNotFoundException ex) {
throw new IllegalArgumentException(ex);
}
}
private String pathOfJarContaining(Class<?> type) {
return type.getProtectionDomain().getCodeSource().getLocation().getPath();
}
}