美文网首页
搭建 Kotlin 学习环境(SpringBoot 版)

搭建 Kotlin 学习环境(SpringBoot 版)

作者: codergege | 来源:发表于2018-11-13 17:39 被阅读0次

回到目录
项目源码 hello-kotlin 项目


想看用记事本, 命令行并手动构建运行 kotlin 代码的可以参考 官方文档

咱们直接使用 gradle 搭建 spring boot 框架来学习 kotlin 这门语言(因为我相信大家都很牛逼).

不知道 gradle 怎么用的兄弟不要慌, 不会影响学习 kotlin, 把我的 build.gradle 文件复制
到你的项目下, 跟着操作就完事了.

开发环境搭建

  1. Java

需要 java 8 或以上版本.

  1. Gradle

4.0 以上版本

  1. Kotlin

安装 kotlin 1.2.7 以上版本

推荐安装最新版本 v1.3 下载地址

  1. Idea 社区版
    免费的就够了.

SpringBoot 手脚架

进入 SpringBoot Initializer

选择 Gradle Project with Kotlin

无需要选 Dependencies, 暂时用不到

点击 Switch to the full version 依次填入 Group, Artifact, Name...

修改一下包名, 生成项目, 下载下来, 解压到 workspace/kotlin-learning 下

hello-kotlin-1.png

替换 gradle.build

用自己写的 build.gradle 配置文件覆盖自动生成的. 里面干了很多事情, 比如依赖下载仓库换成了阿里的仓库, 下载 jar 包速度飞快; 比如换成了 junit5 测试框架; 加入了自动测试的配置; 打包的配置; 等等...

buildscript {
    ext {
        kotlinVersion = '1.2.70'
        springBootVersion = '2.1.0.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.junit.platform:junit-platform-gradle-plugin:1.1.0'
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
        classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
        classpath "org.jetbrains.kotlin:kotlin-noarg:${kotlinVersion}" // 无参插件
    }
}

apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
// apply plugin: 'kotlin-kapt'
// apply plugin: 'kotlin-jpa'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

apply plugin: 'org.junit.platform.gradle.plugin'
//apply plugin: 'jacoco'

apply plugin: 'application'
apply plugin: 'eclipse'
apply plugin: 'idea'

// group = 'cn.codergege'
// version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

// 这里改一下包名
project.ext.moduleName = 'demo'

// tag::variables[]{{{
// 版本号
project.ext.versionFile = file('version.properties')
project.ext.jarVersion = readVersion()
project.ext.packageName = "cn.codergege.$moduleName"
// Main class
project.ext.mainClass = "${packageName}.${convertProjectName()}ApplicationKt"
// end::variables[]}}}

// tag::jar[]{{{
bootJar {
    mainClassName = mainClass
    launchScript()
}
// end::jar[]}}}

defaultTasks 'bootRun'

repositories {
    maven {
        url "http://maven.aliyun.com/nexus/content/groups/public/"
    }
    jcenter()    
    mavenCentral()
}

dependencies {
    // tag::versions{{{
    ext { 
        // 数据库
        // mysqlDriverVersion = "5.1.46"
        // druidVersion = "1.1.9"
        // querydslVersion = "4.2.1"
    }
    // end::versions}}}

    // - 本地依赖, ojdbc6.jar
    compile fileTree(dir:'lib', include:['*.jar'])

    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("org.jetbrains.kotlin:kotlin-reflect")

    implementation('org.springframework.boot:spring-boot-starter')
    // implementation('org.springframework.boot:spring-boot-starter-web')
    // implementation('com.fasterxml.jackson.module:jackson-module-kotlin')

    // compile "org.springframework.boot:spring-boot-starter-data-jpa"
    // compile "org.springframework.boot:spring-boot-starter-cache"
    // compile("org.springframework.boot:spring-boot-starter-batch") {
        // exclude module: "hsqldb"
    // }

    // for test profiles
    // compile "com.h2database:h2"
    // for dev and prod profiles
    // compile "mysql:mysql-connector-java:$mysqlDriverVersion"
    // compile "com.alibaba:druid:$druidVersion"

    // tag::querydsl[]
    // https://mvnrepository.com/artifact/com.querydsl/querydsl-jpa
    // compile "com.querydsl:querydsl-jpa:$querydslVersion"
    // compile "com.querydsl:querydsl-apt:$querydslVersion:jpa"
    // compileOnly "com.querydsl:querydsl-apt:$querydslVersion"
    // kapt "com.querydsl:querydsl-apt:$querydslVersion"
    // end::querydsl[]
    
    // tag::tests[]
    testImplementation('org.springframework.boot:spring-boot-starter-test'){
        exclude group: 'junit', module: 'junit'
    }
    // testImplementation "org.mockito:mockito-core:2.+"
    testImplementation("org.junit.jupiter:junit-jupiter-api")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")    
    // end::tests[]
}

// kapt {
    // correctErrorTypes = true
// }

// tag::编译设置[]
//{{{
compileKotlin {
    kotlinOptions {
        freeCompilerArgs = ["-Xjsr305=strict"]
        jvmTarget = "1.8"
    }
}
compileTestKotlin {
    kotlinOptions {
        freeCompilerArgs = ["-Xjsr305=strict"]
        jvmTarget = "1.8"
    }
}

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

tasks.withType(JavaCompile) {  
    options.encoding = 'UTF-8'  
}  
//}}}
// end::编译设置[]
// ...

Idea 导入项目

打开 idea, file -> open 选择项目

hello-kotlin-2.png

注意勾选 auto-import 和 use local gradle distribution


hello-kotlin-3.png

修改 test 部分的源码

默认生成的 test 代码用的是 junit4, 现在我们指定了使用 junit5. 所以需要修改一下.

hello-kotlin-4.png

将标红的那些去掉, 然后 import org.junit.jupiter.api.Test

运行

在 main 函数和测试类中分别写上打印 "Hello Kotlin" 的代码

main 函数中:

@SpringBootApplication
class HelloKotlinApplication

fun main(args: Array<String>) {
    runApplication<HelloKotlinApplication>(*args)
    println("Hello, Kotlin!")
}

测试类中:

@SpringBootTest
class HelloKotlinApplicationTests {

    @Test
    fun contextLoads() {
    }

    @Test fun helloKotlin() {
        println("Hello, Kotlin!")
    }
}

可以在终端运行命令 gradle test, gradle(默认 gradle bootJar)

或者在 idea 中点绿色箭头按钮测试或运行

或者打开 idea 中的 gradle 视图选择对应的 task 运行.

可以选择 junitPlatformTest 任务, 生成可读性更好的树型组织的测试报告.


hello-kotlin-5.png

总结

这样我们学习 kotlin 的学习环境已经搭好, 接下来只需要敲代码, 然后 gradle test,
gradle 进行测试运行.


回到目录

相关文章

网友评论

      本文标题:搭建 Kotlin 学习环境(SpringBoot 版)

      本文链接:https://www.haomeiwen.com/subject/dxvlfqtx.html