Skip to content

Commit 8c6910c

Browse files
committed
Polish "Refactor Gradle plugin tests to use runner's plugin classpath"
This commit removes the need for the Kotlin plugin tests to continue to set the plugin classpath in each test build script. This is achieved by adding the jars files containing the Kotlin plugin and its dependencies to the runner's plugin classpath in the same way as was already done for the dependency management plugin. Closes gh-14680
1 parent c514495 commit 8c6910c

File tree

6 files changed

+20
-72
lines changed

6 files changed

+20
-72
lines changed

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<gradle.executable>./gradlew</gradle.executable>
1717
<gradle.task>build</gradle.task>
1818
<skip.gradle.build>false</skip.gradle.build>
19+
<kotlin.version>1.2.10</kotlin.version>
1920
</properties>
2021
<dependencies>
2122
<dependency>

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/testkit/GradleBuild.java

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,7 +25,6 @@
2525
import java.util.Arrays;
2626
import java.util.List;
2727
import java.util.regex.Pattern;
28-
import java.util.stream.Collectors;
2928

3029
import javax.xml.xpath.XPath;
3130
import javax.xml.xpath.XPathExpression;
@@ -35,6 +34,11 @@
3534
import org.apache.commons.compress.archivers.ArchiveEntry;
3635
import org.gradle.testkit.runner.BuildResult;
3736
import org.gradle.testkit.runner.GradleRunner;
37+
import org.jetbrains.kotlin.cli.common.PropertiesKt;
38+
import org.jetbrains.kotlin.compilerRunner.KotlinCompilerRunner;
39+
import org.jetbrains.kotlin.gradle.plugin.KotlinGradleSubplugin;
40+
import org.jetbrains.kotlin.gradle.plugin.KotlinPlugin;
41+
import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper;
3842
import org.junit.rules.TemporaryFolder;
3943
import org.junit.rules.TestRule;
4044
import org.junit.runner.Description;
@@ -121,14 +125,14 @@ private List<File> pluginClasspath() {
121125
new File(pathOfJarContaining(LaunchScript.class)),
122126
new File(pathOfJarContaining(ClassVisitor.class)),
123127
new File(pathOfJarContaining(DependencyManagementPlugin.class)),
128+
new File(pathOfJarContaining(KotlinPluginWrapper.class)),
129+
new File(pathOfJarContaining(PropertiesKt.class)),
130+
new File(pathOfJarContaining(KotlinCompilerRunner.class)),
131+
new File(pathOfJarContaining(KotlinPlugin.class)),
132+
new File(pathOfJarContaining(KotlinGradleSubplugin.class)),
124133
new File(pathOfJarContaining(ArchiveEntry.class)));
125134
}
126135

127-
private String pluginClasspathAsString() {
128-
return pluginClasspath().stream().map(File::getAbsolutePath)
129-
.collect(Collectors.joining(","));
130-
}
131-
132136
private String pathOfJarContaining(Class<?> type) {
133137
return type.getProtectionDomain().getCodeSource().getLocation().getPath();
134138
}
@@ -170,20 +174,6 @@ public GradleRunner prepareRunner(String... arguments) throws IOException {
170174
List<String> allArguments = new ArrayList<>();
171175
allArguments.add("-PbootVersion=" + getBootVersion());
172176
allArguments.add("--stacktrace");
173-
174-
// this is necessary for the tests checking that we react correctly to the Kotlin
175-
// plugin.
176-
// Indeed, when using the plugins block to load the Boot plugin under test,
177-
// relying on the plugin
178-
// classpath set by withPluginClasspath(pluginClasspath()), the Boot plugin and
179-
// the Kotlin plugin
180-
// are loaded using two separate classloaders, and the Boot plugin thus doesn't
181-
// see the Kotlin plugin
182-
// class and can't react to it. To circumvent this test kit limitation, we set the
183-
// classpath
184-
// from the buildscript block of the build script, thanks to this pluginClasspath
185-
// property
186-
allArguments.add("-PpluginClasspath=" + pluginClasspathAsString());
187177
allArguments.addAll(Arrays.asList(arguments));
188178
return gradleRunner.withArguments(allArguments);
189179
}

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/KotlinPluginActionIntegrationTests-kotlinCompileTasksCanOverrideDefaultJavaParametersFlag.gradle

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
1-
// this is necessary for the tests checking that we react correctly to the Kotlin plugin.
2-
// Indeed, when using the plugins block to load the Boot plugin under test, relying on the plugin
3-
// classpath set by the test kit gradle runner, the Boot plugin and the Kotlin plugin
4-
// are loaded using two separate classloaders, and the Boot plugin thus doesn't see the Kotlin plugin
5-
// class and can't react to it. To circumvent this test kit limitation, we set the classpath here.
6-
buildscript {
7-
dependencies {
8-
classpath files(pluginClasspath.split(','))
9-
}
10-
}
11-
121
plugins {
13-
id 'org.jetbrains.kotlin.jvm' version '1.2.10'
2+
id 'org.springframework.boot' version '{version}'
143
}
154

16-
apply plugin: 'org.springframework.boot'
5+
apply plugin: 'org.jetbrains.kotlin.jvm'
176

187
import org.jetbrains.kotlin.gradle.dsl.KotlinCompile
198

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/KotlinPluginActionIntegrationTests-kotlinCompileTasksUseJavaParametersFlagByDefault.gradle

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
1-
// this is necessary for the tests checking that we react correctly to the Kotlin plugin.
2-
// Indeed, when using the plugins block to load the Boot plugin under test, relying on the plugin
3-
// classpath set by the test kit gradle runner, the Boot plugin and the Kotlin plugin
4-
// are loaded using two separate classloaders, and the Boot plugin thus doesn't see the Kotlin plugin
5-
// class and can't react to it. To circumvent this test kit limitation, we set the classpath here.
6-
buildscript {
7-
dependencies {
8-
classpath files(pluginClasspath.split(','))
9-
}
10-
}
11-
121
plugins {
13-
id 'org.jetbrains.kotlin.jvm' version '1.2.10'
2+
id 'org.springframework.boot' version '{version}'
143
}
154

16-
apply plugin: 'org.springframework.boot'
5+
apply plugin: 'org.jetbrains.kotlin.jvm'
176

187
import org.jetbrains.kotlin.gradle.dsl.KotlinCompile
198

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/KotlinPluginActionIntegrationTests-kotlinVersionMatchesKotlinPluginVersion.gradle

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
1-
// this is necessary for the tests checking that we react correctly to the Kotlin plugin.
2-
// Indeed, when using the plugins block to load the Boot plugin under test, relying on the plugin
3-
// classpath set by the test kit gradle runner, the Boot plugin and the Kotlin plugin
4-
// are loaded using two separate classloaders, and the Boot plugin thus doesn't see the Kotlin plugin
5-
// class and can't react to it. To circumvent this test kit limitation, we set the classpath here.
6-
buildscript {
7-
dependencies {
8-
classpath files(pluginClasspath.split(','))
9-
}
10-
}
11-
121
plugins {
13-
id 'org.jetbrains.kotlin.jvm' version '1.2.10'
2+
id 'org.springframework.boot' version '{version}'
143
}
154

16-
17-
apply plugin: 'org.springframework.boot'
185
apply plugin: 'io.spring.dependency-management'
6+
apply plugin: 'org.jetbrains.kotlin.jvm'
197

208
dependencyManagement {
219
resolutionStrategy {

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/KotlinPluginActionIntegrationTests-noKotlinVersionPropertyWithoutKotlinPlugin.gradle

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
1-
// this is necessary for the tests checking that we react correctly to the Kotlin plugin.
2-
// Indeed, when using the plugins block to load the Boot plugin under test, relying on the plugin
3-
// classpath set by the test kit gradle runner, the Boot plugin and the Kotlin plugin
4-
// are loaded using two separate classloaders, and the Boot plugin thus doesn't see the Kotlin plugin
5-
// class and can't react to it. To circumvent this test kit limitation, we set the classpath here.
6-
buildscript {
7-
dependencies {
8-
classpath files(pluginClasspath.split(','))
9-
}
1+
plugins {
2+
id 'org.springframework.boot' version '{version}'
103
}
114

12-
apply plugin: 'org.springframework.boot'
13-
145
task kotlinVersion {
156
doLast {
167
def kotlinVersion = project.hasProperty('kotlin.version') ? project.getProperty('kotlin.version') : 'none'

0 commit comments

Comments
 (0)