4

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 ?

Mohamed Taboubi
  • 6,663
  • 11
  • 55
  • 87

2 Answers2

4

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.

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • Thanks, that leads me to find my issue, in fact I think if I ommit the plugin: 'org.springframework.boot' the compile('org.springframework.boot:spring-boot-starter-web') was not able to find a version to use ... – Mohamed Taboubi Apr 17 '19 at 17:19
  • so the issue is correcte by adding ther versions as compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.1.4.RELEASE' – Mohamed Taboubi Apr 17 '19 at 17:19
  • mavenBom SpringBootPlugin.BOM_COORDINATES is sufficient – matio Jan 18 '22 at 09:44
3

All you need to do is to disable the bootJar task and enable the jar task (which creates an ordinary jar rather than an executable jar). Add the following snippet in your build.gradle file:

bootJar {
    enabled = false
}
jar {
    enabled = true
}

Source: https://spring.io/guides/gs/multi-module/

dbaltor
  • 2,737
  • 3
  • 24
  • 36