Maven 的聚合(多模块)和 Parent 继承

在使用多模块时,子模块总要指定聚合的 pom 为 parent

一般配置:

1.parentr父项目,引用私服,放一下maven jar包,或者依赖项目

<groupId>ins.fastapp</groupId>
	<artifactId>lifeBusiness-parent</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>
	<name>lifeBusiness-parent</name>
	<description>fast Parent</description>
	<distributionManagement>
		<repository>
			<id>nexus</id>
			<name>Team Nexus Release Repository</name>
			<url>http:/IP地址/nexus/content/repositories/releases</url>
		</repository>
		<snapshotRepository>
			<id>nexus-snapshot</id>
			<name>Team Nexus Snapshot Repository</name>
			<url>http://IP地址/nexus/content/repositories/snapshots</url>
			<uniqueVersion>false</uniqueVersion>
		</snapshotRepository>
	</distributionManagement>

	<repositories>
		<!-- 如有Nexus私服, 取消注释并指向正确的服务器地址. -->
		<repository>
			<id>nexus</id>
			<name>Team 
				Nexus Repository</name>
			<url>http:/IP地址/nexus/content/groups/public</url>
			<releases>
				<enabled>true</enabled>
			</releases>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</repository>
	</repositories>

2.dal逻辑处理、common公共字典/功能,两模块引用父项目

<parent>
		<artifactId>lifeBusiness-parent</artifactId>
		<groupId>ins.fastapp</groupId>
		<version>0.0.1-SNAPSHOT</version>
		<relativePath>../lifeBusiness-parent/pom.xml</relativePath>
	</parent>

3.web入口,control层,依赖dal和common

<parent>
		<artifactId>lifeBusiness-parent</artifactId>
		<groupId>ins.fastapp</groupId>
		<version>0.0.1-SNAPSHOT</version>
		<relativePath>../lifeBusiness-parent/</relativePath>
	</parent>
	<artifactId>lifeBusiness-web</artifactId>
	<packaging>war</packaging>
	<name>lifeBusiness-web</name>
	<description>fastframework webapp</description>
	<dependencies>
		<dependency>
			<groupId>jaxen</groupId>
			<artifactId>jaxen</artifactId>
		</dependency>
		<dependency>
			<groupId>ins.fastapp</groupId>
			<artifactId>lifeBusiness-commons</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>ins.fastapp</groupId>
			<artifactId>lifeBusiness-dal</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
</dependencies>

继承

继承是 Maven 中很强大的一种功能,继承可以使得子POM可以获得 parent 中的各项配置,可以对子pom进行统一的配置和依赖管理。父POM中的大多数元素都能被子POM继承,这些元素包含:

groupId
version
description
url
inceptionYear
organization
licenses
developers
contributors
mailingLists
scm
issueManagement
ciManagement
properties
dependencyManagement
dependencies
repositories
pluginRepositories
build
plugin executions with matching ids
plugin configuration
etc.
reporting
profiles
注意下面的元素,这些都是不能被继承的。

artifactId
name
prerequisites
想要添加 parent,只需要像下面这样写。



4.0.0

org.codehaus.mojo my-parent 2.0 ../my-parent

my-project


其中relativePath元素不是必须的,指定后会优先从指定的位置查找父pom。

聚合(或多模块)

具有模块的项目被称为多模块或聚合项目。模块是此POM列出并作为一组执行的项目。通过一个pom打包的项目可以将它们列为模块来聚合成一组项目进行构建,这些模块名是这些项目的相对目录。

<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
                      https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.codehaus.mojo</groupId>
  <artifactId>my-parent</artifactId>
  <version>2.0</version>
  <packaging>pom</packaging>

  <modules>
    <module>my-project</module>
    <module>another-project</module>
  </modules>
</project>

在列出模块时,不需要自己考虑模块间依赖关系,即POM给出的模块排序并不重要。Maven将对模块进行拓扑排序,使得依赖关系始终在依赖模块之前构建。

聚合 VS 父POM

虽然聚合通常伴随着父POM的继承关系,但是这两者不是必须同时存在的,从上面两者的介绍可以看出来,这两者的都有不同的作用,他们的作用不依赖于另一个的配置。

父POM是为了抽取统一的配置信息和依赖版本控制,方便子POM直接引用,简化子POM的配置。聚合(多模块)则是为了方便一组项目进行统一的操作而作为一个大的整体,所以要真正根据这两者不同的作用来使用,不必为了聚合而继承同一个父POM,也不比为了继承父POM而设计成多模块。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值