How to configure Maven multi module dependency on sub project with different packaging

雨燕双飞 提交于 2020-01-24 20:02:06

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/48860053/how-to-configure-maven-multi-module-dependency-on-sub-project-with-different-pac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!