《maven in action》部分知识点总结
maven in action 的部分知识点总结
今天又将《maven in action》这本书看了一遍,总结了一下,大概需要的知识点
(一)解耦
使用maven,在没有任何实际的Java代码,我们就可以定义一个maven项目的pom,这个就是maven的一大优点,能让项目对象模型最大程度的和实际的代码相互独立,这个就叫做解耦,或者正交性。
当项目需要升级版本的时候,只需要修改pom,而不需要更改Java代码,在pom稳定之后,日常的Java代码开发公国基本不涉及pom的修改。
(二)依赖范围
在pom.xml的配置中,表示的是依赖范围
例如在pom.xml中的junit的坐标如下
<deprdency>
<groupId>junit</groupId>
<artifactId>junit</artificatId>
<version>4.7</version>
<scope>test</scope>
</dependency>
scope为依赖范围,在此处依赖范围是test,代表改以来只对测试有效。
换句话说,在测试代码中使用import Junit没有问题,但是在主代码中使用import Junit,则会造成编译错误,如果不声明依赖范围,那么默认值就是compile,表示改以来对主代码和测试代码都有效。
(三)maven的compile插件支持的Java版本
由于历史的原因,maven的核心插件之一---compiler插件默认只支持变异Java1.3,因此需要配置该插件使其支持Java的别的版本。
代码如下:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
这样配置后,该compiler插件支持Java1.5版本
聚合
为了能够使用一条命令就能构建 account-email和 account-persist两个模块,我们需要建立一个额外的名为 account-aggregator的模块,然后通过该模块构建整个项目的所有模块。 account-aggregator本身也是个 Maven项目,它的 POM如下
<project>
<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>
注意:packaging的类型为pom ,module的值是一个以当前POM为主目录的相对路径。
继承
可声明父POM供子POM继承
父模块POM如下:
<project>
<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>
子模块声明继承如下
<project>
<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>
...
</project>
最后,同样还需要把 account-parent加入到聚合模块account-aggregator中。聚合的 POM如下:
<project>
<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>
<module> account-parent</module>
</modules>
</project>
注意:
1、子模块没有声明groupId和version, 这两个属性继承至父模块。但如果子模块有不同与父模块的 groupId、version ,也可指定;
2、不应该继承artifactId,如果groupId ,version,artifactId 完全继承的话会造成坐标冲突;另外即使使用不同的 groupId或version,同样的 artifactId也容易产生混淆。
3、使用继承后 parent也必须像自模块一样加入到聚合模块中。也就是在在聚合模块的 pom中加入account-parent
(四)聚合与继承的关系
区别 :
1.对于聚合模块来说,它知道有哪些被聚合的模块,但那些被聚合的模块不知道这个聚合模块的存在。
2.对于继承关系的父 POM来说,它不知道有哪些子模块继承与它,但那些子模块都必须知道自己的父 POM是什么。
共同点 :
1.聚合 POM与继承关系中的父POM的 packaging都是pom
2.聚合模块与继承关系中的父模块除了 POM之外都没有实际的内容。
(五)maven可继承pom元素
groupId :项目组 ID ,项目坐标的核心元素;
version :项目版本,项目坐标的核心元素;
description :项目的描述信息;
organization :项目的组织信息;
inceptionYear :项目的创始年份;
url :项目的 url 地址
develoers :项目的开发者信息;
contributors :项目的贡献者信息;
distributionManagerment :项目的部署信息;
issueManagement :缺陷跟踪系统信息;
ciManagement :项目的持续继承信息;
scm :项目的版本控制信息;
mailingListserv :项目的邮件列表信息;
properties :自定义的 Maven 属性;
dependencies :项目的依赖配置;
dependencyManagement :醒目的依赖管理配置;
repositories :项目的仓库配置;
build :包括项目的源码目录配置、输出目录配置、插件配置、插件管理配置等;
reporting :包括项目的报告输出目录配置、报告插件配置等。
(六)依赖管理---dependencyManagement
看上面的maven可继承pom元素中有dependencies元素,说明依赖会被继承的。
想到一般多个子模块会痛是依赖springframework中的架包,那么可以将这些依赖加入到父模块中,这样子模块就可以一处相应的依赖,简化配置。
但是这个有一个问题,就是不能确定将来添加的子模块就一定需要这几个依赖,加入将来又要添加一个子模块,但是需要别的架包,那么根本就不需要依赖springframework的架包
可以使用dependencyManagement来让子模块继承到父模块的依赖配置,又能保证子模块依赖使用的灵活性。
父模块代码如下:
<project xmlnsxmlns="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>
<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 >
</project>
注意这里的父模块的pom代码,这里将用到的架包的的版本已maven参数的形式提取了出来,不仅消除了一些重复,也使得各以来的版本处于更明显的位置
<properties>
<springframework.version>2.5.6<springframework.version>
<junit.version>4.7</junit.version>
</properties>
子模块pom代码如下
<project xmlnsxmlns="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>
</ parent >
<artifactId>account-email</artifactId>
<name>Account Email</name>
<properties>
<javax.mail.version>1.4.1</javax.mail.version>
<greenmail.version>1.3.1b</greenmail.version>
</properties>
<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>
<build>
</build>
</project>
注意子模块的写法,需要只有自己这个模块需要的架包,就在相应的架包依赖中写version,否则不需要写,共有的架包的依赖已经在父模板中已经写好,只需要继承父模板即可,兹莫办只需要配置相应的groupId和actifactId就可以获得相应的以来信息,从而引入正确的依赖;
使用管理依赖的好处
1.代码量减少,不用多次重复声明version等元素
2.version在父模块中管理,父变则子变,便于修改,也便于版本的统一,不会发生不同的子模板使用的依赖的版本不一致的情况,降低冲突的几率。
3.dependencyManagement一旦定义,可以复用
《maven in action》部分知识点总结的更多相关文章
- struts2的action的知识点和利用action向页面注入值的操作
1. Action的顺序,会先搜索指定名字下的包的action,如果找不到会去搜索默认路径下的包下的action. 2. 如果没有给action设置值,那么action会有一些默认 ...
- Maven一些零散的知识点
Maven常用命令: 1. 创建Maven的普通java项目: mvn archetype:create -DgroupId=com.yida.framework -DartifactId=hello ...
- Maven简介与简单使用
Maven项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具. Maven 除了以程序构建能力为特色之外,还提供高级项目管理工具.由于 Maven 的缺省构建 ...
- 构建工具maven
构建工具maven =UTF-8''Gradle Effective Implementation Guide.pdf: http://www.t00y.com/file/76854506 b ...
- maven 介绍(zz )
Maven 编辑 目录 1简介 2特点 3常用命令 4推荐书籍 5Win7配置 6生命周期 1 1简介 Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构 ...
- 项目构建工具maven的使用方法
最近在开发javaweb项目中有用到maven,以前并不是很了解,于是学习了一些相关内容,记之共享. 本篇内容在Windows环境下实施,JDK版本使用的1.7.0_79. 一.maven是什么? 简 ...
- Java-Maven:Maven百科
ylbtech-Java-Maven:Maven百科 1.返回顶部 1. Maven项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具.Maven 除了以程序 ...
- Android 广播大全 Intent Action 事件详解
Android 广播大全 Intent Action 事件详解 投稿:mrr 字体:[增加 减小] 类型:转载 时间:2015-10-20我要评论 这篇文章主要给大家介绍Android 广播大全 In ...
- maven学习笔记(超详细总结)
目录 项目管理利器--maven 第1章 maven概述 1-1 项目管理利器-maven简介 1.1.1 什么是maven 1.1.2 什么是依赖管理 1.1.3 传统项目的依赖管理 1.1.4 m ...
随机推荐
- RabbitMQ 在 web 页面 创建 exchange, queue, routing key
这里只是为了展示, 在实际开发中一般在消费端通过 注解来自动创建 消费端: https://www.cnblogs.com/huanggy/p/9695934.html 1, 创建 Exchange ...
- 18.Odoo产品分析 (二) – 商业板块(10) – 电子商务(2)
查看Odoo产品分析系列--目录 接上一篇Odoo产品分析 (二) – 商业板块(10) – 电子商务(1) 6. 高级属性 除了我们到目前为止已经覆盖基本选项,Odoo在产品页面还提供了一些高级选项 ...
- 测者的测试技术笔记:Screenplay 模式(Journey 模式)
Screenplay模式 Junit的Screenplay 举例 Actor theReceptionist =newActor().with(WebBrowsing.ability()) theRe ...
- ajax参数
$.ajax({ type: "GET", url: "Login.ashx", dataType: "text", cache: fals ...
- Linux进程上下文切换过程context_switch详解--Linux进程的管理与调度(二十一)
1 前景回顾 1.1 Linux的调度器组成 2个调度器 可以用两种方法来激活调度 一种是直接的, 比如进程打算睡眠或出于其他原因放弃CPU 另一种是通过周期性的机制, 以固定的频率运行, 不时的检测 ...
- 20个必不可少的Python库
转载:http://www.python123.org/tutorials/58b41f2a28c8f30100bd41dc 读者们好.今天我将介绍20个属于我常用工具的Python库,我相信你看完之 ...
- SpringCloud之初识Feign ----- 分布式负载自动拼接请求的URL
在前面的学习中,我们使用了Ribbon的负载均衡功能,大大简化了远程调用时的代码: String baseUrl = "http://user-service/user/"; Us ...
- 【存储】RAID磁盘阵列选择
RAID磁盘阵列(Redundant Arrays of Inexpensive Disks) 一个基本思想:将多个容量较小.相对廉价的磁盘进行有机组合,从而以较低的成本获得与昂贵大容量磁盘相当的容量 ...
- 最小生成树 A - 畅通工程
dalao视频:https://www.bilibili.com/video/av4768483 https://www.bilibili.com/video/av4768483?p=2 省政府“畅通 ...
- Spring国际化模块
1.Spring3.1.0实现原理分析(二).国际化(i18n) https://blog.csdn.net/roberts939299/article/details/69666291