如何使一个模块依赖于另一个模块工件?

Dav*_*vid 40 dependencies module maven

我有maven多模块项目.

 A: parent.
    B: child1.
    C: child2.
Run Code Online (Sandbox Code Playgroud)

B将被打包以获取jar文件,然后c将使用此jar文件来编译代码.

在B中,如果我跑mvn package,它将创建b.jar(保持B/target/jars不在B/target另一个目的).

在C中,我需要使用它b.jar来编译代码.

现在,从A,当我跑:mvn package.首先,我成功b.jar为B 创建文件

但是当它进入C的编译阶段时,看起来C b.jar在类路径中无法识别(编译得到错误,因为C的代码无法从B导入类文件).

我的问题是:我该如何解决这个问题?

---------- Below是pom文件

A: pom.xml
  <groupId>AAA</groupId>
  <artifactId>A</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

   <modules>
   <module>C</module>
   <module>B</module>
   </modules>

B: pom.xml
        <groupId>AAA</groupId>
 <artifactId>B</artifactId>
 <packaging>jar</packaging>
 <version>0.0.1-SNAPSHOT</version>
 <parent>
  <artifactId>A</artifactId>
  <groupId>AAA</groupId>
  <version>0.0.1-SNAPSHOT</version>
 </parent>

C: pom.xml
       <parent>
  <artifactId>A</artifactId>
  <groupId>AAA</groupId>
  <version>0.0.1-SNAPSHOT</version>
 </parent>

 <groupId>AAA</groupId>
 <artifactId>C</artifactId>
 <packaging>war</packaging>
 <version>0.0.1-SNAPSHOT</version>

 <dependencies>

  <dependency>
   <groupId>AAA</groupId>
   <artifactId>B</artifactId>
   <version>0.0.1-SNAPSHOT</version>
  </dependency>
....
Run Code Online (Sandbox Code Playgroud)

Mat*_*nry 23

看起来它应该对我有用.但你可能会尝试mvn install而不是mvn package.


小智 17

试试$ {project.version}

例如

<dependency>
   <groupId>AAA</groupId>
   <artifactId>B</artifactId>
   <version>${project.version}</version>
  </dependency>
Run Code Online (Sandbox Code Playgroud)

  • 只是想注意“${project.version}”现在已被弃用。在新项目中使用:`${project.parent.version}` (6认同)
  • 这解决了我的问题。看了很多文章后,我认为这是正确的做法。唯一需要注意的是,您的模块版本需要与依赖模块的版本同步。 (2认同)

Pas*_*ent 11

我的问题是如何解决这个问题?

依赖性解析是通过本地存储库完成的,因此"解决"问题的规范方法是install从A 运行,以便模块安装在本地存储库中.

现在,关于以下评论

但如果我使用安装,那么也将安装c war文件.我当前的项目不接受那个".

当然,我不是你的项目,我不知道所有的约束和规则.但是如果你决定使用Maven,这是一个完全荒谬的政策(严重的是,WTF?)并且使用system范围的依赖肯定不是一个好的解决方案(以后保证更麻烦).如果这个政策是真的,那么最好不要在这种情况下使用Maven.