本文介绍了Maven - 添加一个 Maven 模块作为对其他 Maven 模块的依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个 Spring Boot Maven 项目(模块 A,模块 B).它们都作为模块添加到父项目中.两个模块都有一些共同的依赖和java类(域对象),所以我创建了第三个模块Module C,并将所有常见的java文件和依赖放在那里.

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.

我将模块 C 作为模块之一添加到父 pom 中.并添加模块 C 作为对模块 A 和模块 B 的依赖.在模块 A、B 中,无论模块 C 的类在那里被引用,它都被解析并指向模块 C 类(按 ctrl+单击).eclipse 中没有显示错误并且 Maven 依赖项已更新.但是,当我从父 pom 构建项目或单独构建模块 A 时(在构建模块 c 之后),我在引用模块 C 类的地方出现 cannot find symbol 错误.

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.

下面是我的 pom.xml

Below are my pom.xml's

父 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>

模块 C 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.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>

模块 A Pom.xml - 模块 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>

模块 B Pom.xml

Same as ModuleA.

我的配置有什么问题.任何帮助深表感谢.谢谢.

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

推荐答案

如果是 spring 项目,jar 的根文件夹将是 BOOT-INF.

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

如果您创建一个普通的项目 jar,它将不会有 BOOT-INF.

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

它的工作方式是如果你有一个项目 A 并且它依赖于 x.jar

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

假设项目 A 需要 x.jar 中的 com.x.y.ClassA.class,它将在 x.jar 的根文件夹中使用此路径 x/y/ClassA.class 进行搜索.

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.

这篇关于Maven - 添加一个 Maven 模块作为对其他 Maven 模块的依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 05:35