首页 > 解决方案 > 使用gradle的spring boot多项目生成相同的jar

问题描述

我是 gradle 新手,我在使用 spring boot 生成多个 jar 时遇到了麻烦。我生成了两个不同的构建,但是当我运行构建 A 或构建 B 时,两者都是 A

我的项目具有以下结构:

root
├── facade
     ├─── rest-api
     └─── web-api

├── dependencies
     ├─── services
     ├─── entities
     └─── ...

├── settings.gradle
└── build.gradle

我的意图是拥有一个微服务的单一回购。我将生成的微服务是外观目录的 web api 和 rest api 模块。顾名思义,这些模块具有称为依赖项的模块的依赖项。

正如我之前所描述的,当我运行 web api 模块时,就像我在运行 rest api 一样,甚至询问它的依赖关系。

我的settings.gradle

rootProject.name = "root"

include ":root:facade:rest-api"
include ":root:facade:web-api"



include ":root:dependencies:entities"
include ":root:dependencies:services"
...

还有我的build.gradle

buildscript {
    ext.kotlin_version = '1.3.61'
    ext.spring_boot_version = '2.2.2.RELEASE'
    ext.jjwt_version = '0.10.6'
    ext.klockVersion = "1.7.3"
    ext.queryDslVersion = '4.1.4'
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlin_version"
    }
}

group 'org.com'
version '0.1.0'


def javaProjects() {
    return subprojects.findAll { new File(it.projectDir, "src").exists() }
}

subprojects {
    repositories {
        jcenter()
        mavenCentral()
    }

    configure(javaProjects()) {
        apply plugin: "java"
        apply plugin: "java-library"
        apply plugin: "org.jetbrains.kotlin.jvm"
        apply plugin: 'kotlin'
        apply plugin: "kotlin-spring"
        apply plugin: "kotlin-jpa"
        apply plugin: 'org.springframework.boot'
        apply plugin: 'io.spring.dependency-management'
        compileKotlin {
            kotlinOptions.jvmTarget = "1.8"
        }
        compileTestKotlin {
            kotlinOptions.jvmTarget = "1.8"
        }


        dependencies {
            implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
            implementation "io.jsonwebtoken:jjwt-api:$jjwt_version"
            implementation "io.jsonwebtoken:jjwt-impl:$jjwt_version"
            implementation "io.jsonwebtoken:jjwt-jackson:$jjwt_version"
            implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

            implementation "com.soywiz.korlibs.klock:klock-jvm:1.7.3"
            implementation "org.jetbrains.kotlin:kotlin-reflect"

            implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
            implementation 'org.springframework.boot:spring-boot-starter-data-rest'
            implementation "org.springframework.boot:spring-boot-starter-web"
            implementation("org.springframework.boot:spring-boot-starter-security")

            implementation "org.postgresql:postgresql:42.1.3"

            implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'

            implementation "khttp:khttp:1.0.0"

            testImplementation('org.springframework.boot:spring-boot-starter-test') {
                exclude module: 'junit'
                exclude module: 'mockito-core'
            }

            testImplementation('org.junit.jupiter:junit-jupiter:5.5.2')
            testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
            testImplementation('com.ninja-squad:springmockk:2.0.0')
        }

        allOpen {
            annotation("javax.persistence.Entity")
            annotation("javax.persistence.MappedSuperclass")
            annotation("javax.persistence.Embeddable")
        }
    }
}

project(":root:dependencies:services") {
    bootJar {
        enabled = false
    }
    jar {
        enabled = true
    }
}
project(":root:dependencies:entities") {
    bootJar {
        enabled = false
    }
    jar {
        enabled = true
    }
}

...

我觉得我的build.gradle一定有问题,但我不明白是什么。

我也省略了模块 rest api 和 web api 的 build.gradle 文件,因为我只有依赖项并且我认为它不相关。

我之前曾使用过 maven 并遵循此架构。我不知道 gradle 是否是正确的做法,所以我愿意接受你能给我的任何建议。

感谢您的关注。

标签: springkotlingradle

解决方案


正如我在评论中提到的,包含子项目的正确方法是将子项目的路径替换为:ie 如果子项目位于(从根项目)sub/project1 中,那么包含它的正确方法是:

include ':sub:project1'

现在关于您在评论中的其他问题:

主类名未配置,无法解析

你可以简单地做:

mainClassName = 'full.cannonical.name.of.MainClass'

如果所有项目都有一个主类,那么你需要在每个项目的 build.gradle 中这样做


推荐阅读