Spring Boot multi module project with Gradle doesn't build

前端 未结 4 952
有刺的猬
有刺的猬 2021-01-31 03:29

I\'m working on a Spring Boot app with multiple modules and we\'re using Gradle to build it. Unfortunately I can\'t get the Gradle configuration right.

The project stru

相关标签:
4条回答
  • 2021-01-31 04:05

    The answer is similar to this one.

    Gradle documentation on multi-project builds states:

    A “lib” dependency is a special form of an execution dependency. It causes the other project to be built first and adds the jar with the classes of the other project to the classpath.

    A repackaged jar, thus, poses a problem.

    If, say, project B depends on project A, and the project A has a org.springframework.boot plugin applied to it, a simple compile project(':A') dependency will not work because the project jar is repackaged during the bootRepackage or bootJar task. The resulting fat jar has different structure, preventing Gradle from loading its classes.

    In this case, the dependencies should be written the following way:

    dependencies {
      compile project(':A').sourceSets.main.output
    }
    

    Using output of a source set directly is equivalent to using the "normal" resulting jar of project A before it is repackaged.

    0 讨论(0)
  • 2021-01-31 04:14
    bootJar {
        enabled = false
    }
    
    jar {
        enabled = true
    }
    

    This works for my. In my case use spring boot with spring cloud with multi project and gradle fail to build. With this solution works!

    0 讨论(0)
  • 2021-01-31 04:17

    You should exclude org.springframework.boot from all submodules (root build.gradle file, allProjects block), that are stands as dependencies for your core module, which will be build to fat-jar.

    Include dependencyManagement configuration into all of your spring-boot managed submodules:

    dependencyManagement {
        imports {
            mavenBom "org.springframework.boot:spring-boot-starter-parent:${springBootVersion}"
        }
    }
    

    The reason of your problem is the absence of submodules compiled jar files inside your core module fat-jar.

    0 讨论(0)
  • 2021-01-31 04:23

    In your utility (data) projects put:

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

    If kotlin dsl

    tasks.getByName<BootJar>("bootJar") {
        enabled = false
    }
    
    tasks.getByName<Jar>("jar") {
        enabled = true
    }
    
    0 讨论(0)
提交回复
热议问题