在平面几何中坐标(x,y)可以标识平面中唯一的一点。
样例:
依赖配置主要包含如下元素:
<!--添加依赖配置--> <dependencies> <!--项目要使用到junit的jar包,所以在这里添加junit的jar包的依赖--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> <!--项目要使用到Hello的jar包,所以在这里添加Hello的jar包的依赖--> <dependency> <groupId>me.gacl.maven</groupId> <artifactId>Hello</artifactId> <version>0.0.1-SNAPSHOT</version> <scope>compile</scope> </dependency> </dependencies>
依赖范围scope用来控制依赖和编译,测试,运行的classpath的关系. 主要的是三种依赖关系如下:
1.compile: 默认编译依赖范围。对于编译,测试,运行三种classpath都有效
2.test:测试依赖范围。只对于测试classpath有效
3.provided:已提供依赖范围。对于编译,测试的classpath都有效,但对于运行无效。因为由容器已经提供,例如servlet-api
4.runtime:运行时提供。例如:jdbc驱动
MakeFriends.jar直接依赖于HelloFriends.jar,而HelloFriends.jar又直接依赖于Hello.jar,那么MakeFriends.jar也依赖于Hello.jar,这就是传递性依赖,只不过这种依赖是间接依赖,如下图所示:
用来统一存储所有Maven共享构建的位置就是仓库
根据Maven坐标定义每个构建在仓库中唯一存储路径,大致为:groupId/artifactId/version/artifactId-version.packaging
每个用户只有一个本地仓库,默认是在~/.m2/repository/,~代表的是用户目录
1、中央仓库:Maven默认的远程仓库,URL地址:http://search.maven.org/
2、私服:是一种特殊的远程仓库,它是架设在局域网内的仓库
Maven生命周期就是为了对所有的构建过程进行抽象和统一,包括项目清理,初始化,编译,打包,测试,部署等几乎所有构建步骤
Maven有三套相互独立的生命周期,请注意这里说的是"三套",而且"相互独立",这三套生命周期分别是:
再次强调一下它们是相互独立的,你可以仅仅调用clean来清理工作目录,仅仅调用site来生成站点。当然你也可以直接运行 mvn clean install site 运行所有这三套生命周期。
clean生命周期每套生命周期都由一组阶段(Phase)组成,我们平时在命令行输入的命令总会对应于一个特定的阶段。比如,运行mvn clean ,这个的clean是Clean生命周期的一个阶段。有Clean生命周期,也有clean阶段。Clean生命周期一共包含了三个阶段:
"mvn clean" 中的clean就是上面的clean,在一个生命周期中,运行某个阶段的时候,它之前的所有阶段都会被运行,也就是说,"mvn clean"等同于 mvn pre-clean clean ,如果我们运行 mvn post-clean ,那么 pre-clean,clean 都会被运行。这是Maven很重要的一个规则,可以大大简化命令行的输入。
Site生命周期pre-site 执行一些需要在生成站点文档之前完成的工作
这里经常用到的是site阶段和site-deploy阶段,用以生成和发布Maven站点,这可是Maven相当强大的功能,Manager比较喜欢,文档及统计数据自动生成,很好看。
Default生命周期Default生命周期是Maven生命周期中最重要的一个,绝大部分工作都发生在这个生命周期中。这里,只解释一些比较重要和常用的阶段:
运行任何一个阶段的时候,它前面的所有阶段都会被运行,这也就是为什么我们运行mvn install 的时候,代码会被编译,测试,打包。此外,Maven的插件机制是完全依赖Maven的生命周期的,因此理解生命周期至关重要。
如果我们想一次构建多个项目模块,那我们就需要对多个项目模块进行聚合
<modules> <module>模块一</module> <module>模块二</module> <module>模块三</module>
</modules>
例如:对项目的Hello、HelloFriend、MakeFriends这三个模块进行聚合
<modules> <module>../Hello</module> <module>../HelloFriend</module> <module>../MakeFriends</module> </modules>
其中module的路径为相对路径。
继承为了消除重复,我们把很多相同的配置提取出来,例如:grouptId,version等
1 <parent> 2 <groupId>me.gacl.maven</groupId> 3 <artifactId>ParentProject</artifactId> 4 <version>0.0.1-SNAPSHOT</version> 5 <relativePath>../ParentProject/pom.xml</relativePath> 6 </parent>
继承代码过程中,可以定义属性,例如:
1 <properties> 2 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 3 <junit.version>4.9</junit.version> 4 <maven.version>0.0.1-SNAPSHOT</maven.version> 5 </properties>
访问属性的方式为${junit.version},例如:
1 <dependency> 2 <groupId>junit</groupId> 3 <artifactId>junit</artifactId> 4 <version>${junit.version}</version> 5 <scope>test</scope> 6 </dependency>
1 <dependencyManagement> 2 <dependencies> 3 <dependency> 4 <groupId>junit</groupId> 5 <artifactId>junit</artifactId> 6 <version>${junit.version}</version> 7 <scope>test</scope> 8 </dependency> 9 <dependency> 10 <groupId>cn.itcast.maven</groupId> 11 <artifactId>HelloFriend</artifactId> 12 <version>${maven.version}</version> 13 <type>jar</type> 14 <scope>compile</scope> 15 </dependency> 16 </dependencies> 17 </dependencyManagement>
这样的好处是子模块可以有选择行的继承,而不需要全部继承。
聚合主要为了快速构建项目,继承主要为了消除重复
创建四个Maven项目,如下图所示:
这四个项目放在同一个目录下,方便后面进行聚合和继承
Parent项目是其它三个项目的父项目,主要是用来配置一些公共的配置,其它三个项目再通过继承的方式拥有Parent项目中的配置,首先配置Parent项目的pom.xml,添加对项目的Hello、HelloFriend、MakeFriends这三个模块进行聚合以及jar包依赖,pom.xml的配置信息如下:
Parent项目的pom.xml配置
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 5 <groupId>me.gacl.maven</groupId> 6 <artifactId>Parent</artifactId> 7 <version>0.0.1-SNAPSHOT</version> 8 <packaging>pom</packaging> 9 10 <name>Parent</name> 11 <url>http://maven.apache.org</url> 12 13 <!-- 对项目的Hello、HelloFriend、MakeFriends这三个模块进行聚合 --> 14 <modules> 15 <module>../Hello</module> 16 <module>../HelloFriend</module> 17 <module>../MakeFriends</module> 18 </modules> 19 20 <!-- 定义属性 --> 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <junit.version>4.9</junit.version> 24 <maven.version>0.0.1-SNAPSHOT</maven.version> 25 </properties> 26 27 <!-- 用dependencyManagement进行jar包依赖管理 --> 28 <dependencyManagement> 29 <!-- 配置jar包依赖 --> 30 <dependencies> 31 <dependency> 32 <groupId>junit</groupId> 33 <artifactId>junit</artifactId> 34 <!-- 访问junit.version属性 --> 35 <version>${junit.version}</version> 36 <scope>test</scope> 37 </dependency> 38 <dependency> 39 <groupId>me.gacl.maven</groupId> 40 <artifactId>Hello</artifactId> 41 <!-- 访问maven.version属性 --> 42 <version>${maven.version}</version> 43 <scope>compile</scope> 44 </dependency> 45 <dependency> 46 <groupId>me.gacl.maven</groupId> 47 <artifactId>HelloFriend</artifactId> 48 <!-- 访问maven.version属性 --> 49 <version>${maven.version}</version> 50 </dependency> 51 </dependencies> 52 </dependencyManagement> 53 </project>
在Hello项目的pom.xml中继承Parent项目的pom.xml配置
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 4 <modelVersion>4.0.0</modelVersion> 5 <artifactId>Hello</artifactId> 6 7 <!-- 继承Parent项目中的pom.xml配置 --> 8 <parent> 9 <groupId>me.gacl.maven</groupId> 10 <artifactId>Parent</artifactId> 11 <version>0.0.1-SNAPSHOT</version> 12 <!-- 使用相对路径 --> 13 <relativePath>../Parent/pom.xml</relativePath> 14 </parent> 15 16 <dependencies> 17 <dependency> 18 <groupId>junit</groupId> 19 <artifactId>junit</artifactId> 20 </dependency> 21 </dependencies> 22 </project>
在HelloFriend项目的pom.xml中继承Parent项目的pom.xml配置
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <artifactId>HelloFriend</artifactId> 5 <name>HelloFriend</name> 6 7 <!-- 继承Parent项目中的pom.xml配置 --> 8 <parent> 9 <groupId>me.gacl.maven</groupId> 10 <artifactId>Parent</artifactId> 11 <version>0.0.1-SNAPSHOT</version> 12 <relativePath>../Parent/pom.xml</relativePath> 13 </parent> 14 <dependencies> 15 <dependency> 16 <!-- Parent项目的pom.xml文件配置中已经指明了要使用的Junit的版本号,因此在这里添加junit的依赖时, 17 可以不指明<version></version>和<scope>test</scope>,会直接从Parent项目的pom.xml继承 --> 18 <groupId>junit</groupId> 19 <artifactId>junit</artifactId> 20 </dependency> 21 <!-- HelloFriend项目中使用到了Hello项目中的类,因此需要添加对Hello.jar的依赖 22 Hello.jar的<version>和<scope>也已经在Parent项目的pom.xml文件配置中已经指明了 23 因此这里也可以省略不写了 24 --> 25 <dependency> 26 <groupId>me.gacl.maven</groupId> 27 <artifactId>Hello</artifactId> 28 </dependency> 29 </dependencies> 30 </project>
在MakeFriends项目的pom.xml中继承Parent项目的pom.xml配置
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <artifactId>MakeFriends</artifactId> 5 <!-- 继承Parent项目中的pom.xml配置 --> 6 <parent> 7 <groupId>me.gacl.maven</groupId> 8 <artifactId>Parent</artifactId> 9 <version>0.0.1-SNAPSHOT</version> 10 <relativePath>../Parent/pom.xml</relativePath> 11 </parent> 12 <dependencies> 13 <dependency> 14 <!-- Parent项目的pom.xml文件配置中已经指明了要使用的Junit的版本号,因此在这里添加junit的依赖时, 15 可以不指明<version></version>和<scope>test</scope>,会直接从Parent项目的pom.xml继承 --> 16 <groupId>junit</groupId> 17 <artifactId>junit</artifactId> 18 </dependency> 19 <dependency> 20 <!-- MakeFriends项目中使用到了HelloFriend项目中的类,因此需要添加对HelloFriend.jar的依赖 21 HelloFriend.jar的<version>和<scope>也已经在Parent项目的pom.xml文件配置中已经指明了 22 因此这里也可以省略不写了 23 --> 24 <groupId>me.gacl.maven</groupId> 25 <artifactId>HelloFriend</artifactId> 26 </dependency> 27 </dependencies> 28 </project>
以上的四个项目的pom.xml经过这样的配置之后,就完成了在Parent项目中聚合Hello、HelloFriend、MakeFriends这三个子项目(子模块),而Hello、HelloFriend、MakeFriends这三个子项目(子模块)也继承了Parent项目中的公共配置,这样就可以使用Maven一次性构建所有的项目了,如下图所示:
选中Parent项目的pom.xml文件→【Run As】→【Maven install】,这样Maven就会一次性同时构建Parent、Hello、HelloFriend、MakeFriends这四个项目,如下图所示: