Ali Kaan Bacı
Ali Kaan Bacı

Reputation: 151

Maven multi module project packaging error

I am trying to develop multimodule project with maven. But when I want to package my project it gives errors.

It says package does not exist in web class. I already tried the solution on ask & answer part. But did not work. Can you review my repo and tell me what I did wrong, please?

[INFO] 8 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] xguru .............................................. SUCCESS [  1.330 s]
[INFO] data ............................................... SUCCESS [  5.453 s]
[INFO] web ................................................ FAILURE [  0.746 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.064 s
[INFO] Finished at: 2018-12-04T12:15:14+03:00
[INFO] Final Memory: 48M/391M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:testCompile (default-testCompile) on project web: Compilation failure: Compilation failure:
[ERROR] /C:/Users/asus/IdeaProjects/xguru/web/src/test/java/com/alikaan/xguru/controller/OwnerControllerTest.java:[3,31] package com.alikaan.xguru.model does not exist
[ERROR] /C:/Users/asus/IdeaProjects/xguru/web/src/test/java/com/alikaan/xguru/controller/OwnerControllerTest.java:[4,33] package com.alikaan.xguru.service does not exist
[ERROR] /C:/Users/asus/IdeaProjects/xguru/web/src/test/java/com/alikaan/xguru/controller/OwnerControllerTest.java:[32,13] cannot find symbol
[ERROR] symbol:   class OwnerService
[ERROR] location: class com.alikaan.xguru.controller.OwnerControllerTest
[ERROR] /C:/Users/asus/IdeaProjects/xguru/web/src/test/java/com/alikaan/xguru/controller/OwnerControllerTest.java:[37,9] cannot find symbol
[ERROR] symbol:   class Owner
[ERROR] location: class com.alikaan.xguru.controller.OwnerControllerTest
[ERROR] /C:/Users/asus/IdeaProjects/xguru/web/src/test/java/com/alikaan/xguru/controller/OwnerControllerTest.java:[45,9] cannot find symbol
[ERROR] symbol:   class Owner
[ERROR] location: class com.alikaan.xguru.controller.OwnerControllerTest
[ERROR] /C:/Users/asus/IdeaProjects/xguru/web/src/test/java/com/alikaan/xguru/controller/OwnerControllerTest.java:[45,25] cannot find symbol
[ERROR] symbol:   class Owner
[ERROR] location: class com.alikaan.xguru.controller.OwnerControllerTest
[ERROR] /C:/Users/asus/IdeaProjects/xguru/web/src/test/java/com/alikaan/xguru/controller/OwnerControllerTest.java:[46,9] cannot find symbol
[ERROR] symbol:   class Owner
[ERROR] location: class com.alikaan.xguru.controller.OwnerControllerTest
[ERROR] /C:/Users/asus/IdeaProjects/xguru/web/src/test/java/com/alikaan/xguru/controller/OwnerControllerTest.java:[46,26] cannot find symbol
[ERROR] symbol:   class Owner
[ERROR] location: class com.alikaan.xguru.controller.OwnerControllerTest
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
[ERROR] 
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <goals> -rf :web

Process finished with exit code 1

And Here my pom.xml files:

PARENT:

<?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.alikaan</groupId>
    <artifactId>xguru</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <modules>
        <module>data</module>
        <module>web</module>
    </modules>
    <packaging>pom</packaging>


    <name>xguru</name>
    <description>Demo project for Spring Boot</description>

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


    <properties>
        <start-class>com.alikaan.xguru.XguruApplication</start-class>
        <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-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <configuration>
                    <goals>install</goals>
                    <autoVersionSubmodules>true</autoVersionSubmodules>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

WEB:

<?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">
    <parent>
        <artifactId>xguru</artifactId>
        <groupId>com.alikaan</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>web</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.alikaan</groupId>
            <artifactId>data</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.0.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.0.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <version>2.23.4</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <!--Required to get circleCI working-->
                    <useSystemClassLoader>false</useSystemClassLoader>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

DATA:

<?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">
    <parent>
        <artifactId>xguru</artifactId>
        <groupId>com.alikaan</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>data</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <version>2.23.4</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
    <build>
        <plugins>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <!--Note your version numbe might be different-->
                    <version>2.22.1</version>
                    <configuration>
                        <useSystemClassLoader>false</useSystemClassLoader>
                    </configuration>
                </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <skip>true</skip>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Upvotes: 4

Views: 6330

Answers (2)

David Lakatos
David Lakatos

Reputation: 318

There is a problem with the project structure. Since the parent artifact of your reactor parent is

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>

all artifacts, including the data module artifact will execute this plugin execution:

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <executions>
    <execution>
      <id>repackage</id>
      <goals>
        <goal>repackage</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <mainClass>${start-class}</mainClass>
  </configuration>
</plugin>

The data module should not be repackaged to a fat jar, since you don't want to execute it as a Spring Boot application. You want to use it as a dependency artifact.

Don't use the spring-boot-starter-parent as a reactor parent! Instead, import the Spring Boot BOM into your parent's dependencyManagement (more here):

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.1.0.RELEASE</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

Add this to the parent's pluginManagement:

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

Move the spring-boot-maven-plugin invocation from your parent to the web module so only web will be repackaged, data will not be.

Finally, invoke mvn clean install from the parent directory. install is required for the reactor to work, since the dependency artifact (data-0.0.1-SNAPSHOT.jar) must be copied to the local Maven repository (usually ~/.m2/repositories/...) before it can be used as a dependency by the web module.

Upvotes: 5

Ali Kaan Bacı
Ali Kaan Bacı

Reputation: 151

I figure it out by adding to data pom this

   <properties>
    <spring-boot.repackage.skip>true</spring-boot.repackage.skip>
   </properties>

Upvotes: 10

Related Questions