一、Maven简介
Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建、报告和文档的软件项目管理工具。
apache-maven-3.3.3
|- bin 存放命令
|- boot
|- plexus-classworlds-2.5.2.jar 类加载器的框架
|- conf 配置文件目录
|- lib 类库
二、maven构建命令介绍
mvn -v 查看maven版本
mvn compile 编译
mvn test 测试
mvn package 打包
mvn clean 删除target
mvn install 安装jar包到本地仓库中
三、自动创建目录骨架
archetype插件,用语创建符合maven约定的目录骨架;
创建目录骨架,命令行方式进入项目文件夹后输入:
第一种方式:按照提示进行选择
mvn archetype:generate Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 1007: Choose org.apache.maven.archetypes:maven-archetype-quickstart version: 1: 1.0-alpha-1 2: 1.0-alpha-2 3: 1.0-alpha-3 4: 1.0-alpha-4 5: 1.0 6: 1.1 Choose a number: 6: 6 Define value for property 'groupId': org.study.maven03 Define value for property 'artifactId': maven03-service Define value for property 'version' 1.0-SNAPSHOT: : 1.0.0SNAPSHOT Define value for property 'package' org.study.maven03: : org.study.maven03.service Confirm properties configuration: groupId: org.study.maven03 artifactId: maven03-service version: 1.0.0SNAPSHOT package: org.study.maven03.service Y: : y |
第二种方式:
mvn archetype:generate -DgroupId=org.study.maven04 -DartifactId=maven04-demo -Dversion=1.0.0SNAPSHOT -Dpackage=org.study.maven04.demo |
mvn archetype:generate -DgroupId=org.asset -DartifactId=asset -DarchetypeArtifactId=maven-archetype-webapp |
maven-archetype-quickstart
-DgroupId=组织名,公司网址的反写+项目名
-DartifactId=项目名-模块名
-Dpackage=代码所在的包名
四、maven中坐标和仓库
构件通过坐标唯一标示
仓库分为:
本地仓库:
远程仓库:
超级pom:
$M2_HOME/lib/maven-model-builder-3.5.0.jar/org/apache/maven/model/pom-4.0.0.xml
<repositories> <repository> <id>central</id> <name>Central Repository</name> <url>https://repo.maven.apache.org/maven2</url> <layout>default</layout> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> |
添加镜像仓库:$M2_HOME/conf/setting.xml,在<mirrors>标签中添加:
<mirror> <id>maven.net.cn</id> <mirrorOf>central</mirrorOf> <name>Central mirror in China.</name> <url>http://maven.net.cn/content/groups/public</url> </mirror> |
更改仓库位置:
默认位置为:${user.home}/.m2/repository
本机地址为:/Users/daniel/.m2/repository
$M2_HOME/conf/setting.xml中添加:
<localRepository>/Users/daniel/java/maven/repository</localRepository> |
复制settings.xml文件到新的仓库位置,方便后期更新maven;
进入到maven01项目,执行
mvn clean compile |
maven自动下载所依赖的包,说明仓库修改成功
更新本地仓库
mvn help:system |
五、Eclipse中配置Maven
下载比较新的Eclipse自带Maven插件,无需单独下载;
Preferences - Java - Installed JREs,选择JDK安装目录;
Preferences - Maven - installations,选择本地安装好的Maven;
Preferences - Maven - User Settings,选择更改本地仓库后的settings.xml文件;
六、maven生命周期和插件
完整的项目构建过程包括:
清理、编译、测试、打包、集成测试、验证、部署
maven生命周期:
clean 清理项目
pre-clean 执行清理前的工作
clean 清理上一次构建生成的所有文件
post-clean 执行清理后的文件
default 构建项目(最核心)
compile test package install
site 生成项目站点
pre-site 在生成项目站点前腰完成的工作
site 生成项目的站点文档
post-site 在生成项目站点后要完成的工作
site-deploy 发布生成的站点到服务器
将source绑定到package阶段,在pom.xml中添加:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin> </plugins> </build> |
然后右键pom.xml - Run As - Maven build... --> clean package
七、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/xsd/maven-4.0.0.xsd"> <!-- 当前pom版本 --> <modelVersion>4.0.0</modelVersion> <!-- 反写公司网址+项目名 --> <groupId>org.study.maven05.test</groupId> <!-- 项目名+模块名 --> <artifactId>maven05-test</artifactId> <!-- 第一个0表示大版本号 第二个0表示分支版本号 第三个0表示小版本号 SNAPSHOT快照 alpha内部测试 beta公测 Release稳定版本 GA正式发布 --> <version>0.0.1-SNAPSHOT</version> <!-- 打包类型,默认为jar war zip pom --> <packaging>jar</packaging> <!-- 项目描述名 --> <name>maven05-test</name> <!-- 项目地址 --> <url>http://maven.apache.org</url> <!-- 项目描述 --> <description></description> <!-- 开发人员列表 --> <developers></developers> <!-- 许可信息 --> <licenses></licenses> <!-- 组织信息 --> <organization></organization>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>
<!-- 依赖列表 --> <dependencies> <dependency> <groupId></groupId> <artifactId></artifactId> <version></version> <type></type> <scope>test</scope> <!-- 设置依赖是否可选,默认为false --> <optional></optional> <!-- 排除依赖传递列表 --> <exclusions> <exclusion></exclusion> <exclusion></exclusion> </exclusions> </dependency>
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> </dependencies>
<!-- 依赖管理,用于定义在父类模块中定义,其他直接继承 --> <dependencyManagement> <dependencies> <dependency></dependency> </dependencies> </dependencyManagement>
<build> <!-- 插件列表 --> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <parent></parent> <modules></modules> </project> |
八、maven依赖范围
maven提供三种CLASSPATH:编译、测试、运行;
scope:
|- compile 默认选项,编译、测试、运行均有效;
|- provided 编译、测试时有效;
|- runtime 测试、运行时有效;
|- test 测试有效;
|- system 与本机系统相关联,可移植性差;
|- import 导入的范围,只使用在dependencyManagement中,表示从其他的pom中导入dependency的配置;
九、maven依赖传递
B依赖于A,C依赖于B,所以C传递依赖于A,但是对于C可以只依赖于B,不依赖于A;
<groupId>org.study.maven06</groupId> <artifactId>A</artifactId> <version>0.0.1-SNAPSHOT</version> |
<groupId>org.study.maven06</groupId> <artifactId>B</artifactId> <version>0.0.1-SNAPSHOT</version>
<dependency> <groupId>org.study.maven06</groupId> <artifactId>A</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> |
<groupId>org.study.maven06</groupId> <artifactId>C</artifactId> <version>0.0.1-SNAPSHOT</version>
<dependency> <groupId>org.study.maven06</groupId> <artifactId>B</artifactId> <version>0.0.1-SNAPSHOT</version> <exclusions> <exclusion> <groupId>org.study.maven06</groupId> <artifactId>A</artifactId> </exclusion> </exclusions> </dependency> |
JDK版本全局修改:settings.xml中加入
<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> |
JDK版本局部修改,针对当前项目修改pom.xml文件:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> |
十、maven依赖冲突
1、短路优先,优先选取路径长度短的;
A -> B -> C -> X(jar)
A -> D -> X(jar)
2、先声明先优先
如果路径长度相同,则谁先声明,先解析谁;
十一、maven聚合和继承
聚合:将几个项目由一个项目管理;
新建ABC项目,管理A、B、C三个项目:
<groupId>org.study.maven06</groupId> <artifactId>ABC</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging>
<modules> <module>../A</module> <module>../B</module> <module>../C</module> </modules> |
继承:类似面向对象编程语言一样,定义一个父项目pom依赖junit,其他项目使用parent标签继承;
<groupId>org.study.maven06</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <junit.version>4.10</junit.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> </dependencyManagement> |
<parent> <groupId>org.study.maven06</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent>
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> |
十二、maven构建web项目
新建maven project,选择maven-archetype-webapp 1.0,jsp页面报错,添加servlet依赖:
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <!-- 只在编译和测试时运行 --> <scope>provided</scope> </dependency> </dependencies> |
并把目录结构补全完整;
确保*.class文件输出目录为target/classes或target/test-classes路径下;
将项目转换为web项目并配置:
右键 - properties - Project Facets - 确保Dynamic Web Module被选中;
右键 - properties - Deployment Assembly 将测试相关的删除掉,因为在发布产品时不需要测试类;
添加jetty插件,并配置maven在打包成功后运行jetty服务(maven官方仓库):
<build> <finalName>maven07-demo</finalName> <plugins> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.4.6.v20170531</version> <executions> <execution> <!-- 在打包成功后使用jetty:run来运行jetty服务 --> <phase>package</phase> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> </plugins> </build> |
使用tomcat容器,添加tomcat的插件(从tomcat官网查找)
<build> <finalName>maven07-demo</finalName> <plugins> <plugin> <!-- <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.4.6.v20170531</version> --> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <executions> <execution> <!-- 在打包成功后运行服务 --> <phase>package</phase> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> </plugins> </build> |