Commit bf4c5720 authored by Andy Wilkinson's avatar Andy Wilkinson

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
parents 404f22e5 1f35abac
...@@ -532,10 +532,11 @@ JSON request body. When exposed via JMX, the parameters are mapped to the parame ...@@ -532,10 +532,11 @@ JSON request body. When exposed via JMX, the parameters are mapped to the parame
the MBean's operations. Parameters are required by default. They can be made optional the MBean's operations. Parameters are required by default. They can be made optional
by annotating them with `@org.springframework.lang.Nullable`. by annotating them with `@org.springframework.lang.Nullable`.
NOTE: To allow the input to be mapped to the operation method's parameters, code NOTE: To allow the input to be mapped to the operation method's parameters, Java code
implementing an endpoint should be compiled with `-parameters`. This will happen implementing an endpoint should be compiled with `-parameters`, and Kotlin code
automatically if you are using Spring Boot's Gradle plugin or if you are using Maven implementing an endpoint should be compiled with `-java-parameters`. This will happen
and `spring-boot-starter-parent`. automatically if you are using Spring Boot's Gradle plugin or if you are using Maven and
`spring-boot-starter-parent`.
......
...@@ -294,6 +294,7 @@ ...@@ -294,6 +294,7 @@
<jvmTarget>${java.version}</jvmTarget> <jvmTarget>${java.version}</jvmTarget>
<apiVersion>1.1</apiVersion> <apiVersion>1.1</apiVersion>
<languageVersion>1.1</languageVersion> <languageVersion>1.1</languageVersion>
<javaParameters>true</javaParameters>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>
......
...@@ -51,6 +51,7 @@ ...@@ -51,6 +51,7 @@
<version>${kotlin.version}</version> <version>${kotlin.version}</version>
<configuration> <configuration>
<jvmTarget>${java.version}</jvmTarget> <jvmTarget>${java.version}</jvmTarget>
<javaParameters>true</javaParameters>
</configuration> </configuration>
<executions> <executions>
<execution> <execution>
......
...@@ -36,6 +36,7 @@ plugin: ...@@ -36,6 +36,7 @@ plugin:
1. Aligns the Kotlin version used in Spring Boot's dependency management with the version 1. Aligns the Kotlin version used in Spring Boot's dependency management with the version
of the plugin. This is achieved by setting the `kotlin.version` property with a value of the plugin. This is achieved by setting the `kotlin.version` property with a value
that matches the version of the Kotlin plugin. that matches the version of the Kotlin plugin.
2. Configures any `KotlinCompile` tasks to use the `-java-parameters` compiler argument.
......
...@@ -20,6 +20,7 @@ import org.gradle.api.Plugin; ...@@ -20,6 +20,7 @@ import org.gradle.api.Plugin;
import org.gradle.api.Project; import org.gradle.api.Project;
import org.gradle.api.plugins.ExtraPropertiesExtension; import org.gradle.api.plugins.ExtraPropertiesExtension;
import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper; import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper;
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile;
/** /**
* {@link PluginApplicationAction} that reacts to Kotlin's Gradle plugin being applied by * {@link PluginApplicationAction} that reacts to Kotlin's Gradle plugin being applied by
...@@ -39,6 +40,12 @@ class KotlinPluginAction implements PluginApplicationAction { ...@@ -39,6 +40,12 @@ class KotlinPluginAction implements PluginApplicationAction {
if (!extraProperties.has("kotlin.version")) { if (!extraProperties.has("kotlin.version")) {
extraProperties.set("kotlin.version", kotlinVersion); extraProperties.set("kotlin.version", kotlinVersion);
} }
enableJavaParametersOption(project);
}
private void enableJavaParametersOption(Project project) {
project.getTasks().withType(KotlinCompile.class,
(compile) -> compile.getKotlinOptions().setJavaParameters(true));
} }
@Override @Override
......
...@@ -52,4 +52,18 @@ public class KotlinPluginActionIntegrationTests { ...@@ -52,4 +52,18 @@ public class KotlinPluginActionIntegrationTests {
.containsPattern("org.jetbrains.kotlin:kotlin-stdlib-jdk8:* -> 1.2.10"); .containsPattern("org.jetbrains.kotlin:kotlin-stdlib-jdk8:* -> 1.2.10");
} }
@Test
public void kotlinCompileTasksUseJavaParametersFlagByDefault() {
assertThat(this.gradleBuild.build("kotlinCompileTasksJavaParameters").getOutput())
.contains("compileKotlin java parameters: true")
.contains("compileTestKotlin java parameters: true");
}
@Test
public void kotlinCompileTasksCanOverrideDefaultJavaParametersFlag() {
assertThat(this.gradleBuild.build("kotlinCompileTasksJavaParameters").getOutput())
.contains("compileKotlin java parameters: false")
.contains("compileTestKotlin java parameters: false");
}
} }
buildscript {
dependencies {
classpath files(pluginClasspath.split(','))
}
}
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.2.10'
}
apply plugin: 'org.springframework.boot'
import org.jetbrains.kotlin.gradle.dsl.KotlinCompile
tasks.withType(KotlinCompile) {
kotlinOptions.javaParameters = false
}
task('kotlinCompileTasksJavaParameters') {
doFirst {
tasks.withType(KotlinCompile) {
println "$name java parameters: ${kotlinOptions.javaParameters}"
}
}
}
buildscript {
dependencies {
classpath files(pluginClasspath.split(','))
}
}
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.2.10'
}
apply plugin: 'org.springframework.boot'
import org.jetbrains.kotlin.gradle.dsl.KotlinCompile
task('kotlinCompileTasksJavaParameters') {
doFirst {
tasks.withType(KotlinCompile) {
println "$name java parameters: ${kotlinOptions.javaParameters}"
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment