简体   繁体   English

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

[英]Maven multi module project cannot find sibling module

I can't seem to get Maven to find a sibling's module in a multi-module project. 我似乎无法让Maven在多模块项目中找到兄弟模块。

I've run mvn clean install in all modules. 我在所有模块中都运行了mvn clean install

Here's the setup: 这是设置:

Product
+-- MagniCompCommon
+-- Model

The Model project has MagniCompCommon as a dependency. Model项目将MagniCompCommon作为依赖项。 When I run mvn clean compile in Model , I get: 当我在Model运行mvn clean compile时,我得到:

[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]

As you can see, Maven is trying to find MagniCompCommon in the java.net repo (this is the first repository entry in the parent ( Product ) pom.xml ). 如您所见,Maven正在尝试在java.net repo中找到MagniCompCommon (这是父( Productpom.xml的第一个存储库条目)。

Here is Product pom.xml : 这是Product 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>

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

Here is Model pom.xml 这是Model 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>

When you are building a multi-module Maven project, you need to run Maven commands from the root POM. 在构建多模块Maven项目时,需要从根POM运行Maven命令。 This means you need to run mvn clean install on Product 's pom.xml . 这意味着您需要在Productpom.xml上运行mvn clean install

The error you are getting is expected: you are only building Model . 您将获得错误:您只构建Model In Model 's POM, Maven sees that there is a dependency on MagniCompCommon so it tries to look for that dependency. Model的POM中,Maven发现MagniCompCommon存在依赖关系,因此它试图寻找这种依赖关系。 First, it searches in your local repo: it fails to find it there since you did not install MagniCompCommon before. 首先,它在您的本地MagniCompCommon进行搜索:由于您之前未安装MagniCompCommon ,因此无法在那里找到它。 As a result, it looks for it in the predefined remote repositories (and also fails to find it). 因此,它在预定义的远程存储库中查找它(并且也找不到它)。

You would be able to circumvent this by first running mvn clean install on MagniCompCommon 's POM, then on Model POM but this is much easier done by invoking Maven directly on the root POM. 你可以通过首先在MagniCompCommon的POM上运行mvn clean install然后在Model POM MagniCompCommon规避这个问题,但是通过直接在根POM上调用Maven可以更容易地做到这一点。 It will correctly build every modules in the right order (since Model depends on MagniCompCommon , it will build MagniCompCommon first, then Model ). 它将以正确的顺序正确构建每个模块(因为Model依赖于MagniCompCommon ,它将首先构建MagniCompCommon然后构建Model )。

As a side note, you can remove the line <packaging>jar</packaging> because this is the default. 作为旁注,您可以删除行<packaging>jar</packaging>因为这是默认值。

I noticed MagniCompCommon pom doesnt specify a version 我注意到MagniCompCommon pom没有指定版本

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

And in Product pom, you're referencing version 1.0 Product pom中,您引用的是1.0版本

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

Have you tried specifying version 1.0 in MagniCompCommon pom? 您是否尝试在MagniCompCommon pom中指定版本1.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM