maven之聚合/继承

来源:互联网 发布:我的世界手机版龙珠js 编辑:程序博客网 时间:2024/05/24 07:08

1 聚合

一般项目中包含很多个模块,而每个模块就可以当作一个maven项目,那么每个模块聚合来完成一个大的项目呢?maven就实现了这个功能。例如在《maven实战》中实现的一个账户注册服务,它包含account-email,account-persis等五个模块,需要将这五个模块聚合在一起以实现用户账户注册服务。

以下对account-email,account-persis两个模块的聚合进行说明:

1.1 创建一个名为account-aggregator模块,这个模块本身就是一个maven项目,它也需要有一个pom.xml文件,内容如下:

<span style="font-family:Microsoft YaHei;"><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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.juvenxu.mvnbook.account</groupId><artifactId>account-aggregator</artifactId><version>1.0.0-SNAPSHOT</version><packaging>pom</packaging><name>Account Aggregator</name><modules><module>account-email</module><module>account-persist</module></modules></project></span>

<packaging>元素表示打包方式,对于聚合模块,打包方式一定是pom;<modules>元素表示聚合的模块集合,一个<module>元素代表一个模块,它的值就是要模块的artfactId,同是要注意各个模块文件的关系如下:

<module>元素的值也指示了聚合模块与被聚合模块的位置关系,如果<module>元素值如下:

<modules><module><span style="font-family:Microsoft YaHei;">../</span>account-email</module><module>../account-persist</module></modules>
此时,各个模块文件之间位置的关系:


2 继承

根据经验,一个项目的各个模块中不可避免重复使用各种依赖和插件,这些都是在各自模块中的pom.xml文件中进行配置,如果模块很多的话,这样的做法无疑加大了程序员的工作量,同时很容易出现错误,例如版本的设置等。maven当然想到了这种情况,所以maven提供了继承功能。

2.1 继承一般步骤

2.1.1 创建一个account-parent模块,该模块也可以作为一个maven项目,建立一个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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.juvenxu.mvnbook.account</groupId><artifactId>account-parent</artifactId><version>1.0.0-SNAPSHOT</version><packaging>pom</packaging><name>Account Parent</name></project>
2.1.2 修改需要继承上模块的子模块的pom.xml文件,如修改account-email中的pom.xml文件:


<pre name="code" class="html"><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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.juvenxu.mvnbook.account</groupId><artifactId>account-parent</artifactId><version>1.0.0-SNAPSHOT</version><relativePath>../account-parent/pom.xml</relativePath></parent><artifactId>account-email</artifactId><name>Account Email</name><dependencies><span style="font-family:Microsoft YaHei;">...</span></dependencies><build></build></project>

在子模块中通过<relativePath>元素的值来指明子模块与父模块的(文件)关系:

2.1.3 最后需要注意的是新生成的account-parent模块需要配置到account-aggregator的pom.xml中的<modules>中,如下:

<modules><module>account-email</module><module>account-persist</module><module>account-parent</module></modules>

2.2 继承之依赖继承

项目中最常用的应该就是依赖继承了吧,在父模块的pom.xml文件中的<dependencyManagement>元素来定义,然后在子模块的pom.xml中通过<dependencies>元素来确定继承关系。

2.2.1 父模块的pom.xml<dependencyManagement>元素内容:

<dependencyManagement><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>${springframework.version}</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version><scope>test</scope></dependency></dependencies></dependencyManagement>
2.2.2 子模块account-email的pom.xml中<dependencies>元素内容:

<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId></dependency><dependency><groupId>javax.mail</groupId><artifactId>mail</artifactId><version>${javax.mail.version}</version></dependency><dependency><groupId>com.icegreen</groupId><artifactId>greenmail</artifactId><version>${greenmail.version}</version><scope>test</scope></dependency></dependencies>
2.2.3 可以看出,account-email中继承了account-parent中的五个依赖,子模块的pom.xml中<dependencies>元素相比父模块的pom.xml中<dependencyManagement>元素的内容,少了version,scope等,只剩下groupId和artfactId的配置,其他的元素都继承父模块。其他没有继承父模块的依赖就必须配置version等,例如上面的greenmail。

这样机制貌似没有减少太多内容,但是为什么还是会用这种机制呢?父模块中的<dependencyManagement>元素能改指定其下所有子模块依赖的版本,防止出现多个子模块使用依赖版本不同的情况。

同时,如果子模块不需要全部继承父模块中的依赖是,只需要在子模块的pom.xml中的<dependencies>元素中不配置出来就搞定。

2.3 继承之插件继承

除了依赖继承,插件继承也使用的比较多,在父模块的pom.xml文件中的<pluginManagement>元素来定义,然后在子模块的pom.xml中通过<plugins>元素来确定继承关系。

2.3.1父模块的pom.xml<pluginManagement>元素内容:

<build><pluginManagement><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.5</source><target>1.5</target></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><configuration><encoding>UTF-8</encoding></configuration></plugin></plugins></pluginManagement></build>
2.3.2 子模块的pom.xml中<plugins>元素内容:

<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-source-plugin</artifactId></plugin></plugins></build>
2.3.3 和依赖继承类似,不在说明。

3 聚合和继承的关系

对于聚合模块,它知道有哪些被聚合的模块,但是被聚合的模块不知道这个聚合步骤;对于继承,父模块不知道被哪些子模块继承,但是子模块都必须知道自己的父模块。

在现实项目中,一般一个pom既可以聚合pom,又可以是父pom,例如,可以将account-aggregator和account-parent合并在一个新的account-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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.juvenxu.mvnbook.account</groupId><artifactId>account-parent</artifactId><version>1.0.0-SNAPSHOT</version><packaging>pom</packaging><name>Account Parent</name><modules><module>account-email</module><module>account-persist</module></modules><properties><springframework.version>2.5.6</springframework.version><junit.version>4.7</junit.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>${springframework.version}</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version><scope>test</scope></dependency></dependencies></dependencyManagement><build><pluginManagement><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.5</source><target>1.5</target></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><configuration><encoding>UTF-8</encoding></configuration></plugin></plugins></pluginManagement></build></project>


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 打工要不到工资怎么办 导师回复跟敷衍怎么办 在演讲时紧张怎么办 老房子怎么办不动产证 脚角质层厚粗糙怎么办 皮革包染色了怎么办 皮革包被染色了怎么办 面膜泥干了怎么办 淘宝店铺生意不好怎么办 淘宝商品换主图被下架了怎么办 壁纸店没生意怎么办 支付宝不能借钱怎么办 淘宝号想注销怎么办 被陌生号码骚扰怎么办 买家辱骂卖家怎么办 淘宝禁止创建店铺怎么办 闲鱼上买东西被骗了怎么办 恶意买家付款了怎么办 换手机号了淘宝怎么办 旺旺发不了图片怎么办 拼多多买家投诉怎么办 拼多多恶意用户怎么办 淘宝运单号填错了怎么办 淘宝退货卖家不处理怎么办 淘宝长时间不发货怎么办 实体店卖假货怎么办 淘宝店暂停服务怎么办 淘宝直播开始没人怎么办 店铺违规虚假交易怎么办 电视无频道信息怎么办 hdp直播频道丢失怎么办 小红书订单删了怎么办 退款售后删除我怎么办 毛衣袖子肥了怎么办 店面生意不好要怎么办 中国的农民以后怎么办 做到不好的梦怎么办 美瞳线眼睛肿了怎么办 淘宝买家限购怎么办 一楼房屋潮湿怎么办 一楼屋子潮湿怎么办