码迷,mamicode.com
首页 > 其他好文 > 详细

项目构建之maven篇:7.聚合与继承

时间:2014-06-22 15:49:28      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:maven   继承   聚合   

聚合:


为方便项目构建,通常将聚合模块放在项目目录的最顶层,其他模块则作为聚合模块的子目录存在,

这样我们得到源码的时候,最快发现的是聚合模块的pom

bubuko.com,布布扣



bubuko.com,布布扣


运行命令

bubuko.com,布布扣


查看结果

bubuko.com,布布扣


源代码下载


继承


创建parent项目,用于被继承

bubuko.com,布布扣

bubuko.com,布布扣


parent/pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.demo.inheritance</groupId>
  <artifactId>parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
</project>

创建子模块sub-a,继承parent

bubuko.com,布布扣

bubuko.com,布布扣

bubuko.com,布布扣


sub-a/pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.demo.inheritance</groupId>
    <artifactId>parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>sub-a</artifactId>
</project>

pom说明

parent元素下的groupId,artifactId,version指明父模块的坐标(必需)

parent元素下的relativePath:父模块的pom文件所在位置,默认: ../pom.xml,即maven的默认父pom在上一层目录下


可继承的pom元素


bubuko.com,布布扣


依赖管理


parent/pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.demo.inheritance</groupId>
	<artifactId>parent</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>
	<modules>
		<module>sub-a</module>
	</modules>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-core</artifactId>
				<version>3.0.7.RELEASE</version>
			</dependency>
		</dependencies>
	</dependencyManagement>
</project>

dependencyManagement元素可以让子元素继承到父模块的依赖配置,又能保证子模块依赖使用的灵活性

dependencyManagement下的dependency只会依赖声明,不会引用实际依赖


sub-a/pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.demo.inheritance</groupId>
    <artifactId>parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath></relativePath>
  </parent>
  <artifactId>sub-a</artifactId>
  <dependencies>
  	<dependency>
  		<groupId>org.springframework</groupId>
		<artifactId>spring-core</artifactId>
  	</dependency>
  </dependencies>
</project>

子模块的依赖只要使用groupId,artifaceId,不需要version


插件管理


parent/pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.demo.inheritance</groupId>
	<artifactId>parent</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>
	<modules>
		<module>sub-a</module>
	</modules>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-core</artifactId>
				<version>3.0.7.RELEASE</version>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-dependency-plugin</artifactId>
					<version>2.8</version>
					<executions>
						<execution>
							<!--指定在哪个阶段后执行目标 -->
							<phase>test</phase>
							<goals>
								<!--执行的目标 -->
								<goal>tree</goal>
							</goals>
						</execution>
					</executions>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>
</project>

sub-a/pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.demo.inheritance</groupId>
		<artifactId>parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>sub-a</artifactId>
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

与依赖管理类似:插件版本可以不用

源代码下载


注意:

当项目中有多个模块有同样的插件配置时,应当将配置移动父pom的pluginManagement元素中,即各个模块

对同一个插件具体的配置不尽相同,也应当使用父pom的pluginManagement元素统一版本,甚至可以要求

将所有的用到的插件的版本在父pom的pluginManagement元素中统一声明插件版本



项目构建之maven篇:7.聚合与继承,布布扣,bubuko.com

项目构建之maven篇:7.聚合与继承

标签:maven   继承   聚合   

原文地址:http://blog.csdn.net/wobendiankun/article/details/32352743

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!