Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "Cannot find symbol error" in related subproject while running gradle compileJava

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

  • MyProject
    • common
      • src/main/java/ch/wprogLK/myProject/backend/common/entities
        • User.java
      • build.gradle
      • settings.gradle
    • dataAccess
      • src/main/java/ch/wprogLK/myProject/backend/dataAccess
        • AcLuDataApplication.java
      • build.gradle
      • settings.gradle
    • build.gradle
    • settings.gradle

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
    }
}
like image 377
wprogLK Avatar asked Dec 17 '22 15:12

wprogLK


1 Answers

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
}
like image 158
Jonathan JOhx Avatar answered Jan 15 '23 02:01

Jonathan JOhx