maven应用(二)

来源:互联网 发布:华为端口聚合配置 编辑:程序博客网 时间:2024/06/04 19:10

一、依赖

1、依赖的传递性

A 依赖 B,B 依赖 C,只有B依赖C的范围为compile,A才能依赖C

2、依赖的排除,使用exclusions排除依赖

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.4.RELEASE</version>
 <exclusions>
              <exclusion>
                  <groupId>commons-logging</groupId>
                  <artifactId>commons-logging</artifactId>
              </exclusion>
          </exclusions>
</dependency>

3、统一管理依赖jar的版本号

<properties>
      <org.springframework-version>3.2.4.RELEASE</org.springframework-version>
  </properties>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

4、依赖的原则(解决 jar 包冲突)

a、路径最短者优先,比如A依赖B、B依赖C、A也可传递依赖于C、B与C有相同的jar包,但A会优先依赖与B

b、路径相同时先声明者优先,比如A依赖B、A依赖C、此时B与C中jar包引用,要看B与C哪个工程先配置了该jar包

二、继承(由父工程统一管理子工程jar包的版本号)

1、先创建一个父工程

[1]groupid:例top.einino.maven
[2]artifactId:例ParentHello
[3]version:例0.0.1-SNAPSHOT

[4]Packaging:选择pom

2、子工程Hello引入父工程

<parent>
<groupId>top.einino.maven</groupId>
<artifactId>ParentHello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 指定从当前目录出发,向上寻找父工程的pom.xml位置 -->
<relativePath>../ParentHello/pom.xml</relativePath>
</parent>

<!-- 此时如果子工程和父工程的groupId和version是一样的话,即可删掉 -->
<artifactId>Hello</artifactId>

3、由父工程统一管理jar包版本号
  <dependencyManagement>
      <dependencies>
          <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>4.0</version>
              <scope>test</scope>
          </dependency>
      </dependencies>
  </dependencyManagement>

4、子工程只需要指定依赖

<!-- 依赖范围和版本号由父工程管理,子工程只需要指定依赖即可 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>

三、聚合

使用聚合可以批量进行 Maven 工程的安装、清理工作

在父工程配置如下:

<modules>
<module>../Hello</module>
</modules>

现在选择父工程的pom.xml文件–>点击右键–>Run As–>Maven install,即可同时把Hello工程和ParentHello同时安装到本地仓库中了!

四、小结

本博文介绍了maven的依赖、继承、聚合!都是比较常用的几个点!

如果有疑问或者对本博文有何看法或建议或有问题的,欢迎评论,恳请指正!

0 0
原创粉丝点击