1

I am trying to build a multi module maven project. My Project Structure is

Multi-Module-Project
pom.xml
\Module-1
    src\main\java
        com.xyz.module1
           MyTestClass.java
    pom.xml

\Module-2
   src\main\java
        com.xyz.module2
          App.java
   pom.xml

Now when i am trying to use MyTestClass in App.java of Module-2, i am getting this error on running command "mvn clean install":

    ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project Module-2: Compilation failure: Compilation failure:
    [INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] Multi-Module-Project ............................... SUCCESS [  1.361 s]
[INFO] Module-1 ........................................... SUCCESS [  2.511 s]
[INFO] Module-2 ........................................... FAILURE [  0.135 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.721 s
[INFO] Finished at: 2018-01-31T12:11:29+05:30
[INFO] Final Memory: 36M/395M
[INFO] ------------------------------------------------------------------------

[ERROR] /home/xyz/workspace2/Multi-Module-Project/Module-2/src/main/java/com/xyz/module2/App.java:[3,26] package com.xyz.module1 does not exist
[ERROR] /home/xyz/workspace2/Multi-Module-Project/Module-2/src/main/java/com/xyz/module2/App.java:[13,17] cannot find symbol
[ERROR] symbol:   class MyTestClass
[ERROR] location: class com.xyz.module2.App

Here is my main pom.xml :

<?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.xyz</groupId>
<artifactId>Multi-Module-Project</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>

<name>Multi-Module-Project</name>
<description>Multi Module Project</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.10.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-tomcat</artifactId>
        <scope>provided</scope>
    </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>


<modules>
    <module>Module-1</module>
    <module>Module-2</module>
</modules>
</project>`

Module-1 Pom.xml

    <?xml version="1.0"?>
    <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.xyz</groupId>
    <artifactId>Multi-Module-Project</artifactId>
    <version>0.0.1</version>
  </parent>
  <packaging>war</packaging>
  <groupId>com.xyz</groupId>
  <artifactId>Module-1</artifactId>
  <version>0.0.1</version>
  <name>Module-1</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Module-2 Pom.xml

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.xyz</groupId>
    <artifactId>Multi-Module-Project</artifactId>
    <version>0.0.1</version>
  </parent>
  <packaging>war</packaging>
  <groupId>com.xyz</groupId>
  <artifactId>Module-2</artifactId>
  <version>0.0.1</version>
  <name>Module-2</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.xyz</groupId>
      <artifactId>Module-1</artifactId>
      <version>0.0.1</version>
    </dependency>
  </dependencies>
</project>

This is MyTestClass.java in module-1:

package com.xyz.module1;

public class MyTestClass {

    private String name;

    public MyTestClass() {
        super();
    }

    public MyTestClass(String name) {
        super();
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }



}

This is my App.java in module-2:

package com.xyz.module2;

import com.xyz.module1.MyTestClass;


public class App {
    public static void main(String[] args) {
        MyTestClass mtc = new MyTestClass();
        mtc.setName("Bob");
        System.out.println(mtc.getName());
    }
}
luboskrnac
  • 23,973
  • 10
  • 81
  • 92
Arpit Agrawal
  • 1,180
  • 1
  • 10
  • 21
  • Somewhere i read that maven looks for .jar file by default, so i tried with packaging as jar also, but it didnt work. I have also tried adding : "war" in dependency section of module-1 inside the module-2 pom.xml, but still not working. – Arpit Agrawal Jan 31 '18 at 07:11
  • Just clarifying your last comment ... did you remove `war` from `Module-1/pom.xml`? – glytching Jan 31 '18 at 08:05
  • @glytching yes, earlier i used jar in both module1 and module2 pom.xml and didnt use in dependency, but it didnt work. – Arpit Agrawal Jan 31 '18 at 08:55
  • A WAR can't be used as usual dependency cause it will not being part of the classpath. If you module-1 contains classes you need to use in other modules the packaging has to be jar instead of war... – khmarbaise Jan 31 '18 at 18:43
  • @khmarbaise if i want to deploy Module-1 also on tomcat, then how can i do it? how can i create a jar as well as war to solve both the use cases? – Arpit Agrawal Jan 31 '18 at 19:05
  • Ah Ok..now I get your point. In [maven-war-plugin you can configure](http://maven.apache.org/plugins/maven-war-plugin/war-mojo.html) to create separate jar which contains only the classes from `src/main/java` via `true` in the configuration. In consequence you have to change your dependency to use a classifier which is `classes`...But usually that smells like you should move this code into a separate module..(I would recommend to reconsider this also).. – khmarbaise Jan 31 '18 at 19:24
  • @khmarbaise sure will look into it. For now seems like this was the issue : https://github.com/spring-projects/spring-boot/issues/6792 – Arpit Agrawal Jan 31 '18 at 19:48

2 Answers2

4

For anyone else who might get stuck here, these are the links that solved my problem,

I did have to make Module-1 packaging to jar before my issue got solved.

Actual Answer which worked for me: https://github.com/spring-projects/spring-boot/issues/6792

Links that lead me to actual answer: spring-boot-maven-plugin breaks sibling module dependency

Maven compilation error: package does not exist

Arpit Agrawal
  • 1,180
  • 1
  • 10
  • 21
  • you saved my day! Thank you – xuanhung2401 Jan 28 '19 at 02:09
  • Was stuck with this for more than a day. Thank you!!! The link shows the configuration for the older versions of spring, Configuration for the latest spring version can be found at [https://www.baeldung.com/spring-boot-dependency](https://www.baeldung.com/spring-boot-dependency), hope it helps anyone still having the problem. – sandy.sk Jun 15 '21 at 21:27
2

If your Module 1 is suppose to be used as shared library, it wouldn't have WAR packaging. Just remove WAR packaging from Module 1 configuration.

WAR artifact can't use another WAR artifact.

luboskrnac
  • 23,973
  • 10
  • 81
  • 92
  • Hi, i tried with jar also, but it didnt work. Looks like https://github.com/spring-projects/spring-boot/issues/6792 this was the issue, after making module 1 as jar and with the solution provided in post, i was able to build the module successfully. – Arpit Agrawal Jan 31 '18 at 19:42