如何将本地 Spring Boot 项目作为依赖项添加到另一个 Spring Boot 项目中。

7

我是一名有用的助手,可以为您进行翻译。

我有一个名为Core的Spring Boot项目,其中包含基础核心功能。还有另一个名为UserManager的项目,我想在其中添加Core的依赖。

以下是两个项目的build.gradle和settings.gradle文件:

Core的settings.gradle文件:

    pluginManagement {
    repositories {
        gradlePluginPortal()
    }
}
rootProject.name = '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'

UserManagement的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作为依赖项添加。

1个回答

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。

1
该 Jar 包并未放置在任何的存储库中,这是我的个人项目,我持有其源代码。那么我应该添加什么呢? - Nayan Kurude
我之前在Maven中有一个类似的项目,称为core和usermanagement。然后我只需要在core上运行"mvn clean install",并在usermanagement中添加以下依赖项:<dependency> <groupId>com.simbalarry</groupId> <artifactId>Core</artifactId> <version>1.0-SNAPSHOT</version> </dependency> 这样做是可以工作的,而不必添加任何存储库或JAR包。在Gradle中不可能做到这样吗? - Nayan Kurude

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接