maven多模块项目

分析

原文链接

dependencyManagement

在父模块中,Maven中的dependencyManagement的作用在于对所依赖的jar包的版本进行管理,配置完成之后,父模块以及父模块的子模块不会引入任何的构件依赖,需要在子模块中声明才能正式导入

子模块如果dependencies里面的dependency自己没有声明version元素,那么maven就会到dependencyManagement里面去找有没有对该artifactId和groupId进行过版本声明,如果有,就继承它,如果没有就会报错,告诉我们必须为dependency声明一个version

如果dependencies中的dependency声明了version,那么无论dependencyManagement中有无对该jar的version声明,都以dependency里的为准.

继承关系

子模块会继承父模块所有依赖项

如果A引用了B,B引用了C
那么在A中也可以引用C

新建普通多模块项目

步骤:
新建一个项目
删掉src目录
在项目下添加模块One和模块Two和模块Three
在Two模块新建类,Three模块新建类
在One模块的pom文件中配置Two的依赖,Two模块下添加Three的依赖
将模块Two和Three install安装到本地仓库
在One模块中引用(One能够引用Two和Three,父模块能够引用子模块引用的模块)

父模块pom:
声明自己的基本信息等

  <groupId>org.example</groupId>
  <artifactId>moudels</artifactId>
  <packaging>pom</packaging>
  <version>1.0-SNAPSHOT</version>

声明子模块名称

  <modules>
    <module>one</module>
    <module>two</module>
  </modules>

在properties中指定版本信息

 <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <junit.version>4.11</junit.version>
  </properties>

使用denpendcyManagement声明要用到的依赖

<dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

使用pluginManagement声明要用到的插件

<pluginManagement>
	<plugins>
	……
	</plugins>
</pluginManagement>

子模块pom
声明父模块信息

  <parent>
    <artifactId>moudels</artifactId>
    <groupId>org.example</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>

声明自己的信息
groupId和version会从父模块继承,不需要再单独声明

<artifactId>one</artifactId>

所用依赖声明
指定要用到的依赖的groupId和artifactId,version不用,声明后会从dependecyManagement中继承导入,这里是One模块的pom文件,会用到Two模块的内容,所以引用类Two的依赖

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.example</groupId>
      <artifactId>two</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>

把模块Two install一下,安装到本地仓库
大概就是这样,在one模块下就能使用Two模块的内容了

如果都添加了还是不能引用,看看maven模块是不是灰色的 解决方案
多模块java项目gitee地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值