Add support for Kotlin's runApplication

Spring Boot 2.0.0.M6 offers a better integration with Kotlin. A
top-level `runApplication` function can be used to start the app and
additional dependency and plugin management are provided.

This commit simplifies the way a Kotlin-based project is generated when
running with a compatible version

Closes gh-534
This commit is contained in:
Stephane Nicoll
2017-10-27 14:18:05 +02:00
parent 9d7b2e95d6
commit c33b1df90c
11 changed files with 425 additions and 16 deletions

View File

@@ -80,6 +80,8 @@ public class ProjectGenerator {
private static final Version VERSION_2_0_0_M3 = Version.parse("2.0.0.M3");
private static final Version VERSION_2_0_0_M6 = Version.parse("2.0.0.M6");
@Autowired
private ApplicationEventPublisher eventPublisher;
@@ -344,7 +346,16 @@ public class ProjectGenerator {
model.put("war", true);
}
// Kotlin supported as of M6
final boolean kotlinSupport = VERSION_2_0_0_M6
.compareTo(Version.safeParse(request.getBootVersion())) <= 0;
model.put("kotlinSupport", kotlinSupport);
if (isMavenBuild(request)) {
if (kotlinSupport) { // No need to provide a kotlin.version
request.getBuildProperties().getVersions()
.remove(new VersionProperty("kotlin.version"));
}
model.put("mavenBuild", true);
ParentPom parentPom = metadata.getConfiguration().getEnv().getMaven()
.resolveParentPom(request.getBootVersion());
@@ -433,8 +444,8 @@ public class ProjectGenerator {
model.put("newTestInfrastructure", isNewTestInfrastructureAvailable(request));
// Servlet Initializer
model.put("servletInitializrImport", generateImport(getServletInitializrClass(request),
request.getLanguage()));
model.put("servletInitializrImport", new Imports(request.getLanguage()).add(
getServletInitializrClass(request)).toString());
// Java versions
model.put("isJava6", isJavaVersion(request, "1.6"));
@@ -538,17 +549,6 @@ public class ProjectGenerator {
}
}
protected String addImport(String type, String language) {
return String.format("%s%n", generateImport(type, language));
}
protected String generateImport(String type, String language) {
String end = ("groovy".equals(language) || "kotlin".equals(language)) ? "" : ";";
return "import " + type + end;
}
private static boolean isGradleBuild(ProjectRequest request) {
return "gradle".equals(request.getBuild());
}

View File

@@ -1,11 +1,21 @@
package {{packageName}}
{{^kotlinSupport}}
import org.springframework.boot.SpringApplication
{{/kotlinSupport}}
{{applicationImports}}
{{#kotlinSupport}}
import org.springframework.boot.runApplication
{{/kotlinSupport}}
{{applicationAnnotations}}
class {{applicationName}}
fun main(args: Array<String>) {
{{^kotlinSupport}}
SpringApplication.run({{applicationName}}::class.java, *args)
{{/kotlinSupport}}
{{#kotlinSupport}}
runApplication<{{applicationName}}>(*args)
{{/kotlinSupport}}
}

View File

@@ -87,12 +87,12 @@ dependencies {
compile("org.jetbrains.kotlin:kotlin-stdlib:${kotlinVersion}")
{{/isJava6}}
{{#isJava7}}
compile("org.jetbrains.kotlin:kotlin-stdlib-jre7:${kotlinVersion}")
compile("org.jetbrains.kotlin:kotlin-stdlib-jre7{{^kotlinSupport}}:${kotlinVersion}{{/kotlinSupport}}")
{{/isJava7}}
{{#isJava8}}
compile("org.jetbrains.kotlin:kotlin-stdlib-jre8:${kotlinVersion}")
compile("org.jetbrains.kotlin:kotlin-stdlib-jre8{{^kotlinSupport}}:${kotlinVersion}{{/kotlinSupport}}")
{{/isJava8}}
compile("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
compile("org.jetbrains.kotlin:kotlin-reflect{{^kotlinSupport}}:${kotlinVersion}{{/kotlinSupport}}")
{{/kotlin}}
{{#runtimeDependencies}}
runtime('{{groupId}}:{{artifactId}}{{#version}}:{{version}}{{/version}}{{#type}}@{{type}}{{/type}}')

View File

@@ -58,12 +58,16 @@
{{#isJava8}}
<artifactId>kotlin-stdlib-jre8</artifactId>
{{/isJava8}}
{{^kotlinSupport}}
<version>${kotlin.version}</version>
{{/kotlinSupport}}
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
{{^kotlinSupport}}
<version>${kotlin.version}</version>
{{/kotlinSupport}}
</dependency>
{{/kotlin}}
@@ -177,15 +181,20 @@
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
{{^kotlinSupport}}
<version>${kotlin.version}</version>
{{/kotlinSupport}}
<configuration>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
{{^kotlinSupport}}
{{#isJava8}}
<jvmTarget>1.8</jvmTarget>
{{/isJava8}}
{{/kotlinSupport}}
</configuration>
{{^kotlinSupport}}
<executions>
<execution>
<id>compile</id>
@@ -202,6 +211,7 @@
</goals>
</execution>
</executions>
{{/kotlinSupport}}
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>

View File

@@ -191,6 +191,30 @@ public class ProjectGeneratorBuildTests extends AbstractProjectGeneratorTests {
"project/" + build + "/kotlin-java7-" + assertFileName));
}
@Test
public void kotlinSpringBoot2Legacy() {
ProjectRequest request = createProjectRequest();
request.setLanguage("kotlin");
request.setBootVersion("2.0.0.M5");
ProjectAssert project = generateProject(request);
project.sourceCodeAssert("src/main/kotlin/com/example/demo/DemoApplication.kt")
.equalsTo(new ClassPathResource("project/kotlin/spring-boot-2.0/DemoApplicationLegacy.kt.gen"));
project.sourceCodeAssert(fileName).equalsTo(new ClassPathResource(
"project/" + build + "/kotlin-springboot2-legacy-" + assertFileName));
}
@Test
public void kotlinSpringBoot2() {
ProjectRequest request = createProjectRequest();
request.setLanguage("kotlin");
request.setBootVersion("2.0.0.M6");
ProjectAssert project = generateProject(request);
project.sourceCodeAssert("src/main/kotlin/com/example/demo/DemoApplication.kt")
.equalsTo(new ClassPathResource("project/kotlin/spring-boot-2.0/DemoApplication.kt.gen"));
project.sourceCodeAssert(fileName).equalsTo(new ClassPathResource(
"project/" + build + "/kotlin-springboot2-" + assertFileName));
}
@Override
public ProjectRequest createProjectRequest(String... styles) {
ProjectRequest request = super.createProjectRequest(styles);

View File

@@ -0,0 +1,46 @@
buildscript {
ext {
kotlinVersion = '1.1.1'
springBootVersion = '2.0.0.M6'
}
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
}
}
apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
compile('org.springframework.boot:spring-boot-starter')
compile("org.jetbrains.kotlin:kotlin-stdlib-jre8")
compile("org.jetbrains.kotlin:kotlin-reflect")
testCompile('org.springframework.boot:spring-boot-starter-test')
}

View File

@@ -0,0 +1,46 @@
buildscript {
ext {
kotlinVersion = '1.1.1'
springBootVersion = '2.0.0.M5'
}
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
}
}
apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
compile('org.springframework.boot:spring-boot-starter')
compile("org.jetbrains.kotlin:kotlin-stdlib-jre8:${kotlinVersion}")
compile("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
testCompile('org.springframework.boot:spring-boot-starter-test')
}

View File

@@ -0,0 +1,11 @@
package com.example.demo
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
@SpringBootApplication
class DemoApplication
fun main(args: Array<String>) {
runApplication<DemoApplication>(*args)
}

View File

@@ -0,0 +1,11 @@
package com.example.demo
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
@SpringBootApplication
class DemoApplication
fun main(args: Array<String>) {
SpringApplication.run(DemoApplication::class.java, *args)
}

View File

@@ -0,0 +1,136 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.M5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<kotlin.version>1.1.1</kotlin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jre8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<configuration>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
<jvmTarget>1.8</jvmTarget>
</configuration>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>

View File

@@ -0,0 +1,115 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.M6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jre8</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<configuration>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>