Kotlin Gradle:如何在intellij中添加另一个spring boot项目作为模块依赖项

时间:2019-03-16 14:06:08

标签: java spring-boot gradle kotlin build

我有两个Spring Boot项目

Project1 (Spring boot project)
   |
   |--build.gradle
   |--settings.gradle
   |--src/main/kotlin

Project2 (Spring boot project)
   |
   |--build.gradle
   |--settings.gradle
   |--src/main/kotlin

Project2依赖于Project1。有人可以告诉我如何添加此依赖项(例如,修改build.gradle和settings.gradle),以便这些项目可以正常运行。我已经尝试了很多资源,但没有运气实现这一目标。

PS:Project1和Project2都是Spring Boot Kotlin项目,它们可以单独正常工作。我想将Project1的某些功能重用到Project2中,但是我做不到。

此外,Project1和Project2在同一目录级别。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

根项目

第一步,您需要添加带有两个子项目的根项目。

settings.gradle.kts

rootProject.name = "demo-root"

include(
        "project1",
        "project2"
)

build.gradle.kts

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    base
    kotlin("jvm") version "1.3.71" apply true
    id("java-library")
    id("org.springframework.boot") version "2.2.6.RELEASE" apply false
    id("io.spring.dependency-management") version "1.0.9.RELEASE" apply false
    id("org.jetbrains.kotlin.plugin.jpa") version "1.3.71" apply false
    kotlin("plugin.spring") version "1.3.71" apply false
}

allprojects {
    group = "com.dempcompany"
    version = "0.0.1-SNAPSHOT"

    repositories {
        mavenCentral()
    }

//    apply {
//        plugin("kotlin")
//    }

    tasks.withType<JavaCompile> {
        sourceCompatibility = "13"
        targetCompatibility = "13"
    }

    tasks.withType<KotlinCompile> {
        kotlinOptions {
            jvmTarget = "12"
        }
    }
}

subprojects {
//    apply {
//        plugin("io.spring.dependency-management")
//    }
}

dependencies {
    subprojects.forEach {
        // Make the root project archives configuration depend on every subproject
        archives(it)
    }
}

Project1

然后配置project1的构建配置

project1/build.gradle.kts

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    base
    kotlin("jvm") version "1.3.71" apply true
    id("java-library")
    id("org.springframework.boot") version "2.2.6.RELEASE" apply false
    id("io.spring.dependency-management") version "1.0.9.RELEASE" apply false
    id("org.jetbrains.kotlin.plugin.jpa") version "1.3.71" apply false
    kotlin("plugin.spring") version "1.3.71" apply false
}

allprojects {
    group = "com.democompany"
    version = "0.0.1-SNAPSHOT"

    repositories {
        mavenCentral()
    }

//    apply {
//        plugin("kotlin")
//    }

    tasks.withType<JavaCompile> {
        sourceCompatibility = "13"
        targetCompatibility = "13"
    }

    tasks.withType<KotlinCompile> {
        kotlinOptions {
            jvmTarget = "12"
        }
    }
}

subprojects {
//    apply {
//        plugin("io.spring.dependency-management")
//    }
}

dependencies {
    subprojects.forEach {
        // Make the root project archives configuration depend on every subproject
        archives(it)
    }
}

project1/setting.gradle.kts

rootProject.name = "project1"

Project2

然后为project2配置构建配置

project2/build.gradle.kts

plugins {
    id("org.springframework.boot")
    id("io.spring.dependency-management")
    kotlin("jvm")
    kotlin("plugin.spring")
}

repositories {
    mavenCentral()
    maven("https://jitpack.io")
}

java.sourceCompatibility = JavaVersion.VERSION_13

dependencies {
    /* Project modules */
    implementation(project(":project1", "default"))

    /* Spring Framework */
    implementation("org.springframework.boot:spring-boot-starter")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.springframework.boot:spring-boot-starter-actuator")
    
    /* Kotlin */
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.3")
}

project2/settings.gradle.kts

rootProject.name = "project2"