Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add local spring boot project as a dependency in another spring boot project

I have spring boot project Core which has some basic core functionality. Another project UserManager in which I want to add dependency of core.

Below are the build.gradle and settings.gradle of both projects

settings.gradle of Core

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

build.gradle of Core

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

settings.gradle of UserManagement

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 of UserManagement

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

So what should I add in UserManager project to add Core as dependency.

like image 977
Nayan Kurude Avatar asked Mar 12 '19 12:03

Nayan Kurude


People also ask

How do I add dependency from one project to another?

Right-click the utility project, and select Maven>Add Dependency. Type a dependency name in the Enter groupID… field (e.g., commons-logging) to search for a dependency. Select the dependency, and click OK.

How do I use another project as a spring boot jar?

In order to share classes with another project, the best approach to take is to create a separate jar containing shared classes, then make it a dependency of all modules that rely on them. But if that isn't possible, we can configure the plugin to generate a separate jar that can be used as a dependency.


1 Answers

First add the jar in dependencies

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

Then add the repository in which jar is placed

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

Since Core as dependency and UserManager both are spring boot project, in UserManager add @ComponentScan with packages that need to be scanned in both the projects

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

If project not in repository Add that jar as an external jar to project

In Eclipse --> right click on project --> build path -->configure build path -->Add external jar

like image 167
Deadpool Avatar answered Sep 23 '22 10:09

Deadpool