Maven 多模块项目找不到同级模块

2022-09-02 21:12:08

我似乎无法让Maven在多模块项目中找到兄弟姐妹的模块。

我已经在所有模块中运行过。mvn clean install

设置如下:

Product
+-- MagniCompCommon
+-- Model

该项目具有依赖项。当我跑进去时,我得到:ModelMagniCompCommonmvn clean compileModel

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Model 1.0
[INFO] ------------------------------------------------------------------------
[WARNING] The POM for com.magnicomp:MagniCompCommon:jar:1.0 is missing, no dependency information available
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.585 s
[INFO] Finished at: 2015-10-14T10:09:04-07:00
[INFO] Final Memory: 5M/15M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project Model: Could not resolve dependencies for project com.magnicomp:Model:jar:1.0: Failure to find com.magnicomp:MagniCompCommon:jar:1.0 in http://download.java.net/maven/2/ was cached in the local repository, resolution will not be reattempted until the update interval of Java.Net has elapsed or updates are forced -> [Help 1]

如您所见,Maven 正试图在 java.net 存储库中查找(这是父存储库中的第一个条目 () )。MagniCompCommonProductpom.xml

这是产品: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>

  <groupId>com.magnicomp</groupId>
  <artifactId>Product</artifactId>
  <version>1.0</version>
  <packaging>pom</packaging>


  <modules>
    <module>MagniCompCommon</module>
    <module>Model</module>
    <module>Common</module>
    <module>Agent</module>
    <module>Doc</module>
  </modules>

... snip ...

    <repositories>
        <repository>
            <id>Java.Net</id>
            <url>http://download.java.net/maven/2/</url>
        </repository>

这是MagniCompCommonpom.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>

    <parent>
        <groupId>com.magnicomp</groupId>
        <artifactId>Product</artifactId>
        <version>1.0</version>
    </parent>

    <!-- <groupId>com.magnicomp.common</groupId> -->
    <artifactId>MagniCompCommon</artifactId>
    <packaging>jar</packaging>

这是模型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">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.magnicomp</groupId>
        <artifactId>Product</artifactId>
        <version>1.0</version>
    </parent>

    <artifactId>Model</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>com.magnicomp</groupId>
            <artifactId>MagniCompCommon</artifactId>
            <version>1.0</version>
        </dependency>

    </dependencies>

</project>

答案 1

生成多模块 Maven 项目时,需要从根 POM 运行 Maven 命令。这意味着您需要在 上运行 。mvn clean installProductpom.xml

您收到的错误是预期的:您只是在构建 。在 的 POM 中,Maven 看到存在依赖关系,因此它试图寻找该依赖关系。首先,它会在本地存储库中搜索:由于您之前未安装,因此无法在其中找到它。因此,它会在预定义的远程存储库中查找它(并且也无法找到它)。ModelModelMagniCompCommonMagniCompCommon

你可以通过首先在 POM 上运行,然后在 POM 上运行来规避这一点,但是通过直接在根 POM 上调用 Maven,要容易得多。它将以正确的顺序正确构建每个模块(因为取决于 ,它将首先构建,然后 )。mvn clean installMagniCompCommonModelModelMagniCompCommonMagniCompCommonModel

作为旁注,您可以删除该行,因为这是默认值。<packaging>jar</packaging>


答案 2

我注意到MagniCompCommon pom没有指定版本

<!-- <groupId>com.magnicomp.common</groupId> -->
<artifactId>MagniCompCommon</artifactId>
<packaging>jar</packaging>

Product pom 中,您引用的是 1.0 版

<dependency>
    <groupId>com.magnicomp</groupId>
    <artifactId>MagniCompCommon</artifactId>
    <version>1.0</version>
</dependency>

您是否尝试过在 MagniCompCommon pom 中指定 1.0 版?


推荐