maven的聚合模块和pom继承使用(六)


转载请注明出处哈:http://yanan0628.iteye.com/blog/2270411
 

1.什么是maven的聚合模块?

聚合模块为项目目录的最顶层,其他模块作为聚合模块子目录而存在。目的是为一次构建多个项目模块。

 

1.1 项目结构

 

 maven的聚合模块和pom继承使用(六)_第1张图片

我自己化了一个关于项目的关系:

maven的聚合模块和pom继承使用(六)_第2张图片

 

1.2 pom标签配置-module

    maven-aggregate作为maven pom项目,需要定义pom.xml的 packaging 为 pom类型

    我们还是参照 maven的依赖特性,冲突解决(五) 创建 maven-01 maven-02  maven-03工程,通过maven-aggregate pom工程 对这三个maven项目做一个聚合,配置如下:

  maven的聚合模块和pom继承使用(六)_第3张图片


  4.0.0

  com.sohu.train
  maven-aggregate
  0.0.1-SNAPSHOT
  pom

  
  
  	../maven-01
  	../maven-02
  	..maven-03
   
  

  

   对 maven-aggregate执行下mvn clean compile

  

[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.sohu.train:maven-02:jar:1.0-SNAPSHORT
[WARNING] 'dependencies.dependency.exclusions.exclusion.groupId' for junit:junit:jar is missing. @ line 38, column 16
[WARNING] 'dependencies.dependency.exclusions.exclusion.artifactId' for junit:junit:jar is missing. @ line 38, column 16
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:   //构建 maven 的反应堆,执行清理 编译的顺序
[INFO] 
[INFO] maven-02
[INFO] maven-01
[INFO] maven-03
[INFO] maven-aggregate
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-02 1.0-SNAPSHORT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-02 ---
[INFO] Deleting E:\train\test\maven-02\target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-02 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\train\test\maven-02\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-02 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to E:\train\test\maven-02\target\classes
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-01 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-01 ---
[INFO] Deleting E:\train\test\maven-01\target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-01 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-01 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to E:\train\test\maven-01\target\classes
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-03 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-03 ---
[INFO] Deleting E:\train\test\maven-03\target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-03 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\train\test\maven-03\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-03 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to E:\train\test\maven-03\target\classes
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-aggregate 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-aggregate ---
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:     // 可以看到 对3个maven项目 一起执行编译
[INFO] 
[INFO] maven-02 ........................................... SUCCESS [  1.874 s]
[INFO] maven-01 ........................................... SUCCESS [  0.108 s]
[INFO] maven-03 ........................................... SUCCESS [  0.158 s]
[INFO] maven-aggregate .................................... SUCCESS [  0.002 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.263 s
[INFO] Finished at: 2016-01-15T14:35:41+08:00
[INFO] Final Memory: 14M/227M
[INFO] ------------------------------------------------------------------------
 这样我们就完成 maven对子模块的聚合 ,像我们平常用的dubbo jetty tomcat spring hibernate等等 都是按模块 去开发,一次编译 各个模块都能紧密联系在一起了。我们在平常开发项目也需要适当的去分模块开发,
这是dubbo的子模块,分了很多子模块:

 maven的聚合模块和pom继承使用(六)_第4张图片

 

2.什么是pom继承?

pom继承是为了抽出重复配置,通常配置在父模块中,为子模块提供使用,这样可以做到“一处声明,处处使用”。

2.1 继承结构

maven的聚合模块和pom继承使用(六)_第5张图片

 

2.2 pom标签配置-parent,*Management

 1.常用的pom被继承的元素

 

    groupId :项目组 ID ,项目坐标的核心元素;  
    version :项目版本,项目坐标的核心元素;  
    description :项目的描述信息;  
    properties :自定义的 Maven 属性;  
    dependencies :项目的依赖配置;  
    dependencyManagement :醒目的依赖管理配置;  
    repositories :项目的仓库配置;  
    build :包括项目的源码目录配置、输出目录配置、插件配置、插件管理配置等;  
  

 

 1.配置maven-aggregate pom文件:


  4.0.0

  com.sohu.train
  maven-aggregate
  0.0.1-SNAPSHOT
  pom

  
  
  	../maven-01
  	../maven-02
  	../maven-03
   
  
  
  	3.8.1
  
  
  
  
  	
		
			junit
			junit
			${junit.version}
			test
		
		
  

 maven-01,maven-02,maven-03 需要配置parent ,dependencies,同时对junit做一个依赖,3个工程的pom配置如下:


	4.0.0
    
    
    	com.sohu.train
		maven-aggregate
		0.0.1-SNAPSHOT
		../maven-aggregate/pom.xml
    
	maven-03
	jar
        
	
		
			junit
			junit
		
	

  这样就完成pom的继承,讲公用的jar一次配置,就可以实现多处引用了。

 

 

所有文章:

   参考:项目管理和java文章列表

maven系列文章: 

  1).maven的安装及常用命令(一)

  2).maven的生命周期,插件介绍(二)

  3).maven的坐标,仓库介绍及配置(三)

  4).maven的pom介绍及配置(四)

  5).maven的依赖特性,冲突解决(五)

  6).maven的聚合模块和pom继承使用(六)

  7).maven的版本规范(七)

你可能感兴趣的:(工具-maven)