20

I see a lot of questions on stackoverflow on this. But still I'm not able to know what is the issue in my way of building projects.

I've two spring boot projects: we-data and we-web. we-web is dependent on we-data. we-data compiles fine on maven. But we-web gives me above error.

we-data pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.we</groupId>
    <artifactId>data</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>we-data</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

we-web pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.we</groupId>
    <artifactId>web</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>we-web</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.we</groupId>
            <artifactId>data</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Everytime I do maven "clean compile package install" goals on we-web I get the maven error. All the symbols that this complains isn't found are all in we-data project. For some reason, it says all the classes of we-data cannot be found in we-web. But when I run the application as spring boot app, everything is fine. No maven error matters for this.:

[INFO] ------------------------------------------------------------------------
[INFO] Building we-web 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ web ---
[INFO] Deleting C:\Users\user\Documents\GitHub\we\we-web\target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ web ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ web ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 10 source files to C:\Users\user\Documents\GitHub\we\we-web\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /C:/Users/user/Documents/GitHub/we/we-web/src/main/java/com/we/controller/EmployeeController.java:[13,25] package com.we.service does not exist
[ERROR] /C:/Users/user/Documents/GitHub/we/we-web/src/main/java/com/we/controller/EmployeePaymentController.java:[22,9] cannot find symbol
  symbol:   class EmployeeService
  location: class com.we.controller.EmployeePaymentController
.
.
.
[INFO] 44 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.322 s
[INFO] Finished at: 2016-12-26T15:30:54+05:30
[INFO] Final Memory: 26M/253M
[INFO] ------------------------------------------------------------------------
[WARNING] The requested profile "pom.xml" could not be activated because it does not exist.
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project web: Compilation failure: Compilation failure:
.
.

I have 1.8 jdk being used by the STS. And also I have not put the we-data project on build path for we-web. I want this to pass, because I want to generate a war of this. Please help. I'm doing "clean compile package install" on we-data and then doing it on we-web. "Update maven project" is also not helping me.

User1230321
  • 1,435
  • 4
  • 23
  • 39
  • Looks like package mismatch - pls correct the import and resolve this `com.we.service does not exist`. Hope this should resolve the build – Clement Amarnath Dec 26 '16 at 10:42
  • @ClementAmarnath: No, the packages' import are all right. If that was the case I shouldn't have got the we-web running. But it runs fine. I get the error only when I do maven build. I have the same kind of package structure in both the projects. Can that cause any problem? Updated the question. – User1230321 Dec 26 '16 at 10:47
  • In that case, try this delete your .m2 for `we-data`, build `we-data` and then `we-web` – Clement Amarnath Dec 26 '16 at 10:54
  • 5
    Don't create 2 spring boot projects. You shouldn't be including a spring-boot project into another spring boot project. The jar isn't a normal jar so the resources cannot be found (although before 1.4 this worked you still shouldn't be doing it). Make we-data a regular jar and not a spring boot jar. If they both are standalone projects, then one shouldn't depend on the other in the first place. – M. Deinum Dec 27 '16 at 08:03

5 Answers5

38

I've been stuck on this problem too for a day and finally found the root cause and solution here what i've done:

if you have the following lines on your pom (for your case is the we-data's pom)

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>

you should remove those lines and change into:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
</plugin>

after got a clue from M. Deinum i found that he's correct about the jar compiled won't be a normal jar if you using spring-boot-maven-plugin, thanks for the clue anyway.

and also please check this question: Dependency not found in Spring Boot project

I've tried to use this:

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <executions>
            <execution>
                <goals>
                    <goal>repackage</goal>
                </goals>
                <configuration>
                    <classifier>exec</classifier>
                </configuration>
            </execution>
        </executions>
    </plugin>

it works like a charm.

Community
  • 1
  • 1
Sal Prima
  • 1,412
  • 2
  • 18
  • 23
  • Worked for me.. Spring boot jar dependency was not working by default. – inaitgaJ Nov 01 '17 at 06:00
  • Adding the configuration classifier `exec` fixed it for me as well. It outputs two separate jars on build: one normal one `app.jar` (same as you would get when building without this plugin) and a new one for the fat jar `app-exec.jar`. From your explanation that the fat jar is not recognized as a normal jar, I'm guessing that the original one needs to be present to fix it. Cheers! – Babyburger Mar 28 '21 at 12:05
  • I put the configuration below spring-boot-maven-plugin, and without tags. With your solution, it produces 3 jars, app-version.jar.original, app-version.jar, and app-version-exec.jar. Without tags, it produces two jars: app-version.jar and app-version-exec.jar. – Harun Sep 24 '21 at 08:24
  • What is the importance of putting `repackage` because it worked for me after putting this in execution tag. – Abhilash Ramteke Feb 24 '23 at 13:06
4

Everytime I do maven "clean compile package install" goals on we-web I get the maven error. All the symbols that this complains isn't found are all in we-data project.

1) I think you should read the introduction about Maven pom here and lifecycle in Maven.

mvn clean compile package install

is redundant for compile and package phases since the install phase includes these phases (. So, do this only :

mvn clean install

2) Your web module where you run the maven goal requires the com.we:data:0.0.1-SNAPSHOT dependency with your actual code :

    <dependency>
        <groupId>com.we</groupId>
        <artifactId>data</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>

If you add or modify code and you don't install this module, the web module cannot see these changes.

So you should do mvn clean install in the com.we:data module before doing that in the web module.

Finally the simplest to avoid having non updated dependency is running mvn clean install from a multi-module project which aggregates these modules.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • I've been building we-data first and then building we-web. But now, I'm doing "mvn clean install" instead of "mvn clean compile package install"(Thanks for that). That's not working. I would like to go with creating multi-module project for this as you suggested. But I don't know how to create multi-module "spring boot" project(also spring boot maven module(pardon if I'm sounding stupid)). Let me know if you can help me on this or I should open a new question on stackoverflow. – User1230321 Dec 27 '16 at 03:06
  • You are welcome. "That's not working." Having a multi module project will not resolve the missing class at compile time.It allows only to have a more robust build as all is ordered. Can you detail your error, edit your post and show all relevant information (package with classes missing for example)? – davidxxx Dec 27 '16 at 19:20
1

In my case in Spring Boot application I had the following service and accompanying non-public classes in a single ServiceFoo.java file:

@Service
public class ServiceFoo {
}
class Foo { }
class Bar { }

At some point Maven started to fail with cannot find symbol: class Service.

Only after moving classes Foo and Bar into separate java files (Foo.java and Bar.java respectively) the error disappeared. Looks like either Maven (3.5.2 and 3.6.3) or Java (1.8.0_201) bug.

mp31415
  • 6,531
  • 1
  • 44
  • 34
0

In pom.xml, Add depedency

<dependency>
    <groupId>javax.inject</groupId>
    <artifactId>javax.inject</artifactId>
    <version>1</version>
</dependency>

and class where you are using @Inject Import javax.inject.Inject import javax.inject.Inject;

Dinesh Sharma
  • 658
  • 8
  • 13
0

I got the same error "cannot find symbol" .

I got 2 maven modules front and back and the front module could not access to the back module despite the dependency.

The problem was on the pom.xml in the back module. I remove these lines :

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>

and I just got these lines in the front pom module.

Patrikoko
  • 478
  • 5
  • 5