1

I have 2 maven projects: one is a rest service with a WAR packaging (rest), the other one is a module to access database with a jar packaging (service).

In the pom of the rest module I add the dependency with service as follows:

<dependency>
    <groupId>project</groupId>
    <artifactId>service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

However I'm getting this error:

Project 'rest' is missing required library: 'C:\Users\user.m2\repository\project\service\0.0.1-SNAPSHOT\service-0.0.1-SNAPSHOT.jar'

I'm still developping the service module, so it can't be in .m2 library. How can I connect this two modules. Is there any way that I can access to my service functions in the @RestController classes?

EDIT: My project directory structure is

Rest
  |--- pom.xml

Service
  |--- pom.xml

Both are independent projects.

Running mvn clean package install on service project works, but, is there any way to make maven read my snapshoot version?

Shafin Mahmud
  • 3,831
  • 1
  • 23
  • 35
CodeSniffer
  • 83
  • 1
  • 10

1 Answers1

2

To have your multi module projects works together you need to add them with parent project. Dont worry all your modular projects can be independent and build separately. Say for your case your parent project directory should be like

parent
  |-- pom.xml
  rest
     |--- pom.xml  
  service
     |--- pom.xml

And in your top level or parent POM you will tell maven about all your sub module projects like

    <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>1.0.0</modelVersion>
       <groupId>your.groudid</groupId>
       <artifactId>parent</artifactId>
       <version>1.0</version>
       <packaging>pom</packaging>

        <modules>
            <module>rest</module>
            <module>service</module>
        </modules>
    </project>

And in your service module pom.xml have this

<parent>
    <groupId>your.groudid</groupId>
    <artifactId>parent</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>

<artifactId>service</artifactId>
<name>service</name>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

It will tell maven that it has a parent module and its needed to be build as a jar with the concerning artifact-id

Now you can tell maven that your rest module has a dependecy of service module just as like other dependecies. Follow this

<parent>
    <groupId>your.groudid</groupId>
    <artifactId>parent</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>

<artifactId>rest</artifactId>
<name>rest</name>
<packaging>war</packaging>

<dependencies>
   ...
    <dependency>
        <groupId>your.groupid</groupId>
        <artifactId>service</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    ....
</dependencies>

Note: For better understanding you can read this short Maven Multi Module Project article. And also this github repository that has a concise example about this.

Shafin Mahmud
  • 3,831
  • 1
  • 23
  • 35