Maven多模块项目找不到兄弟模块

时间:2015-10-14 17:28:08

标签: java maven multi-module

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

我在所有模块中都运行mvn clean install

以下是设置:

Product
+-- MagniCompCommon
+-- Model

Model项目有MagniCompCommon作为依赖项。当我在mvn clean compile中运行Model时,我得到:

[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 repo中找到MagniCompCommon(这是父(Productpom.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>

这是MagniCompCommon pom.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>

2 个答案:

答案 0 :(得分:18)

在构建多模块Maven项目时,需要从根POM运行Maven命令。这意味着您需要在mvn clean install的{​​{1}}上运行Product

您预计会遇到错误:您只构建pom.xml。在Model的POM中,Maven发现存在对Model的依赖,因此它会尝试查找该依赖关系。首先,它在您的本地仓库中进行搜索:由于您之前未安装MagniCompCommon,因此无法在那里找到它。因此,它在预定义的远程存储库中查找它(并且也找不到它)。

你可以通过首先在MagniCompCommon的POM上运行mvn clean install,然后在MagniCompCommon POM上运行来避免这种情况,但通过直接在根POM上调用Maven可以轻松完成。它将以正确的顺序正确构建每个模块(因为Model取决于Model,它将首先构建MagniCompCommon,然后然后 MagniCompCommon)。

作为旁注,您可以删除行Model,因为这是默认行。

答案 1 :(得分:0)

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

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

产品 pom中,您引用的是版本 1.0

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

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