【问题标题】:How to add local spring boot project as a dependency in another spring boot project如何在另一个 Spring Boot 项目中添加本地 Spring Boot 项目作为依赖项
【发布时间】:2019-08-02 22:40:54
【问题描述】:

我有 Spring Boot 项目 Core,它具有一些基本的核心功能。 我想在其中添加核心依赖项的另一个项目 UserManager。

下面是两个项目的build.gradle和settings.gradle

核心的settings.gradle

    pluginManagement {
    repositories {
        gradlePluginPortal()
    }
}
rootProject.name = 'core'

Core 的 build.gradle

plugins {
    id 'org.springframework.boot' version '2.1.3.RELEASE'
    id 'org.jetbrains.kotlin.jvm' version '1.3.21'
    id 'org.jetbrains.kotlin.plugin.spring' version '1.3.21'
}

apply plugin: 'io.spring.dependency-management'

group = 'com.simbalarry'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

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'
}

compileKotlin {
    kotlinOptions {
        freeCompilerArgs = ['-Xjsr305=strict']
        jvmTarget = '1.8'
    }
}

compileTestKotlin {
    kotlinOptions {
        freeCompilerArgs = ['-Xjsr305=strict']
        jvmTarget = '1.8'
    }
}

UserManagement 的settings.gradle

pluginManagement {
    repositories {
        maven { url 'https://repo.spring.io/snapshot' }
        maven { url 'https://repo.spring.io/milestone' }
        gradlePluginPortal()
    }
    resolutionStrategy {
        eachPlugin {
            if (requested.id.id == 'org.springframework.boot') {
                useModule("org.springframework.boot:spring-boot-gradle-plugin:${requested.version}")
            }
        }
    }
}

rootProject.name = 'usermanager'

用户管理的build.gradle

    plugins {
    id 'org.jetbrains.kotlin.plugin.jpa' version '1.2.71'
    id 'org.springframework.boot' version '2.2.0.M1'
    id 'org.jetbrains.kotlin.jvm' version '1.2.71'
    id 'org.jetbrains.kotlin.plugin.spring' version '1.2.71'
}

apply plugin: 'io.spring.dependency-management'

group = 'com.simbalarry'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

test {
    useJUnitPlatform()
}

repositories {
    mavenCentral()
    maven { url 'https://repo.spring.io/snapshot' }
    maven { url 'https://repo.spring.io/milestone' }
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'
    implementation 'org.jetbrains.kotlin:kotlin-reflect'
    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
    implementation 'com.microsoft.sqlserver:mssql-jdbc'
    implementation 'org.modelmapper:modelmapper:2.3.0'
    compile project(':core')
    //compile 'com.simbalarry:core:0.0.1-SNAPSHOT'
    runtimeOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'com.h2database:h2'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.security:spring-security-test'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.2'
    testImplementation 'org.junit.jupiter:junit-jupiter-params:5.3.2'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.2'
}

compileKotlin {
    kotlinOptions {
        freeCompilerArgs = ['-Xjsr305=strict']
        jvmTarget = '1.8'
    }
}

compileTestKotlin {
    kotlinOptions {
        freeCompilerArgs = ['-Xjsr305=strict']
        jvmTarget = '1.8'
    }
}

那么我应该在 UserManager 项目中添加什么来添加 Core 作为依赖项。

【问题讨论】:

    标签: java spring-boot gradle build.gradle


    【解决方案1】:

    首先在依赖项中添加jar

    dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'
    implementation 'org.jetbrains.kotlin:kotlin-reflect'
    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
    implementation 'com.microsoft.sqlserver:mssql-jdbc'
    implementation 'org.modelmapper:modelmapper:2.3.0'
    compile project(':core')
    compile 'com.simbalarry:core:0.0.1-SNAPSHOT'
    runtimeOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'com.h2database:h2'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.security:spring-security-test'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.2'
    testImplementation 'org.junit.jupiter:junit-jupiter-params:5.3.2'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.2'
     }
    

    然后添加jar所在的仓库

    repositories {
    mavenCentral()
    maven { url 'https://repo.spring.io/snapshot' }
    maven { url 'https://repo.spring.io/milestone' }
      //company repository or custom repository
    }
    

    由于Core as dependencyUserManager 都是spring boot 项目,所以在UserManager 添加@ComponentScan 以及需要在两个项目中扫描的包

    @ComponentScan({"com.user.management", "com.core.dependency"})
    

    如果项目不在存储库中将该 jar 作为外部 jar 添加到项目中

    在Eclipse中-->右键项目-->构建路径-->配置构建路径-->添加外部jar

    【讨论】:

    • jar 没有放在任何仓库中。这是我的个人项目,我有源代码。那我应该添加什么。
    • 我在 maven 中有类似的项目说 core 和 usermanagement 然后我以前只是在 core 上运行 mvn clean install 并在 usermanagement com.simbalarry 中添加以下依赖项Core1.0-SNAPSHOT 而且它过去只是在不添加任何存储库或 jar 的情况下工作。在gradle中不能做这样的事情吗?
    猜你喜欢
    • 2020-09-03
    • 1970-01-01
    • 2021-07-23
    • 2017-02-26
    相关资源
    最近更新 更多