Maven - Add a maven module as dependency to other maven module

佐手、 提交于 2021-01-28 09:00:26

问题


I have 2 Spring boot maven projects(module A, Module B). And they both are added as modules to a parent project. Both Modules have some common dependencies and java classes(Domain objects), so I created third module Module C, and placed all the common java files and dependencies there.

I added the Module C to parent pom as one of the module. And added Module C as dependency to Module A and Module B. In Module A, B wherever the classes of Module C is referred there it was resolved and is pointing to Module C Classes (on ctrl+click).No error was shown in eclipse and maven dependencies are updated. But when I build the projects either from Parent pom or build Module A alone (after building the module c), I get cannot find symbol error in the places where module C classes were referenced.

Below are my pom.xml's

Parent pom.xml

<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>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example.services</groupId>
    <artifactId>cloud-services</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>ModuleC</module> <!-- Project where common dependencies and Common java classes are placed -->
        <module>ModuleA</module>
        <module>ModuleB</module>
    </modules>
</project>

Module C Pom.xml - Common Project

<?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.example.services</groupId>
    <artifactId>cloud-services</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>ModuleC</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>ModuleC</name>
    
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
    </dependencyManagement>
</project>

Module A Pom.xml - Dependent Project on Module C

<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>com.example.services</groupId>
        <artifactId>cloud-services</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>ModuleA</artifactId>
    <dependencies>
    <!-- Project where common dependencies and Common java classes are placed -->
        <dependency>
            <groupId>com.example.services</groupId>
            <artifactId>ModuleC</artifactId>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>

Module B Pom.xml

Same as ModuleA.

Whats wrong with my configuration. Any help is much appreciated. Thanks.


回答1:


If its a spring project the root folder of the jar would be BOOT-INF.

If you create a normal project jar it wouldn't be having BOOT-INF.

The way it works is if you have a Project A and it has dependency on x.jar

Say Project A needs com.x.y.ClassA.class from x.jar it will search in root folder of x.jar with this path x/y/ClassA.class.

In your case as it was a spring project the resolution didnt work and it was complaining about that.




回答2:


I guess that the spring-boot-starter-parent is the culprit.

By using it as a parent POM to your parent POM, you are building all your modules with it. AFAIK, jars build with spring-boot-starter-parent cannot be used as dependencies because the classes are moved to a different directory inside the jar.

As I am not Spring expert, I cannot really offer you a good solution for this.



来源:https://stackoverflow.com/questions/62891554/maven-add-a-maven-module-as-dependency-to-other-maven-module

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