1.maven介绍
1-1.用途
管理第三方依赖,构建项目,整合测试,管理文档等等。
1-2.核心概念
- project pom文件的顶级元素
- modelVersion 所使用的object model版本,为了确保稳定的使用,这个元素是强制性的。除非maven开发者升级模板,否则不需要修改
- groupId 是项目创建团体或组织的唯一标志符,通常是域名倒写,如groupId org.apache.maven.plugins就是为所有maven插件预留的
- artifactId 是项目artifact唯一的基地址名
- packaging artifact打包的方式,如jar、war、ear等等。默认为jar。这个不仅表示项目最终产生何种后缀的文件,也表示build过程使用什么样的lifecycle。
- version artifact的版本,通常能看见为类似0.0.1-SNAPSHOT,其中SNAPSHOT表示项目开发中,为开发版本
- name 表示项目的展现名,在maven生成的文档中使用
- url表示项目的地址,在maven生成的文档中使用
- description 表示项目的描述,在maven生成的文档中使用
- dependencies 表示依赖,在子节点dependencies中添加具体依赖的groupId artifactId和version
- build 表示build配置
- parent 表示父pom
2.maven的配置
2-1.下载并解压
- 2-1-1.下载地址:maven下载地址;选择
Binary tar.gz archive
或者Binary zip archive
都可以。 - 解压到任意地址,例如:
/Users/lizhongren1/maven/apache-maven-3.3.9
, 下面统一使用该路径配置maven。各位读者请各自选择地址。
2-2.配置环境变量
2-2-1.maven的环境变量
- 终端输入
$ open .bash_profile
- 将下面的三行环境变量输入,保存并关闭(确保Java环境变量已经配置):
export M2_HOME=/Users/lizhongren1/maven/apache-maven-3.3.9
export M2=$M2_HOME/bin
export PATH=$M2:$PATH
- 终端输入
$ source .bash_profile
, 使修改生效。
2-2-2. 检查是否安装成功
终端输入mvn -v
.显示maven的配置信息则成功。
2-3.修改maven配置
2-3-1.配置本地库
- 在
/Users/lizhongren1/maven/
路径下新建一个LocalWarehouse
文件夹作为本地仓库地址 - 打开
/Users/lizhongren1/maven/apache-maven-3.3.9/conf/settings.xml
,在setting标签中加入一行:
<localRepository>/Users/lizhongren1/maven/LocalWarehouse</localRepository>
2-3-2.配置镜像
- 上面打开的setting.xml文件中,找到下面mirror标签加入阿里的镜像信息:
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
2-4.配置idea的maven
- 打开
preference
,找到Build-Build Tools-Maven
。这里有自带的maven框架 - 将
Maven home directory
的地址改为/Users/lizhongren1/maven/apache-maven-3.3.9
- 将
User setting file
修改为/Users/lizhongren1/maven/apache-maven-3.3.9/conf/settings.xml
- 保存即可
2-5.新建maven工程
maven repo搜索地址:
3.maven的参数和命令
3-1. 创建项目
mvn archetype:create -DgroupId=packageName -DartifactId=projectName
mvn archetype:create -DgroupId=packageName -DartifactId=webappName -DarchetypeArtifactId=maven-archetype-webapp
3-2. 生成项目
mvn idea:idea
mvn eclipse:eclipse
3-3. 编译源代码
mvn compile
mvn test-compile
3-4. 清理
mvn eclipse:clean
mvn clean
4.pom的基本结构
4-1.基本顺序
<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.lzhr</groupId>
<artifactId>BlogManagement</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>BlogManagement Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>4.3.6.RELEASE</spring.version>
<spring-data.version>1.11.0.RELEASE</spring-data.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<finalName>BlogManagement</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
...
</plugins>
</build>
</project>
4-2.pom的标签详解
groupId,artifactId和version三行。这三个元素定义了一个项目基本的坐标,在Maven的世界,任何的jar、pom或者war都是以基于这些基本的坐标进行区分的。
- groupId定义了项目属于哪个组,这个组往往和项目所在的组织或公司存在关联,例如
com.google.myapp
(前面代表公司名,后面myapp代表项目名) - artifactId定义了当前Maven项目在组中唯一的ID,建议使用项目的名称-模块名称例如:myapp-hello
- version指定了项目当前的版本——1.0-SNAPSHOT。SNAPSHOT意为快照,说明该项目还处于开发中,是不稳定的版本.
- type: 依赖的类型,对应于项目坐标定义的packaging。大部分情况下,该元素不必声明,其默认值是jar
- optional: 标记依赖是否可选
- exclusions: 用来排除传递性依赖,下面会进行详解
- scope: 依赖的范围,下面会进行详解
4-2-1.scope
依赖的范围:三种classpath:
- 编译classpath
- 测试classpath
运行classpath
compile: 编译依赖范围。如果没有指定,就会默认使用该依赖范围。使用此依赖范围的Maven依赖,对于
编译、测试、运行
三种classpath都有效。- test: 测试依赖范围。使用此依赖范围的Maven依赖,只对于测试classpath有效,在编译主代码或者运行项目的使用时将无法使用此类依赖。典型的例子就是JUnit,它只有在编译测试代码及运行测试的时候才需要。
- provided: 已提供依赖范围。使用此依赖范围的Maven依赖,
对于编译和测试classpath有效,但在运行时无效
。典型的例子是servlet-api,编译和测试项目的时候需要该依赖,但在运行项目的时候,由于容器已经提供,就不需要Maven重复地引入一遍。 - runtime: 运行时依赖范围。使用此依赖范围的Maven依赖,
对于测试和运行classpath有效,但在编译主代码时无效
。典型的例子是JDBC驱动实现,项目主代码的编译只需要JDK提供的JDBC接口,只有在执行测试或者运行项目的时候才需要实现上述接口的具体JDBC驱动。 - system: 系统依赖范围。该依赖与三种classpath的关系,
和provided依赖范围完全一致
。但是,使用system范围依赖时必须通过systemPath元素显式地指定依赖文件的路径。由于此类依赖不是通过Maven仓库解析的,而且往往与本机系统绑定,可能造成构建的不可移植,因此应该谨慎使用。systemPath元素可以引用环境变量,如:
具体如下:
依赖 | 编译 | 测试 | 运行 |
---|---|---|---|
compile | √ | √ | ⨉ |
test | x | √ | x |
provided | √ | √ | x |
runtime | x | √ | √ |
4-2-2.propreties属性
可以自定义变量, 如下:
<properties>
<testmark>test</testmark>
</properties>
...
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>${testmark}</scope>
</dependency>
4-3.开发中应用较多的Maven配置(每个配置的详细作用还需要后续补充)
<!--1-->
<properties>
<spring.version>4.3.6.RELEASE</spring.version>
<spring-data.version>1.11.0.RELEASE</spring-data.version>
</properties>
<dependencies>
<!--2-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--3-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.2</version>
</dependency>
<!--4-->
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.4</version>
</dependency>
<!--5-->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<!--6-->
<!-- https://mvnrepository.com/artifact/jstl/jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--7-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!--8-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!--9-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<!--10-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--11-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-dao -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-dao</artifactId>
<version>2.0.8</version>
</dependency>
<!--12-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!--13-->
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
<!--14-->
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.6</version>
</dependency>
<!--15-->
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.6</version>
</dependency>
<!--16.Blog-->
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--17.paging-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.0.0</version>
</dependency>
</dependencies>
<build>
<finalName>H-ui</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
未完待续…