IDEA+Gradle+Springboot2.1.5+kotlin 搭建多模块项目

1 用kotlin搭建

项目源代码github地址

https://gitee.com/qiangzhouliang_admin/app-rest-framework/tree/master/app_rest

1.1 创建主项目

进入新建项目窗口,选择 Spring Initializr ,这是spring官方提供的构建springboot demo的网站 https://start.spring.io,也可直接在此网站上初始化项目后,下载后在导入idea

  • 主项目build.gradle配置文件
plugins {
    id 'org.springframework.boot' version '2.1.5.RELEASE'
    // 使用kotlin
    id 'org.jetbrains.kotlin.jvm' version '1.3.41'
    id 'org.jetbrains.kotlin.plugin.spring' version '1.3.41'
    id 'java'
}

repositories {
    mavenCentral()
    maven { url 'https://repo.spring.io/milestone' }
}
subprojects{
    //使用kotlin插件
    apply plugin: 'kotlin' // Required for Kotlin integration
    apply plugin: "kotlin-spring" // https://kotlinlang.org/docs/reference/compiler-plugins.html#spring-support
    apply plugin: 'java'
    apply plugin: 'idea'
    apply plugin: 'maven'
    apply plugin: 'eclipse'
    apply plugin: 'jacoco'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'

    group = 'com.example'
    sourceCompatibility = 1.8
    targetCompatibility = 1.8

    // java编译的时候缺省状态下会因为中文字符而失败
    [compileJava,compileTestJava,javadoc]*.options*.encoding = 'UTF-8'

    bootJar {
        enabled = false  // 默认不需要打可执行jar包
    }
    repositories {
        mavenCentral()
        maven { url 'https://repo.spring.io/milestone' }
    }

    dependencies {
        compile ('org.jetbrains.kotlin:kotlin-stdlib') // Required for Kotlin integration
        testCompile('org.springframework.boot:spring-boot-starter-test')
    }

    uploadArchives {
        repositories {
            mavenDeployer {
                repository(url: "http://XXX:8081/repository/mxq/") {
                    authentication(userName: "XXX", password: "XXX.")
                }
            }
        }
    }
}

删除主项目的src目录

  • setting.gradle配置
    在主项目的setting中将模块包含进来
include('core','service-test')

1.2 创建core模块

同上

  • core模块配置文件
jar{
    enabled = true
}

bootJar {
    enabled = true
}

dependencies {
    // https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.9'

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

删除setting.gradle 、.gitignore 、HELP.md文件

1.3 创建应用项目APP

bootJar {
    enabled = true
}
jar{
    enabled = true
}

dependencies {
    //引入core依赖
    implementation project(':core')
    implementation 'org.jetbrains.kotlin:kotlin-stdlib'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

同样删除删除setting.gradle 、.gitignore 、HELP.md文件

1.4 启动类

package com.example.servicetest

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication

@SpringBootApplication
class ServiceTestApplication

fun main(args: Array<String>) {
    SpringApplication.run(ServiceTestApplication::class.java, *args)
}

1.5 测试

package com.example.servicetest

import com.example.core.Test
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController

@RestController
class TestController {
    @GetMapping("/testK")
    fun getString():String{
        // test 为 core里面的一个类
        val test = Test()
        return "姓名:${test.getName()}"
    }
}

2 kotlin使用配置网址

https://kotlinlang.org/docs/reference/using-gradle.html

2.1 kotlin Spring支持

https://kotlinlang.org/docs/reference/compiler-plugins.html#spring-support

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值