Spring-boot: Compile error on Spring Boot Gradle Plugin 1.4.0.RELEASE multi project

Created on 5 Aug 2016  路  4Comments  路  Source: spring-projects/spring-boot

We have a gradle multi project (core, webapp), when i build from root project i get a compile error on a subproject about the dependencies that is located in another subproject, it麓s strange that WEBAPP builds if i run gradlew from it麓s directory, is was only able to reproduce from building the ROOT project.

Here is a link to a repository with sample code to reproduce the error.
Sample spring-boot-grade-multiproject

gradlew clean build
:clean
:core:clean
:webapp:clean
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar
:findMainClass
:startScripts
:distTar
:distZip
:bootRepackage
:assemble
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build
:core:compileJava
:core:processResources UP-TO-DATE
:core:classes
:core:jar
:core:findMainClass
:core:startScripts
:core:distTar
:core:distZip
:core:bootRepackage
:core:assemble
:core:compileTestJava UP-TO-DATE
:core:processTestResources UP-TO-DATE
:core:testClasses UP-TO-DATE
:core:test UP-TO-DATE
:core:check UP-TO-DATE
:core:build
:webapp:compileJava
C:\Users\rmartins\workspace\test\webapp\src\main\java\project\TestWeb.java:3: error: cannot find symbol
import project.TestCore;
              ^
  symbol:   class TestCore
  location: package project
C:\Users\rmartins\workspace\test\webapp\src\main\java\project\TestWeb.java:7: error: cannot find symbol
        public TestCore core = new TestCore();
               ^
  symbol:   class TestCore
  location: class TestWeb
C:\Users\rmartins\workspace\test\webapp\src\main\java\project\TestWeb.java:7: error: cannot find symbol
        public TestCore core = new TestCore();
                                   ^
  symbol:   class TestCore
  location: class TestWeb
3 errors
:webapp:compileJava FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':webapp:compileJava'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 15.616 secs

Please let me know if i need to share more information.

Thank you

invalid

Most helpful comment

As of Boot 1.4, executable jar files store compiled classes in BOOT-INF/classes. This means that they aren't accessible when simply adding the jar to the classpath. You are packaging your core module as an executable jar file and also trying to use it as a dependency. When you use it as a dependency its jar is added to the classpath. If the jar has been repackaged into an executable archive its classes are not visible and the compilation failure occurs.

The simplest change is to disable repackaging of your core project's jar:

project(':core') {
    bootRepackage {
        enabled = false
    }
}

All 4 comments

As of Boot 1.4, executable jar files store compiled classes in BOOT-INF/classes. This means that they aren't accessible when simply adding the jar to the classpath. You are packaging your core module as an executable jar file and also trying to use it as a dependency. When you use it as a dependency its jar is added to the classpath. If the jar has been repackaged into an executable archive its classes are not visible and the compilation failure occurs.

The simplest change is to disable repackaging of your core project's jar:

project(':core') {
    bootRepackage {
        enabled = false
    }
}

@wilkinsona, we have same problem in our project and we need multiply module repackage to executable jar, so disable bootRepackage not suitable. Now I fix it explicitly set dependency from 'compileJava':

core/build.gradle
bootRepackage {
    dependsOn ':webapp:compileJava'
}

But it not good solution for some reasons. Maybe exist better solution in my case?

I think you need to use normal java modules or modules with bootRepackage disabled for any module that does not need to produce an executable jar. If some module is being used both as an executable jar and as a dependency to other modules you probably will need to split it on separate dependency modules plus the executable one. This also helps to create lighter executable jars since depending on executable modules you are probably bringing along too many dependencies.

See http://docs.spring.io/spring-boot/docs/current/reference/html/howto-build.html#howto-create-an-additional-executable-jar

If you use classifier flag you will get two jars, one as project dependencie and second as executable jar with "-exec" prefix.

Was this page helpful?
0 / 5 - 0 ratings