Maven:POM模块和子模块层次结构 [英] Maven: POM modules and submodules hierarchy

查看:139
本文介绍了Maven:POM模块和子模块层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目的结构如下:

.
 |--module
 |  `-- pom.xml
 |  --submodule
 |    `-- pom.xml
 `-- pom.xml

POM(简体):

  • 项目:
<project>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>project</artifactId>
    <name>Project</name>
    <groupId>org.myorg</groupId>
    <version>1.0.6-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>module</module>     
    </modules>
    (...)
</project>

  • 模块:
  • <project>
        <modelVersion>4.0.0</modelVersion>
    
        <parent>
            <groupId>org.myorg</groupId>
            <artifactId>project</artifactId>
            <version>1.0.6-SNAPSHOT</version>
            <relativePath>../pom.xml</relativePath>
        </parent>
    
        <artifactId>module</artifactId>
        <name>Module</name>
        <groupId>org.myorg</groupId>
        <version>1.0.6-SNAPSHOT</version>
        <packaging>pom</packaging>
    
        <modules>
            <module>submodule</module>      
        </modules>
        (...)
    </project>
    

    • 子模块:
    • <project>
          <modelVersion>4.0.0</modelVersion>
      
          <parent>
              <groupId>org.myorg</groupId>
              <artifactId>module</artifactId>
              <version>1.0.6-SNAPSHOT</version>
              <relativePath>../pom.xml</relativePath>
          </parent>
      
          <artifactId>submodule</artifactId>
          <name>Submodule</name>
          <groupId>org.myorg</groupId>
          <version>1.0.6-SNAPSHOT</version>
          <packaging>jar</packaging>
          (...)
      </project>
      

      在POM的项目模块中运行maven install时,将成功构建项目.但是,当在子模块中运行时,会出现此错误:

      When run maven install in POM's project or module the project is built sucessfully. But, when run in submodule occours this error:

      无法在项目子模块上执行目标:找不到工件org.myorg:project:pom:1.0.6-SNAPSHOT

      为什么我的子模块找不到POM 项目?已指定相对路径.

      Why my submodule not find the POM project? The relative path is specified.

      推荐答案

      我注意到的第一件事是,每个具有父级的子模块都包含以下行:

      The first thing which i noticed is that every sub-module which has a parent contains the line:

      <relativePath>../pom.xml</relativePath>
      

      这是没有用的,因为它是maven中的默认值,或者将其删除.

      which is useless, cause it's default in maven or in other word just remove it.

      此外,在多模块构建中,您不应定义版本.如果groupId始终相同,则也可以省略groupId,因为当前模块从其父级继承了该版本.

      Furthermore in a multimodule build you shouldn't define the version. In case if the groupId is always the same you can omit the groupId as well, cause the current module inherits the version from it's parent.

      模块:pom.xml

      module: pom.xml

      <project>
          <parent>...
          </parent>    
          <artifactId>module</artifactId>
          <packaging>pom</packaging>
      
          <name>Module</name>
      
          <modules>
              <module>submodule</module>      
          </modules>
          (...)
      </project>
      

      除此之外,您无法进入子模块并调用

      Apart from that you can't go into a sub-module and call

      mvn install
      

      如果您要安装多模块构建中的单独模块,则应使用以下内容:

      If you like to install a separate module of a multi-module build you should use a thing like this:

      mvn -amd -pl submodule install
      

      它将完成您想做的事情,但是通常您应该安装完整的多模块构建,除非您完全知道自己在做什么. 选项 -amd -also-make-dependents 的缩写. -pl -projects 的缩写,用于定义在调用过程中应制作的项目列表.

      which will do what you like to do, but usually you should install a full mulit-module build unless you exactly know what you are doing. The options -amd is an abbrevation for --also-make-dependents. The -pl is an abbreviation for --projects to define a list of project which should be made during the call.

      这篇关于Maven:POM模块和子模块层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆