I try to create a spring boot project with gradle using subprojects for different layers (web, business, dataAccess, common).
I followed the instruction of the official gradle guide Creating Multi-Projects builds.
Both subprojects are create originally with Spring.io.
Problem
I try to use my User
class of the subproject common in my other subproject dataAccess. The import works fine in Intellij. However, when I run gradlew :dataAccess:compileJava
the task fails because User
cannot be found:
Task :dataAccess:compileJava FAILED ...\dataAccess\src\main\java\ch\wprogLK\MyProject\backend\dataAccess\MyProjectDataApplication.java:23: error: cannot find symbol User u = new User(); ^ symbol: class User location: class MyProjectDataApplication)
The common project gets compiled before the dataAccess project. I guess it's a gradle configuration problem that the dataAccess project does not know anything about the common project at compilation time (maybe a classpath issue?). I'm glad for any help. Thanks.
Project setup
Code
MyProject/build.gradle
plugins {
id 'org.springframework.boot' version '2.1.8.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
group = 'ch.wprogLK'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
allprojects {
apply plugin: 'java'
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
}
MyProject/settings.gradle
rootProject.name = 'MyProject'
include 'common'
include 'dataAccess'
MyProject/common/build.gradle
plugins {
id 'org.springframework.boot'
id 'io.spring.dependency-management'
id 'java'
}
group = 'ch.wprogLK'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
}
MyProject/common/settings.gradle
rootProject.name = 'common'
MyProject/common/src/main/java/ch/wprogLK/myProject/backend/common/entities/User.java
package ch.wprogLK.myProject.backend.common.entities;
public class User
{
private Long userId;
private String username;
}
MyProject/dataAccess/build.gradle
plugins {
id 'org.springframework.boot'
id 'io.spring.dependency-management'
id 'java'
}
group = 'ch.wprogLK'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
compile project(":common")
}
MyProject/dataAccess/settings.gradle
rootProject.name = 'dataAccess'
MyProject/dataAccess/src/main/java/ch/wprogLK/myProject/backend/dataAccess/MyProjectDataApplication.java
package ch.wprogLK.myProject.backend.dataAccess;
import ch.wprogLK.myProject.backend.common.entities.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyProjectDataApplication
{
public static void main(String[] args)
{
SpringApplication.run(MyProjectDataApplication.class, args);
}
public MyProjectDataApplication()
{
User u = new User(); //This line is the problem when running gradle :dataAccess:javaCompile
}
}
It is missing to set two configurations in order to indicate that common
is a library. So add the following configuration and compile
, run
from root of project.
common build.gradle
bootJar {
enabled = false
}
jar {
enabled = true
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With