3

I have a multi module project with 1 main module (engine) and 2 sub modules (client and server)

engine (Main module) - Client - Server

When I try to use the server module in the client, It throws a compilation error that indicates that the server package was not found.

Please find all the 3 POM's below.

Engine POM

<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>dev.kasse</groupId>
<artifactId>engine</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
    <module>server</module>
    <module>client</module>
</modules>

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

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

Client pom

<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>
    <parent>
    <groupId>dev.kasse</groupId>
    <artifactId>engine</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>client</artifactId>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    **<dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>server</artifactId>
        <scope>test</scope>
        <version>${project.version}</version>
    </dependency>**
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
    </dependency>
    <dependency>
        <groupId>commons-configuration</groupId>
        <artifactId>commons-configuration</artifactId>
        <version>1.10</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <forkCount>0</forkCount>
                <reuseForks>true</reuseForks>               
            </configuration>
        </plugin>
    </plugins>
</build>

Server POM

    <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>
    <parent>
        <groupId>dev.kasse</groupId>
        <artifactId>engine</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>server</artifactId>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-library</artifactId>
        </dependency>
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>
  </project>
Ian Turton
  • 10,018
  • 1
  • 28
  • 47
Dev
  • 345
  • 7
  • 21
  • What Maven commands did you launch and on which project? – Tunaki Sep 28 '16 at 10:23
  • i tried launching with "mvn clean install". I get a compilation error in my eclipse as well. – Dev Sep 28 '16 at 15:19
  • You need to launch that command from the parent POM, see here http://stackoverflow.com/a/33131999/1743880 – Tunaki Sep 28 '16 at 15:21
  • Yes i launched the command from parent POM but still i get the compilation error. I could see the jar file (server) getting added to the client's maven dependencies but still it does shows a compilation error :( – Dev Sep 28 '16 at 15:35
  • you launch "mvn clean test" from the parent pom directory, right? Please attach the error message – Sammyrulez Sep 29 '16 at 13:12
  • Why did you include the dependency in double stars? Is this a failed attempt to highlight it? Or is it really in the code? – J Fabian Meier Oct 11 '16 at 11:13
  • Actually i want to higlight the dependency..its not included in my actual pom – Dev Oct 11 '16 at 12:02
  • @Sammyrulez I am using eclipse and i couldn't see server package being resolved in my client. Eclipse also throws a compilation error... – Dev Oct 11 '16 at 12:03

4 Answers4

2

Your modules need

<packaging>jar</packaging>

to overwrite the

<packaging>pom</packaging>

from the parent file.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • Thanks for your reply...but its still not resolved :( – Dev Oct 11 '16 at 12:04
  • Could you please copy and paste the exact error message? – J Fabian Meier Oct 11 '16 at 12:05
  • One additional note...In my client's maven dependency i could see the server jar file with packages from springframework and my server classes are included in BOOT_INF folder. Please let me know whether this is normal...I was expecting server packages as well under thee jar – Dev Oct 11 '16 at 12:08
  • The import dev.kasse.engine.server cannot be resolved – Dev Oct 11 '16 at 12:08
  • import dev.kasse.engine.server.Application; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = {Application.class}, webEnvironment=WebEnvironment.DEFINED_PORT) public class CustomerRestClientTest { – Dev Oct 11 '16 at 12:08
  • The above one is a client's test case where Application.class is the server class file – Dev Oct 11 '16 at 12:09
  • Is dev.kasse.engine.server a class? (Shouldn't class names be upper case)? – J Fabian Meier Oct 11 '16 at 12:10
  • Maven Error: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile (default-testCompile) on project client: Compilation failure: Compilation failure: [ERROR] /D:/Restaurant/workspace/engine/client/src/test/java/dev/kasse/client/test/rest/TicketRestClientTest.java:[20,31] package dev.kasse.engine.server does not exist – Dev Oct 11 '16 at 12:11
  • dev.kasse.engine.server is package and Application is the class name – Dev Oct 11 '16 at 12:11
  • My complete project is available in https://github.com/deivarayanazhagappan/kasse-engine May be this could provide you a deeper insight of the problem – Dev Oct 11 '16 at 12:14
2

I have a similar project set-up, but your solution worked just fine for me.

Still one way that could maybe have solved it in your case, would be to define the version in the parent pom using the dependencyManagment

<dependencyManagement>
   <dependencies>
      <dependency>
            <groupId>${groupId}</groupId>
            <artifactId>server</artifactId>
            <version>${project.version}</version>
        </dependency>
   </dependencies>
</dependencyManagement>
Community
  • 1
  • 1
David Lilljegren
  • 1,799
  • 16
  • 19
1

The dependency to the server has "test" scope: this means that is will be not included in the runtime package

<dependency>
    <groupId>${project.groupId}</groupId>
    <artifactId>server</artifactId>
    <scope>test</scope> <!-- remove this tag -->
    <version>${project.version}</version>
</dependency>

for more information on dependency scopes see: here

Sammyrulez
  • 1,046
  • 8
  • 15
  • Thank you for your reply..I am using the server dependency in my client for only JUnit test cases. I dont need it for my client application. But even removing it does not solve the problem :( – Dev Sep 28 '16 at 15:18
1

Ran into a similar problem. This seems to be caused by spring boots' packaging of jar. The Spring boot create an executable jar, sending the classes into BOOT-INF folder inside the jar.

Adding the following into the build\plugins section of the pom resolves the issue (client pom in the case here)

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                    <configuration>
                        <forceCreation>true</forceCreation>
                    </configuration>
                </execution>
            </executions>
        </plugin>

An alternative solution is here : Maven Multi-module dependency package not found

jsdeveloper
  • 46
  • 1
  • 4