Skip to content

Commit 5e2726f

Browse files
ruifigueirawilkinsona
authored andcommitted
Configure Kotlin compiler to use -java-parameters by default
See gh-12641
1 parent 404f22e commit 5e2726f

File tree

8 files changed

+74
-6
lines changed

8 files changed

+74
-6
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -532,11 +532,10 @@ 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`.
539-
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 should
537+
be compiled with `-java-parameters`. This will happen automatically if you are using
538+
Spring Boot's Gradle plugin or if you are using Maven and `spring-boot-starter-parent`.
540539

541540

542541
[[production-ready-endpoints-custom-input-conversion]]

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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ plugin:
2424
the `bootJar` task.
2525
6. Configures any `JavaCompile` tasks with no configured encoding to use `UTF-8`.
2626
7. Configures any `JavaCompile` tasks to use the `-parameters` compiler argument.
27-
27+
8. Configures any `KotlinCompile` tasks to use the `-java-parameters` compiler argument.
2828

2929

3030
[[reacting-to-other-plugins-kotlin]]

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

Lines changed: 8 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,13 @@ 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, (compile) -> {
48+
compile.getKotlinOptions().setJavaParameters(true);
49+
});
4250
}
4351

4452
@Override

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,17 @@ 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+
}
5568
}
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)