82

我有一个包含多个模块的 Maven 项目。在 Eclipse(Juno,带有 m2e)中,它似乎编译得很好。但是当我在其中一个模块上进行 maven 安装时,构建会立即失败。

父pom:

  <groupId>com.sw.system4</groupId>
  <artifactId>system4-parent</artifactId>
  <version>${system4.version}</version>
  <packaging>pom</packaging>
  <name>System 4 Parent Project</name>
  <modules>
    <module>system4-data</module>
     ...others...
  </modules>
  <properties>
    <system4.version>0.0.1-SNAPSHOT</system4.version>
    <spring.version>3.2.3.RELEASE</spring.version>
    ... others...
  </properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
        <scope>runtime</scope>
      </dependency>
    ... lots of others ...
    </dependencies>
  </dependencyManagement>

儿童绒球:

  <parent>
    <groupId>com.sw.system4</groupId>
    <artifactId>system4-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>system4-data</artifactId>
  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <scope>runtime</scope>
    </dependency>
    ... lots of others...
  </dependencies>

当我构建时,我得到以下输出:

[ERROR] The build could not read 1 project -> [Help 1]
[ERROR]
[ERROR]   The project com.sw.system4:system4-data:0.0.1-SNAPSHOT (C:\work\eclips
e_workspaces\systemiv\system4-parent\system4-data\pom.xml) has 8 errors

[ERROR]     'dependencies.dependency.version' for org.springframework:spring-cor
e:jar is missing. @ line 16, column 16

... others omitted for clarity ...

我不明白为什么它甚至不尝试编译。我已经尝试从父级和子级中删除运行时范围,这没有任何区别。请帮忙!

4

11 回答 11

60

我认为您可以尝试几件事:

  1. 将版本的字面值放在pom中

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>3.2.3.RELEASE</version>
      <scope>runtime</scope>
    </dependency>
    
  2. 清除通常位于 C:\Users\user.m2\repository 的 .m2 缓存。当我在 Maven 中工作时,我会说我经常这样做。尤其是在提交之前,这样我可以更有信心 CI 会运行。您不必每次都对文件夹进行核对,有时只需您的项目包和 .cache 文件夹就足够了。

  3. 将 relativePath 标记添加到您的父 pom 声明

    <parent>
      <groupId>com.mycompany.app</groupId>
      <artifactId>my-app</artifactId>
      <version>1</version>
     <relativePath>../parent/pom.xml</relativePath>
    </parent>
    

看起来你的 pom 中有 8 个错误。在添加父 pom 和属性之前,我会尝试运行一些基本的编译。

于 2013-06-26T17:51:35.763 回答
59

如果有人在这里遇到与我相同的问题,我的问题是我缺少<dependencyManagement>从子 pom.xml 复制的依赖项周围的标签。

于 2016-08-18T19:43:30.590 回答
5

对,经过大量的头发撕裂后,我有了一个编译系统。

清理 .m2 缓存是一件有帮助的事情(感谢 Brian)

我犯的一个错误是将每个依赖项的 2 个版本放在父 pom 依赖项管理部分中 - 一个有<scope>runtime</scope>一个没有 - 这是为了尝试让 eclipse 快乐(即不显示流氓编译错误)以及能够在命令行上运行。这只是使事情复杂化,所以我删除了运行时的。

显式设置父级的版本似乎也有效(遗憾的是,maven 没有更广泛地支持使用这样的属性!)

  <parent>
    <groupId>com.sw.system4</groupId>
    <artifactId>system4-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>

然后我对所有依赖项的子模块中的错误“无法收集依赖项”感到奇怪,说它无法找到父模块 - 即使它的设置与其他编译的模块相同。

我终于通过从父 pom 编译而不是尝试单独编译每个模块来解决问题。这告诉我一个错误,在不同的模块中进行了相对简单的修复,奇怪的是,这使得它全部编译。

换句话说,如果你得到与子模块 A 相关的 maven 错误,实际上可能是不相关的子模块 Z 的问题,所以请看那里。(并删除您的缓存)

于 2013-06-27T09:49:00.023 回答
3

理论上,maven 不允许使用属性来设置父版本。

在您的情况下,maven 根本无法确定您的父 pom 的 0.0.1-SNAPSHOT 版本是当前在您的项目中的版本,因此它会尝试在您的本地存储库中找到它。它可能会找到一个,因为它是一个快照,但它是一个旧版本,可能不包含您的依赖管理部分。

不过有一个解决方法:

只需使用以下命令更改子 pom 中的父部分:

<parent>
    <groupId>com.sw.system4</groupId>
    <artifactId>system4-parent</artifactId>
    <version>${system4.version}</version>
    <relativePath>../pom.xml</relativePath>  <!-- this must match your parent pom location -->
</parent>
于 2013-06-26T17:16:01.913 回答
2

我有同样的错误,我忘了在<dependencyManagement>. 例如在父 pom 中:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.sw.system4</groupId>
            <artifactId>system4-data</artifactId><!-- child artifact id -->
            <version>${project.version}</version>
        <dependency>

        <!-- add all third party libraries ... -->

    </dependencies>
<dependencyManagement>
于 2019-06-11T13:22:21.597 回答
0

确保子项目/父/版本节点中的值与其父项目/版本值匹配

于 2017-01-17T15:16:17.877 回答
0

在做子模块之前,您必须先构建父模块。

于 2017-05-24T08:26:45.403 回答
0

刚刚起作用的是删除 .m2 文件夹中的 settings.xml:该文件告诉项目寻找不存在的 spring mvc 和 web 版本。

于 2017-07-19T14:28:00.117 回答
0

我遇到了同样的问题,我重命名了“.m2”上的“repository”文件夹(类似于repositoryBkp,名称并不重要,以防万一出现问题)并创建一个新的“repository”文件夹,然后我重新运行maven和所有项目编译成功

于 2019-11-05T18:29:53.637 回答
0

就我而言,我在同一个 pom.xml 中列出了两次相同的依赖项。确保它只使用一次。

于 2021-11-01T22:49:49.907 回答
0

对我来说,与我的dependencyManagement中的分类器相关的问题我有项目的依赖项(和“版本”关闭诅咒)我从dependencyManagement中删除,这解决了问题

于 2022-01-26T10:01:04.177 回答