创建Kotlin项目

创建 kotlin maven 项目

通过 start.srping.io 项目创建基于maven构建系统的kotlin项目

1
2
3
4
5
6
7
8
9
10
11
curl -o starter.zip  "https://start.spring.io/starter.zip?type=maven-project\
&language=kotlin\
&bootVersion=2.5.3\
&baseDir=demo\
&groupId=com.example\
&artifactId=demo\
&name=demo\
&description=Demo%20project%20for%20Spring%20Boot\
&packageName=com.example.demo\
&packaging=jar\
&javaVersion=11"

由生成的maven构建文件pom.xml中的配置可知:

dependency章节加了kotlin-reflect(反射)kotlin-stdlib

1
2
3
4
5
6
7
8
9
10
...
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
</dependency>
...

plugin章节中添加kotlin-maven-plugin插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<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>

创建 kotlin gradle 项目

通过 start.srping.io 项目创建基于 gradle 构建系统的kotlin项目

1
2
3
4
5
6
7
8
9
10
11
curl -o starter.zip  "https://start.spring.io/starter.zip?type=gradle-project\
&language=kotlin\
&bootVersion=2.5.3\
&baseDir=demo\
&groupId=com.example\
&artifactId=demo\
&name=demo\
&description=Demo%20project%20for%20Spring%20Boot\
&packageName=com.example.demo\
&packaging=jar\
&javaVersion=11"

gradle构建工具的构建脚本build.gradle现已支持kotlin脚本,
如果项目语言选择了kotlin语言,默认使用kotlin编写build.gradle,并以kts结尾,如:build.gradle.kts.
如果项目语言选择了java语言,使用groovy编写build.gradle,如:build.gradle.

对比 settings.gradle 与 build.gradle的差异

由此可见kotlingroovy语法略有不同

settings.gradle.kts

1
rootProject.name = "demo"

settings.gradle

1
rootProject.name = 'demo'

build.gradle.kts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
id("org.springframework.boot") version "2.5.3"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
kotlin("jvm") version "1.5.21"
kotlin("plugin.spring") version "1.5.21"
}

group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11

repositories {
mavenCentral()
}

dependencies {
implementation("org.springframework.boot:spring-boot-starter")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}

tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "11"
}
}

tasks.withType<Test> {
useJUnitPlatform()
}

build.gradle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
plugins {
id 'org.springframework.boot' version '2.5.3'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

test {
useJUnitPlatform()
}