Skip to content

Commit bf4c572

Browse files
committed
Merge pull request #12641 from Rui Figueira
* gh-12641: Polish “Configure Kotlin compiler to use -java-parameters by default” Configure Kotlin compiler to use -java-parameters by default
2 parents 404f22e + 1f35aba commit bf4c572

File tree

8 files changed

+75
-4
lines changed

8 files changed

+75
-4
lines changed

spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -532,10 +532,11 @@ JSON request body. When exposed via JMX, the parameters are mapped to the parame
532532
the MBean's operations. Parameters are required by default. They can be made optional
533533
by annotating them with `@org.springframework.lang.Nullable`.
534534

535-
NOTE: To allow the input to be mapped to the operation method's parameters, code
536-
implementing an endpoint should be compiled with `-parameters`. This will happen
537-
automatically if you are using Spring Boot's Gradle plugin or if you are using Maven
538-
and `spring-boot-starter-parent`.
535+
NOTE: To allow the input to be mapped to the operation method's parameters, Java code
536+
implementing an endpoint should be compiled with `-parameters`, and Kotlin code
537+
implementing an endpoint should be compiled with `-java-parameters`. This will happen
538+
automatically if you are using Spring Boot's Gradle plugin or if you are using Maven and
539+
`spring-boot-starter-parent`.
539540

540541

541542

spring-boot-project/spring-boot-parent/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@
294294
<jvmTarget>${java.version}</jvmTarget>
295295
<apiVersion>1.1</apiVersion>
296296
<languageVersion>1.1</languageVersion>
297+
<javaParameters>true</javaParameters>
297298
</configuration>
298299
</plugin>
299300
<plugin>

spring-boot-project/spring-boot-starters/spring-boot-starter-parent/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
<version>${kotlin.version}</version>
5252
<configuration>
5353
<jvmTarget>${java.version}</jvmTarget>
54+
<javaParameters>true</javaParameters>
5455
</configuration>
5556
<executions>
5657
<execution>

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/asciidoc/reacting.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ plugin:
3636
1. Aligns the Kotlin version used in Spring Boot's dependency management with the version
3737
of the plugin. This is achieved by setting the `kotlin.version` property with a value
3838
that matches the version of the Kotlin plugin.
39+
2. Configures any `KotlinCompile` tasks to use the `-java-parameters` compiler argument.
3940

4041

4142

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/KotlinPluginAction.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.gradle.api.Project;
2121
import org.gradle.api.plugins.ExtraPropertiesExtension;
2222
import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper;
23+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile;
2324

2425
/**
2526
* {@link PluginApplicationAction} that reacts to Kotlin's Gradle plugin being applied by
@@ -39,6 +40,12 @@ public void execute(Project project) {
3940
if (!extraProperties.has("kotlin.version")) {
4041
extraProperties.set("kotlin.version", kotlinVersion);
4142
}
43+
enableJavaParametersOption(project);
44+
}
45+
46+
private void enableJavaParametersOption(Project project) {
47+
project.getTasks().withType(KotlinCompile.class,
48+
(compile) -> compile.getKotlinOptions().setJavaParameters(true));
4249
}
4350

4451
@Override

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,18 @@ public void kotlinVersionMatchesKotlinPluginVersion() {
5252
.containsPattern("org.jetbrains.kotlin:kotlin-stdlib-jdk8:* -> 1.2.10");
5353
}
5454

55+
@Test
56+
public void kotlinCompileTasksUseJavaParametersFlagByDefault() {
57+
assertThat(this.gradleBuild.build("kotlinCompileTasksJavaParameters").getOutput())
58+
.contains("compileKotlin java parameters: true")
59+
.contains("compileTestKotlin java parameters: true");
60+
}
61+
62+
@Test
63+
public void kotlinCompileTasksCanOverrideDefaultJavaParametersFlag() {
64+
assertThat(this.gradleBuild.build("kotlinCompileTasksJavaParameters").getOutput())
65+
.contains("compileKotlin java parameters: false")
66+
.contains("compileTestKotlin java parameters: false");
67+
}
68+
5569
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
buildscript {
2+
dependencies {
3+
classpath files(pluginClasspath.split(','))
4+
}
5+
}
6+
7+
plugins {
8+
id 'org.jetbrains.kotlin.jvm' version '1.2.10'
9+
}
10+
11+
apply plugin: 'org.springframework.boot'
12+
13+
import org.jetbrains.kotlin.gradle.dsl.KotlinCompile
14+
15+
tasks.withType(KotlinCompile) {
16+
kotlinOptions.javaParameters = false
17+
}
18+
19+
task('kotlinCompileTasksJavaParameters') {
20+
doFirst {
21+
tasks.withType(KotlinCompile) {
22+
println "$name java parameters: ${kotlinOptions.javaParameters}"
23+
}
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
buildscript {
2+
dependencies {
3+
classpath files(pluginClasspath.split(','))
4+
}
5+
}
6+
7+
plugins {
8+
id 'org.jetbrains.kotlin.jvm' version '1.2.10'
9+
}
10+
11+
apply plugin: 'org.springframework.boot'
12+
13+
import org.jetbrains.kotlin.gradle.dsl.KotlinCompile
14+
15+
task('kotlinCompileTasksJavaParameters') {
16+
doFirst {
17+
tasks.withType(KotlinCompile) {
18+
println "$name java parameters: ${kotlinOptions.javaParameters}"
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)