spring-bootgradlespring-boot-test

Create a library for Springboot using gradle without doing bootJar


I developed a controllerAdvice that I want to put it in a librairie that will be used by several microservices; Since my library is using spring annotations I m adding plugins and dependencies for springboot. However when I add

apply plugin: 'org.springframework.boot'

this adds me a bootJar step in the build step. The bootJar asking that I should have a main class for the spring boot the thing that I dont want too, since my jar is only a library and not a springboot application.

here is a part of my gradle.build script :

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'


group = 'com.test'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'

repositories {
    mavenCentral()
    maven {
        url = artifactoryRepoUrl
    }
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile("org.springframework.boot:spring-boot-configuration-processor")
    compile("org.springframework.boot:spring-boot-starter-hateoas")

    testCompile('org.springframework.boot:spring-boot-starter-test')
    compileOnly('org.projectlombok:lombok:1.18.6')
    annotationProcessor('org.projectlombok:lombok:1.18.6')
}

how can I resolve this issue ?


Solution

  • It sounds like you'd benefit from using Spring Boot's dependency management in isolation. This will take care of managing your dependencies versions, while leaving your project building a normal jar rather than a Spring Boot fat jar. The Gradle plugin's reference documentation describes how to do this. This first step is to depend on the plugin without applying it. If you're using the plugins block, that would look like this:

    plugins {
        id 'org.springframework.boot' version '2.1.4.RELEASE' apply false
    }
    

    In your case it looks as it removing apply plugin: 'org.springframework.boot' would be sufficient.

    You're already applying the dependency management plugin so the remaining step is to configure Boot's dependency management:

    dependencyManagement {
        imports {
            mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
        }
    }
    

    This will allow you to continue to declare dependencies with no version. You can then run the jar task to build a normal jar that's suitable for use as a dependency.