Harmonize Kotlin's jvmTarget to chosen JVM generation

Closes gh-1084
This commit is contained in:
Stephane Nicoll
2020-06-10 15:56:55 +02:00
parent ca4bd57139
commit f9a7404c39
8 changed files with 44 additions and 22 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@@ -85,7 +85,7 @@ public class KotlinProjectGenerationConfiguration {
String kotlinVersion = kotlinVersionResolver
.getIfAvailable(() -> new InitializrMetadataKotlinVersionResolver(metadata))
.resolveKotlinVersion(this.description);
return new SimpleKotlinProjectSettings(kotlinVersion);
return new SimpleKotlinProjectSettings(kotlinVersion, this.description.getLanguage().jvmVersion());
}
@Bean

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@@ -37,9 +37,7 @@ public interface KotlinProjectSettings {
* Return the {@code jvmTarget} to use.
* @return the jvm target
*/
default String getJvmTarget() {
return "1.8";
}
String getJvmTarget();
/**
* Return the compiler arguments.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@@ -16,6 +16,8 @@
package io.spring.initializr.generator.spring.code.kotlin;
import io.spring.initializr.generator.language.Language;
/**
* Commons settings for Kotlin projects.
*
@@ -25,8 +27,25 @@ public class SimpleKotlinProjectSettings implements KotlinProjectSettings {
private final String version;
private final String jvmTarget;
/**
* Create an instance with the kotlin version to use.
* @param version the kotlin version to use
*/
public SimpleKotlinProjectSettings(String version) {
this(version, Language.DEFAULT_JVM_VERSION);
}
/**
* Create an instance with the kotlin version and the target version of the generated
* JVM bytecode.
* @param version the kotlin version to use
* @param jvmTarget the target version of the generated JVM bytecode
*/
public SimpleKotlinProjectSettings(String version, String jvmTarget) {
this.version = version;
this.jvmTarget = jvmTarget;
}
@Override
@@ -34,4 +53,9 @@ public class SimpleKotlinProjectSettings implements KotlinProjectSettings {
return this.version;
}
@Override
public String getJvmTarget() {
return this.jvmTarget;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@@ -43,15 +43,15 @@ class GroovyDslKotlinGradleBuildCustomizerTests {
@Test
void kotlinCompilationTasksAreCustomized() {
GradleBuild build = new GradleBuild();
new GroovyDslKotlinGradleBuildCustomizer(new SimpleKotlinProjectSettings("1.2.70")).customize(build);
new GroovyDslKotlinGradleBuildCustomizer(new SimpleKotlinProjectSettings("1.2.70", "11")).customize(build);
assertThat(build.tasks().importedTypes()).contains("org.jetbrains.kotlin.gradle.tasks.KotlinCompile");
assertThat(build.tasks().values()).hasOnlyOneElementSatisfying((task) -> {
assertThat(task.getName()).isEqualTo("KotlinCompile");
assertKotlinOptions(task);
assertKotlinOptions(task, "11");
});
}
private void assertKotlinOptions(GradleTask compileTask) {
private void assertKotlinOptions(GradleTask compileTask, String jvmTarget) {
assertThat(compileTask.getAttributes()).isEmpty();
assertThat(compileTask.getInvocations()).isEmpty();
assertThat(compileTask.getNested()).hasSize(1);
@@ -60,7 +60,7 @@ class GroovyDslKotlinGradleBuildCustomizerTests {
assertThat(kotlinOptions.getNested()).hasSize(0);
assertThat(kotlinOptions.getAttributes()).hasSize(2);
assertThat(kotlinOptions.getAttributes()).containsEntry("freeCompilerArgs", "['-Xjsr305=strict']")
.containsEntry("jvmTarget", "'1.8'");
.containsEntry("jvmTarget", String.format("'%s'", jvmTarget));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@@ -42,15 +42,15 @@ class KotlinDslKotlinGradleBuildCustomizerTests {
@Test
void kotlinCompilationTasksAreCustomized() {
GradleBuild build = new GradleBuild();
new KotlinDslKotlinGradleBuildCustomizer(new SimpleKotlinProjectSettings("1.2.70")).customize(build);
new KotlinDslKotlinGradleBuildCustomizer(new SimpleKotlinProjectSettings("1.2.70", "11")).customize(build);
assertThat(build.tasks().importedTypes()).contains("org.jetbrains.kotlin.gradle.tasks.KotlinCompile");
assertThat(build.tasks().values()).hasOnlyOneElementSatisfying((task) -> {
assertThat(task.getName()).isEqualTo("KotlinCompile");
assertKotlinOptions(task);
assertKotlinOptions(task, "11");
});
}
private void assertKotlinOptions(GradleTask compileTask) {
private void assertKotlinOptions(GradleTask compileTask, String jvmTarget) {
assertThat(compileTask.getAttributes()).isEmpty();
assertThat(compileTask.getInvocations()).isEmpty();
assertThat(compileTask.getNested()).hasSize(1);
@@ -59,7 +59,7 @@ class KotlinDslKotlinGradleBuildCustomizerTests {
assertThat(kotlinOptions.getNested()).hasSize(0);
assertThat(kotlinOptions.getAttributes()).hasSize(2);
assertThat(kotlinOptions.getAttributes()).containsEntry("freeCompilerArgs", "listOf(\"-Xjsr305=strict\")")
.containsEntry("jvmTarget", "\"1.8\"");
.containsEntry("jvmTarget", String.format("\"%s\"", jvmTarget));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@@ -53,7 +53,7 @@ class KotlinMavenFullBuildCustomizerTests {
@Test
void kotlinMavenPluginIsConfigured() {
MavenBuild build = new MavenBuild();
new KotlinMavenFullBuildCustomizer(new SimpleKotlinProjectSettings("1.2.70")).customize(build);
new KotlinMavenFullBuildCustomizer(new SimpleKotlinProjectSettings("1.2.70", "1.6")).customize(build);
assertThat(build.plugins().values()).hasOnlyOneElementSatisfying((kotlinPlugin) -> {
assertThat(kotlinPlugin.getGroupId()).isEqualTo("org.jetbrains.kotlin");
assertThat(kotlinPlugin.getArtifactId()).isEqualTo("kotlin-maven-plugin");
@@ -73,7 +73,7 @@ class KotlinMavenFullBuildCustomizerTests {
.hasFieldOrPropertyWithValue("value", "spring");
Setting jvmTarget = configuration.getSettings().get(2);
assertThat(jvmTarget.getName()).isEqualTo("jvmTarget");
assertThat(jvmTarget.getValue()).isEqualTo("1.8");
assertThat(jvmTarget.getValue()).isEqualTo("1.6");
assertThat(kotlinPlugin.getExecutions()).hasSize(2);
Execution compile = kotlinPlugin.getExecutions().get(0);
assertThat(compile.getId()).isEqualTo("compile");

View File

@@ -25,6 +25,6 @@ dependencies {
tasks.withType(KotlinCompile) {
kotlinOptions {
freeCompilerArgs = ['-Xjsr305=strict']
jvmTarget = '1.8'
jvmTarget = '11'
}
}

View File

@@ -25,6 +25,6 @@ dependencies {
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
jvmTarget = "11"
}
}