Merge pull request #10511 from sdeleuze:kotlin-run-extension
* pr/10511: Polish "Add runApplication() Kotlin top level function" Specify kotlin-maven-plugin version for plugin management Add runApplication() Kotlin top level function Add Kotlin main artifacts to dependency management
This commit is contained in:
commit
ae74d198d9
|
|
@ -107,6 +107,7 @@
|
|||
<junit.version>4.12</junit.version>
|
||||
<junit-jupiter.version>5.0.1</junit-jupiter.version>
|
||||
<junit-platform.version>1.0.1</junit-platform.version>
|
||||
<kotlin.version>1.1.51</kotlin.version>
|
||||
<lettuce.version>5.0.0.RELEASE</lettuce.version>
|
||||
<liquibase.version>3.5.3</liquibase.version>
|
||||
<log4j2.version>2.9.1</log4j2.version>
|
||||
|
|
@ -2244,6 +2245,26 @@
|
|||
<artifactId>jdom2</artifactId>
|
||||
<version>${jdom2.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-reflect</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-stdlib</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-stdlib-jre7</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-stdlib-jre8</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jolokia</groupId>
|
||||
<artifactId>jolokia-core</artifactId>
|
||||
|
|
@ -2860,6 +2881,11 @@
|
|||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>kotlin-maven-plugin</groupId>
|
||||
<artifactId>org.jetbrains.kotlin</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.jooq</groupId>
|
||||
<artifactId>jooq-codegen-maven</artifactId>
|
||||
|
|
|
|||
|
|
@ -268,6 +268,14 @@
|
|||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-maven-plugin</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
<configuration>
|
||||
<jvmTarget>${java.version}</jvmTarget>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.asciidoctor</groupId>
|
||||
<artifactId>asciidoctor-maven-plugin</artifactId>
|
||||
|
|
|
|||
|
|
@ -60,6 +60,30 @@
|
|||
<pluginManagement>
|
||||
<plugins>
|
||||
<!-- Apply more sensible defaults for user projects -->
|
||||
<plugin>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-maven-plugin</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
<configuration>
|
||||
<jvmTarget>${java.version}</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>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
|
|
|
|||
|
|
@ -264,6 +264,16 @@
|
|||
<artifactId>snakeyaml</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-stdlib</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-reflect</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<!-- Annotation processing -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
|
@ -377,4 +387,68 @@
|
|||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>compile</id>
|
||||
<phase>compile</phase>
|
||||
<goals>
|
||||
<goal>compile</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<sourceDirs>
|
||||
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
|
||||
<sourceDir>${project.basedir}/src/main/java</sourceDir>
|
||||
</sourceDirs>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>test-compile</id>
|
||||
<phase>test-compile</phase>
|
||||
<goals>
|
||||
<goal>test-compile</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<sourceDirs>
|
||||
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
|
||||
<sourceDir>${project.basedir}/src/test/java</sourceDir>
|
||||
</sourceDirs>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>default-compile</id>
|
||||
<phase>none</phase>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>default-testCompile</id>
|
||||
<phase>none</phase>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>java-compile</id>
|
||||
<phase>compile</phase>
|
||||
<goals>
|
||||
<goal>compile</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>java-test-compile</id>
|
||||
<phase>test-compile</phase>
|
||||
<goals>
|
||||
<goal>testCompile</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
* Copyright 2012-2017 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot
|
||||
|
||||
import org.springframework.context.ConfigurableApplicationContext
|
||||
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
|
||||
/**
|
||||
* Top level function acting as a Kotlin shortcut allowing to write
|
||||
* `runApplication<FooApplication>(arg1, arg2)` instead of
|
||||
* `SpringApplication.run(FooApplication::class.java, arg1, arg2)`.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 2.0.0
|
||||
*/
|
||||
inline fun <reified T : Any> runApplication(vararg args: String): ConfigurableApplicationContext =
|
||||
SpringApplication.run(T::class.java, *args)
|
||||
|
||||
/**
|
||||
* Top level function acting as a Kotlin shortcut allowing to write
|
||||
* `runApplication<FooApplication>(arg1, arg2) { // SpringApplication customization ... }`
|
||||
* instead of instantiating `SpringApplication` class, customize it and then invoking
|
||||
* `run(arg1, arg2)`.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 2.0.0
|
||||
*/
|
||||
inline fun <reified T : Any> runApplication(vararg args: String, init: SpringApplication.() -> Unit): ConfigurableApplicationContext =
|
||||
SpringApplication(T::class.java).apply(init).run(*args)
|
||||
|
||||
|
||||
/**
|
||||
* Top level function acting as a Kotlin shortcut allowing to write
|
||||
* `runApplication(arrayOf(FooApplication::class, FooConfiguration::class), arg1, arg2) { // Optional SpringApplication customization ... }`
|
||||
* instead of instantiating `SpringApplication` class, customize it and then invoking
|
||||
* `run(arrayOf(arg1, arg2))`.`
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 2.0.0
|
||||
*/
|
||||
fun runApplication(primarySources: Array<KClass<*>>, vararg args: String, init: SpringApplication.() -> Unit = {}): ConfigurableApplicationContext =
|
||||
SpringApplication(*primarySources.map { it.java }.toTypedArray()).apply(init).run(*args)
|
||||
|
|
@ -0,0 +1,122 @@
|
|||
/*
|
||||
* Copyright 2002-2017 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.boot
|
||||
|
||||
import org.junit.Assert.assertArrayEquals
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
import org.springframework.beans.factory.getBean
|
||||
import org.springframework.boot.web.servlet.server.MockServletWebServerFactory
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.context.annotation.Configuration
|
||||
import org.springframework.core.env.StandardEnvironment
|
||||
|
||||
/**
|
||||
* Tests for `SpringApplicationExtensions`.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
class SpringApplicationExtensionsTests {
|
||||
|
||||
@Test
|
||||
fun `Kotlin runApplication() top level function`() {
|
||||
val context = runApplication<ExampleWebConfig>()
|
||||
assertNotNull(context)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Kotlin runApplication() top level function with a custom environment`() {
|
||||
val environment = StandardEnvironment()
|
||||
val context = runApplication<ExampleWebConfig> {
|
||||
setEnvironment(environment)
|
||||
}
|
||||
assertNotNull(context)
|
||||
assertEquals(environment, context.environment)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Kotlin runApplication(arg1, arg2) top level function`() {
|
||||
val context = runApplication<ExampleWebConfig>("--debug", "spring", "boot")
|
||||
val args = context.getBean<ApplicationArguments>()
|
||||
assertArrayEquals(arrayOf("spring", "boot"), args.nonOptionArgs.toTypedArray())
|
||||
assertTrue(args.containsOption("debug"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Kotlin runApplication(arg1, arg2) top level function with a custom environment`() {
|
||||
val environment = StandardEnvironment()
|
||||
val context = runApplication<ExampleWebConfig>("--debug", "spring", "boot") {
|
||||
setEnvironment(environment)
|
||||
}
|
||||
val args = context.getBean<ApplicationArguments>()
|
||||
assertArrayEquals(arrayOf("spring", "boot"), args.nonOptionArgs.toTypedArray())
|
||||
assertTrue(args.containsOption("debug"))
|
||||
assertEquals(environment, context.environment)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Kotlin runApplication(array of KClass) top level function`() {
|
||||
val context = runApplication(arrayOf(ExampleConfig::class, ExampleWebConfig::class))
|
||||
assertNotNull(context)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Kotlin runApplication(array of KClass) top level function with a custom environment`() {
|
||||
val environment = StandardEnvironment()
|
||||
val context = runApplication(arrayOf(ExampleConfig::class, ExampleWebConfig::class)) {
|
||||
setEnvironment(environment)
|
||||
}
|
||||
assertNotNull(context)
|
||||
assertEquals(environment, context.environment)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Kotlin runApplication(array of KClass, arg1, arg2) top level function`() {
|
||||
val context = runApplication(arrayOf(ExampleConfig::class, ExampleWebConfig::class), "--debug", "spring", "boot")
|
||||
val args = context.getBean<ApplicationArguments>()
|
||||
assertArrayEquals(arrayOf("spring", "boot"), args.nonOptionArgs.toTypedArray())
|
||||
assertTrue(args.containsOption("debug"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Kotlin runApplication(array of KClass, arg1, arg2) top level function with a custom environment`() {
|
||||
val environment = StandardEnvironment()
|
||||
val context = runApplication(arrayOf(ExampleConfig::class, ExampleWebConfig::class), "--debug", "spring", "boot") {
|
||||
setEnvironment(environment)
|
||||
}
|
||||
val args = context.getBean<ApplicationArguments>()
|
||||
assertArrayEquals(arrayOf("spring", "boot"), args.nonOptionArgs.toTypedArray())
|
||||
assertTrue(args.containsOption("debug"))
|
||||
assertEquals(environment, context.environment)
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
internal open class ExampleConfig
|
||||
|
||||
@Configuration
|
||||
internal open class ExampleWebConfig {
|
||||
|
||||
@Bean
|
||||
open fun webServer(): MockServletWebServerFactory {
|
||||
return MockServletWebServerFactory()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue