'dependencies.dependency.version'缺少错误,但版本在父级中管理

fan*_*nts 54 java maven-3

我有一个包含几个模块的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>
Run Code Online (Sandbox Code Playgroud)

儿童pom:

  <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>
Run Code Online (Sandbox Code Playgroud)

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

[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 ...
Run Code Online (Sandbox Code Playgroud)

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

Bri*_*ain 43

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

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

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>3.2.3.RELEASE</version>
      <scope>runtime</scope>
    </dependency>
    
    Run Code Online (Sandbox Code Playgroud)
  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>
    
    Run Code Online (Sandbox Code Playgroud)

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

  • <relativePath>对我来说是关键:一个模块不在默认子位置,而是一个子目录更深.谢谢. (4认同)
  • 2.清除m2缓存对我有用! (3认同)
  • Maven抱怨缺少依赖项的另一个原因是,当父pom中声明的依赖项的&lt;type&gt;与子pom寻找的类型不对应时。 (2认同)

She*_*rms 35

如果有人在这里遇到了我遇到的同样的问题,我的问题是我错过了<dependencyManagement>从孩子pom复制的依赖关系周围的标签.

  • 直到现在我总是想知道dependencyManagement标签的用途是什么..这对我有用..我在父pom中缺少这个标签. (4认同)
  • 这感觉就像适用于大多数来这里的人的答案 (3认同)
  • 你让我今天一整天都感觉很好 (2认同)

fan*_*nts 7

是的,经过一番撕扯,我有了一个编译系统。

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

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

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

  <parent>
    <groupId>com.sw.system4</groupId>
    <artifactId>system4-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
Run Code Online (Sandbox Code Playgroud)

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

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

换句话说,如果您收到与子模块 A 相关的 Maven 错误,则实际上可能是不相关的子模块 Z 的问题,因此请查看那里。(并删除您的缓存)