`

Maven3构建

    博客分类:
  • JAVA
 
阅读更多

一 : pom文件 由下面四个部分组成

1 项目基本信息:这个配置会指导maven构建出相应的组件,并把他存在maven资源库中

 

2 构建配置:<build>中包含执行maven构建周期目标所需要的插件及相应配置,要配置构建插件,需要把它放在pom.xml文件的<build><plugins>中。

 

3 依赖管理:<dependencies> maven可以帮你管理这些依赖项,这些第三方类库都有它们自己的pom.xml文件,会声明各自的依赖项,maven可以据此找出任何需要下载的其他类库。这些依赖项一开始主要分为两个作用域(compile和test)设置作用域跟把jar文件放在classpath下是一样的效果。

设置<scope>设置为compile会把这些jar加到classpath中用于代码的编译,设置为test会在maven编译和运行测试时把这些jar加到classpath,可以通过搜索https://search.maven.org/

 

4 环境配置:<profiles>可以有效处理不同环境下的构建,可以执行maven时用-P <id> 可以指定要启用的环境配置。

可以使用mvn help:active-profiles。查看当前激活的profile都有哪些

 

二 maven构建周期目标

1:mvn clean 会删除target目录

2:mvn compile 编译后的类最终会出现在target/classes目录下,包括java源文件和resources目录资源文件

3:mvn test 在这一阶段,test目录下的测试类还没有编译,可以通过mvn test来进行编译

4:mvn install  这个阶段是打包阶段,把打包好的模块安装到本地maven资源库中,以便其他项目可以把它当做依赖项。

 

三: Maven依赖介绍

 

1、依赖传递在选择版本的时候首先是根据深度选择的

当一个项目同时经过不同的路径依赖于同一个组件时,会选择其深度最短的对应组件进行依赖。举例来说,假设A->B->C->D1.0,A->E->D2.0,那么这个时候A就会选择对D相对路径短的组件来进行依赖,也就是D2.0。那么当深度一样的时候Maven会如何选择呢?即A->B->D1.0和A->C->D2.0,这个时候Maven会如何选择A所依赖的D的版本呢?这种情况Maven会根据申明的依赖顺序来进行选择,先申明的会被作为依赖包。向前面这种情况,如果先申明对B的依赖,则A依赖的就是D1.0,如果先申明对C的依赖,则A依赖的就是D2.0。

 

2、使用exclusion排除依赖

A不需要B中C的依赖时,就可以通过下面的方式

  1.  <dependency>  
  2.               <groupId>groupB</groupId>  
  3.               <artifactId>artifactB</artifactId>  
  4.               <version>1.0</version>  
  5.               <exclusions>  
  6.                      <exclusion>  
  7.                             <groupId>groupC</groupId>  
  8.                             <artifactId>artifactC</artifactId>  
  9.                      </exclusion>  
  10.               </exclusions>  
  11.        </dependency>  

 

3、可选的依赖项

可选的依赖项表示可有可无,不一定需要的,它只是做一个标记<optional>,这个标记对本身没有什么影响,对于其他的项目相当与exclusion

  1. <project>  
  2.        <groupId>groupB</groupId>  
  3.        <artifactId>artifactB</artifactId>  
  4.        <version>1.0</version>  
  5.        <dependencies>  
  6.               <dependency>  
  7.                      <groupId>groupC</groupId>  
  8.                      <artifactId>artifactC</artifactId>  
  9.                      <version>1.0</version>  
  10.                      <optional>true</optional>  
  11.               </dependency>  
  12.               ...  
  13.        </dependencies>  
  14. </project>

 

四: 依赖项的作用域

我们可以通过scope来指定该依赖项的作用范围。scope的取值有compile、runtime、test、provided、system和import。

compile:这是依赖项的默认作用范围,即当没有指定依赖项的scope时默认使用compile。compile范围内的依赖项在所有情况下都是有效的,包括运行、测试和编译时。

runtime:表示该依赖项只有在运行时才是需要的,在编译的时候不需要。这种类型的依赖项将在运行和test的类路径下可以访问。

test:表示该依赖项只对测试时有用,包括测试代码的编译和运行,对于正常的项目运行是没有影响的。

provided:表示该依赖项将由JDK或者运行容器在运行时提供,也就是说由Maven提供的该依赖项我们只有在编译和测试时才会用到,而在运行时将由JDK或者运行容器提供。

system:当scope为system时,表示该依赖项是我们自己提供的,不需要Maven到仓库里面去找。指定scope为system需要与另一个属性元素systemPath一起使用,它表示该依赖项在当前系统的位置,使用的是绝对路径。

 

五、集中管理依赖项:集中管理项目的依赖项,控制版本

projectA项目

  1. <project>  
  2.        <groupId>groupA</groupId>  
  3.        <artifactId>artifactA</artifactId>  
  4.        <version>1.0</version>  
  5.        <dependencies>  
  6.               <dependency>  
  7.                      <groupId>groupC</groupId>  
  8.                      <artifactId>artifactC</artifactId>  
  9.                      <version>1.0</version>  
  10.               </dependency>  
  11.               <dependency>  
  12.                      <groupId>groupD</groupId>  
  13.                      <artifactId>artifactD</artifactId>  
  14.                      <version>1.0</version>  
  15.               </dependency>  
  16.        </dependencies>  
  17.        ...  
  18. </project>

 

 

projectB项目

  1. <project>  
  2.        <groupId>groupB</groupId>  
  3.        <artifactId>artifactB</artifactId>  
  4.        <version>1.0</version>  
  5.        <dependencies>  
  6.               <dependency>  
  7.                      <groupId>groupC</groupId>  
  8.                      <artifactId>artifactC</artifactId>  
  9.                      <version>1.0</version>  
  10.               </dependency>  
  11.               <dependency>  
  12.                      <groupId>groupE</groupId>  
  13.                      <artifactId>artifactE</artifactId>  
  14.                      <version>1.0</version>  
  15.                      <type>bar</type>  
  16.               </dependency>  
  17.        </dependencies>  
  18.        ...  
  19. </project>  

 

 projectA和projectB项目共同依赖project,可以创建一个projectP

  1. <project>  
  2.        ...  
  3.        <dependencyManagement>  
  4.               <dependencies>  
  5.                      <dependency>  
  6.                             <groupId>groupC</groupId>  
  7.                             <artifactId>artifactC</artifactId>  
  8.                             <version>1.0</version>  
  9.                      </dependency>  
  10.                      <dependency>  
  11.                             <groupId>groupD</groupId>  
  12.                             <artifactId>artifactD</artifactId>  
  13.                             <version>1.0</version>  
  14.                      </dependency>  
  15.                      <dependency>  
  16.                             <groupId>groupE</groupId>  
  17.                             <artifactId>artifactE</artifactId>  
  18.                             <version>1.0</version>  
  19.                             <type>bar</type>  
  20.                      </dependency>  
  21.               </dependencies>  
  22.        </dependencyManagement>  
  23.        ...  
  24. </project>  

projectA

  1. <project>  
  2.        <modelVersion>4.0</modelVersion>  
  3.        <groupId>groupA</groupId>  
  4.        <artifactId>artifactA</artifactId>  
  5.        <version>1.0</version>  
  6.        <dependencies>  
  7.               <dependency>  
  8.                      <groupId>groupC</groupId>  
  9.                      <artifactId>artifactC</artifactId>  
  10.               </dependency>  
  11.               <dependency>  
  12.                      <groupId>groupD</groupId>  
  13.                      <artifactId>artifactD</artifactId>  
  14.               </dependency>  
  15.        </dependencies>  
  16.        ...  
  17. </project>  

 projectB

  1. <project>  
  2.        <modelVersion>4.0</modelVersion>  
  3.        <groupId>groupB</groupId>  
  4.        <artifactId>artifactB</artifactId>  
  5.        <version>1.0</version>  
  6.        <dependencies>  
  7.               <dependency>  
  8.                      <groupId>groupC</groupId>  
  9.                      <artifactId>artifactC</artifactId>  
  10.               </dependency>  
  11.               <dependency>  
  12.                      <groupId>groupE</groupId>  
  13.                      <artifactId>artifactE</artifactId>  
  14.               <!--因为artifactE的类型不是默认的jar,所以这里需要指定依赖项的类型-->  
  15.                      <type>bar</type>  
  16.               </dependency>  
  17.        </dependencies>  
  18.        ...  
  19. </project>  

我们可以只在projectA和projectB中申明需要使用的依赖项,而不必指定其对应的版本和作用范围等信息

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics