1、安装maven
maven下载地址:http://maven.apache.org/download.cgi
Windows:
下载maven,解压;
配置环境变量:
在系统变量中添加maven_home(=解压的路径):
MAVEN_HOME=maven解压路径
在path中添加:%MAVEN_HOME%\bin
Linux:
复制maven的下载地址url,在安装目录下:使用wget url,下载成功后解压;
配置环境变量:修改/etc/profile文件,在末尾增加配置:
export MAVEN_HOME=maven解压路径
export PATH=$MAVEN_HOME/bin:$PATH
添加完成后保存,退出,执行:source profile命令,使环境变量生效。
测试:
输入命令:mvn -v,出现maven的版本和相关信息,出现maven配置成功。
2、配置文件
<?xml version="1.0"?>
<settings>
<!--本地仓库地址-->
<localRepository>E:\study\java\maven\repository</localRepository>
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>nexus</id>
<name>local private nexus</name>
<url>https://maven.oschina.net/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>local private nexus</name>
<url>https://maven.oschina.net/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<!--配置jdk-->
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
</settings>
3、maven常用命令
自动构建命令:
mvn -v 查看maven版本
mvn compile 编译
mvn test 测试
mvn package 打包
mvn clean 删除target
mvn intall 安装jar包到本地仓库中,如果a项目依赖b项目,可以install b项目,然后在a的pom中引入b的依赖。
自动创建目录骨架:
archetype:generate 按提示进行选择
mvn archetype:generate -DgroupId=组织名,公司网址的反写.项目名
-DartifactId=项目名-模块名
-Dversion=版本号
-Dpackage=代码所在的包名
完整的项目构建过程:
清理、编译、测试、打包、集成测试、验证、部署
maven 生命周期:clean 清理项目; default 构建项目; site 生成项目的站点
4、maven的坐标和标签
坐标:
<groupId>公司网址的反写.项目名</groupId>
<artifactId>项目名-模块名</artifactId>
<version>版本号</version>
仓库:本地仓库和远程仓库;镜像仓库
maven标签:
<!--project跟标签 -->
<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/xsd/maven-4.0.0.xsd">
<!-- 当前pom的版本,固定4.0.0 -->
<modelVersion>4.0.0</modelVersion>
<!-- 反写的公司网址+项目名 -->
<groupId>com.szl.test2</groupId>
<!-- 项目名+模块名 -->
<artifactId>test2</artifactId>
<!-- 0.0.1,第一个0表示大版本号,第二个0表示分支版本号,第三个0表示小版本号;snapshot快照,alpha内部测试,beta公测,release稳定,ga正式发布 -->
<version>0.0.1-SNAPSHOT</version>
<!-- 打包类型,默认是jar,还有war,zip,pom -->
<packaging>jar</packaging>
<!-- 项目名称 -->
<name>test2</name>
<!-- 项目地址 -->
<url>http://maven.apache.org</url>
<!-- 项目描述 -->
<description>this is a test project</description>
<!-- 开发者列表 -->
<developers>
<developer>
<name>sunzl</name>
</developer>
</developers>
<!-- 许可证信息 -->
<licenses></licenses>
<!-- 组织信息 -->
<organization></organization>
<!-- 子模块继承父模块 -->
<parent>
<groupId></groupId>
<artifactId></artifactId>
<version></version>
<!-- relativePath是可选的,maven会首先搜索这个地址,在搜索本地远程repositories之前.设置为空,设定一个空值将始终从仓库中获取,不从本地路径获取 -->
<relativePath></relativePath>
</parent>
<!-- 属性信息 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<!-- 依赖列表 -->
<dependencies>
<!-- 依赖项目 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<!-- 作用范围 -->
<scope>test</scope>
<!--设置依赖是否可选,true或者false,默认false继承,true必须显式引入该依赖 -->
<optional></optional>
<!--排除依赖传递列表 -->
<exclusions>
<!--多个排除的依赖 -->
<exclusion></exclusion>
</exclusions>
</dependency>
</dependencies>
<!--依赖的管理,不会引入到实际的依赖中,主要是定义在父模块中,供子模块继承 -->
<dependencyManagement>
<!-- 多个依赖 -->
<dependencies>
</dependencies>
</dependencyManagement>
<!-- 对构建行为提供支持 -->
<build>
<!-- 插件列表 -->
<plugins>
<!-- 多个插件 -->
<plugin>
<groupId></groupId>
<artifactId></artifactId>
<version></version>
</plugin>
</plugins>
</build>
<!-- 聚合运行多个maven项目 -->
<modules>
<!-- 多个模块一起编译 -->
<module></module>
</modules>
</project>
Maven依赖范围:
<scope>:
Compile:默认的范围,编译测试运行都有效
Provided:在编译和测试时有效
Runtime:在测试和运行时有效
Test:只在测试时有效
System:与本机系统相关联,可移植性差,在编译和测试时有效
Import:导入的范围,它只使用在dependencyManagement中,表示从其他的pom中导入dependency的配置
依赖传递:
C依赖B,B依赖A,则C会引入B和A的包,可使用<exclusions>将A排除出去
Maven依赖冲突:
短路优先,靠前优先
Maven 聚合和继承:
聚合:<modules>可使多个项目同时构建
继承:<parent>,将公共的放到父里的<dependencyManagement>,<packaging>使用pom