maven多个子项目、父项目之间的引用问题

下面是一个简略的项目结构图
Parent

`------ childA(BusinessLayer)
          `--- pom.xml

`------ childB(WebLayer)
         `--- pom.xml

`------ pom.xml

1、parent模块指定子模块

在parent的pom文件里加入以下内容。

<modules>
   <module>childA</module>
   <module>childB</module>
</modules>

2、子模块找到父模块
在parent中,写入以下内容,其中"*"标识的行可以组成一个路径,通过这个路径可以在maven仓库中找到这个pom文件所以这三个标签是必须的!!!

<modelVersion>4.0.0</modelVersion>  
<groupId>com.sang.main</groupId>              *
<artifactId>Parent-Moduel</artifactId>         *
<version>1.0.2</version>            *
<packaging>pom</packaging>  
<name>Simple-main</name>

父pom写好了,子pom就通过标签继承父pom的依赖,如下:

<parent>
   <groupId>com.sang.main</groupId>
   <artifactId>Parent-Moduel</artifactId>
   <version>1.0.2</version>
   <relativePath>../pom.xml</relativePath>  <!--本例中此处是可选的-->
</parent>

值得注意的是标签,如果pom的层次关系就像本例中的那样只隔一层,则可以省略这个。maven同样可以找到子pom。

子pom中引入标签后,就会从父pom继承等属性了,

<modelVersion>4.0.0</modelVersion>  
<groupId>com.sang.business</groupId>     <!--和artifactId一起唯一标识这个jar文件-->
<artifactId>ChildA-module</artifactId>
<packaging>jar</packaging>         <!--指明打包类型-->
<name>childA</name>

3、依赖包管理

父pom中把依赖通过引起来,表示子pom可能会用到的jar包依赖

<dependencyManagement>
   <dependencies>
      <dependency>
           <groupId>javax.servlet</groupId>
          <artifactId>servlet-api</artifactId>
          <version>2.5</version>
      </dependency>
   </dependencies>
</dependencyManagement>

子pom如果需要引用该jar包,则直接引用即可!不需要加入,便于统一管理。此外也可以加入仅在子pom中用到的jar包,比如:

<dependencies>
   <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>   <!--此处不再需要verison了!-->
   </dependency>
   <dependency>
       <groupId>org.codehaus.jackson</groupId>
       <artifactId>jackson-core-lgpl</artifactId>
       <version>1.9.4</version>    <!--当然也可以加入只在这个子模块中用到的jar包-->
   </dependency>
</dependencies>

4、除了jar包依赖,插件也可以通过这样的方式进行管理

<!-- mainModule -->
<build>
   <pluginManagement>
      <plugins>
          <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-source-plugin</artifactId>
               <version>2.1.1</version>
          </plugin>
      </plugins>
   </pluginManagement>
</build>

<!-- childA -->
<build>   
   <plugins>
      <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-source-plugin</artifactId>
      </plugin>
   </plugins>
</build>

5、如果子pom间存在引用关系,比如childB 引用了childA

<dependency>
   <groupId>com.module</groupId>
   <artifactId>childA</artifactId>       <!--加上childA的依赖-->
   <version>1.0.0</version>
</dependency>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这个操蛋的人生!!!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值