文章目录

什么是Maven

优势

  • 约定优于配置
  • 使用简单
  • 支持测试
  • 构建简单
  • CI
  • 插件丰富

下载安装

下载很简单,主要讲一下如何配置环境变量

  • 配置MAVEN_HOME为Maven安装目录
  • Path中添加​​;%MAVEN_HOME%\bin;​

Maven加载settings顺序

maven会加载settings.xml文件,可定义在多处,它的加载优先级如下:

  1. 用户目录下 .m2/settings.xml
  2. maven安装目录conf/settings.xml (里面有很多推荐配置)

新建Maven项目结构

maven详解_生命周期

Maven结构

setting.xml元素

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http:///SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http:///SETTINGS/1.0.0 http:///xsd/settings-1.0.0.xsd">

<localRepository>/path/to/local/repo</localRepository>


<pluginGroups>
<pluginGroup>com.your.plugins</pluginGroup>
</pluginGroups>


<servers>
<server>
<id>deploymentRepo</id>
<username>repouser</username>
<password>repopwd</password>
</server>
</servers>

<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>


<profiles>
<profile>
<id>jdk-1.4</id>

<activation>
<jdk>1.4</jdk>
</activation>

<repositories>
<repository>
<id>jdk14</id>
<name>Repository for JDK 1.4 builds</name>
<url>http://www.myhost.com/maven/jdk14</url>
<layout>default</layout>
<snapshotPolicy>always</snapshotPolicy>
</repository>
</repositories>
</profile>
</profiles>

<activeProfiles>
<activeProfile>alwaysActiveProfile</activeProfile>
<activeProfile>anotherAlwaysActiveProfile</activeProfile>
</activeProfiles>

</settings>

localRepository

定义repository下载的目录

pluginGroups

用于注册自定义插件

servers

配置私服的用户名和密码

mirrors

配置下载maven jar包的镜像

profiles

用于切换开发、测试、部署的环境

​mvn -p dev​​ 启用dev环境

pom.xml元素

新建的默认pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http:///POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http:///POM/4.0.0 http:///xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.learn</groupId>
<artifactId>maven-project</artifactId>
<version>1.0-SNAPSHOT</version>


</project>

modelVersion

指定了当前Maven模型的版本号,对于Maven2和Maven3来说,它只能是4.0.0

groupId

类似package,公司名倒过来

artifactId

groupId下一个功能的命名,或者说是项目名

version

版本号,SNAPSHOT意为快照,说明该项目还在开发中,是不稳定的版本。

结构为:
​​​主版本号.次版本号.增量版本号-<里程碑版本>​

groupId、artifactId、version三个元素生成了一个Maven项目的坐标

packaging

打包类型:jar、war、rar、ear、pom

description

描述,通常用于版权说明

properties

定义一些变量,可以在dependency中引用

dependencyManagement

一般只出现在父pom里面

用于帮助管理chidren的dependencies,统一版本号

dependencyManagement下面所依赖的jar包,不会被children所依赖,但是可以管理children所依赖的版本

dependency

Type

默认jar

Scope

定义在哪个阶段使用,以及会不会打到包(jar包,war包等)里面去

  • compile 编译(默认)
    表示被依赖项目需要参与当前项目的编译,还有后续的测试,运行周期也参与其中,是一个比较强的依赖。打包的时候通常需要包含进去
  • test 测试
    依赖项目仅仅参与测试相关的工作,包括测试代码的编译和执行,不会被打包,例如:junit
  • provided
    打包的时候可以不用包进去,别的设施会提供。事实上该依赖理论上可以参与编译,测试,运行等周期。相当于compile,但是打包阶段做了exclude操作
  • runtime
    表示被依赖项目无需参与项目的编译,不过后期的测试和运行周期需要其参与。与compile相比,跳过了编译而已。例如JDBC驱动,适用运行和测试阶段
  • system
    从参与度来说,和provided相同,不过被依赖项不会从maven仓库下载,而是从本地文件系统拿。需要添加systemPath的属性来定义路径
依赖传递
  • A–>B–>C。当前项目为A,A依赖于B,B依赖于C
  • 当C是test或者provided时,C直接被丢弃,A不依赖C; 否则A依赖C
依赖仲裁
  • 最短路径原则
    A->B->C->common1.1.jar
    A->common1.0.jar
    那么A最终会依赖common1.0.jar
  • 加载先后原则
    A->B
    A->C
    B->common1.0.jar
    C->common1.1.jar
    A同时依赖B和C,那么B和C谁先加载,就依赖谁的common.jar
排除依赖传递

解决jar包冲突,比如spring-core排除commons-loggoing

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>

生命周期

一个完整的项目构建过程通常包括清理、编译、测试、打包、集成测试、验证、部署等步骤,Maven从中抽取了一套完善的、易扩展的生命周期。
Maven的生命周期是抽象的,其中的具体任务都交由插件来完成。Maven为大多数构建任务编写并绑定了默认的插件,如针对编译的插件:maven-compiler-plugin。用户也可自行配置或编写插件。

maven详解_生命周期_02

Maven定义了三套生命周期:clean、default、site,每个生命周期都包含了一些阶段(phase)。三套生命周期相互独立,但各个生命周期中的phase却是有顺序的,
且后面的phase依赖于前面的phase。执行某个phase时,其前面的phase会依顺序执行,但不会触发另外两套生命周期中的任何phase。

比如执行package,前面的test、comiple等 会运行

clean生命周期

  1. pre-clean :执行清理前的工作;
  2. clean :清理上一次构建生成的所有文件(target);
  3. post-clean :执行清理后的工作

default生命周期

default生命周期是最核心的,它包含了构建项目时真正需要执行的所有步骤。

  • process-resources :复制和处理资源文件到target目录,准备打包;
  • compile :编译项目的源代码;
  • test-compile :编译测试源代码;
  • test :运行测试代码;
  • package :打包成jar或者war或者其他格式的分发包;
  • install :将打好的包安装到本地仓库,供其他项目使用;
  • deploy :将打好的包安装到远程仓库,供其他项目使用;

site生命周期

  1. pre-site
  2. site :生成项目的站点文档;
  3. post-site
  4. site-deploy :发布生成的站点文档

常用命令

运行方式 ​​mvn 命令​

compile

编译,会下载相关jar包

执行的插件:maven-compiler-plugin:3.1:compile (default-compile)

clean

删除target

执行的插件: maven-clean-plugin:2.5:clean (default-clean)

test

运行测试代码,比如JUnit写的测试
执行的插件:maven-surefire-plugin:2.18.1:test (default-test)

package

打包
执行的插件: maven-resources-plugin:2.7:resources (default-resources)

install

将打好的包安装到本地仓库(repository),供其他项目使用;
在多模块开发环境下,改了代码要执行下install,不然其他模块看不到最新的修改。

执行的插件:maven-install-plugin:2.5.2:install (default-install)

deploy

把本地jar包发送到远程仓库(私服)。

执行的插件:maven-deploy-plugin:2.8.2:deploy

插件

常用插件

findbugs

静态代码检查

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<!-- <configLocation>${basedir}/springside-findbugs.xml</configLocation> -->
<threshold>High</threshold>
<effort>Default</effort>
<findbugsXmlOutput>true</findbugsXmlOutput>
<!-- findbugs xml输出路径--> <findbugsXmlOutputDirectory>target/site</findbugsXmlOutputDirectory>
</configuration>
</plugin>
</plugins>
</build>

versions

统一升级父子模块版本号

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.3</version>
</plugin>

使用方法

​mvn versions:set -DnewVersion=1.1​

source

提供项目自动将源码打包并发布的功能

<!-- Source attach plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>install</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>

assembly

把java工程打包成为一个可执行的jar包

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.learn.MyApp</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>

执行​​mvn clean compile assembly:single​

tomcat

有时候你不想安装并在ide中配置tomcat,这时候可以使用tomcat插件

<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<path>/</path>
</configuration>
</plugin>

插件的目标

一个插件通常可以完成多个任务,每一个任务就叫做插件的一个目标。如执行mvn install命令时,调用的插件和执行的插件目标如下:

maven详解_xml_03

将插件绑定到生命周期

Maven的生命周期是抽象的,实际需要插件来完成任务,这一过程是通过将插件的目标(goal)绑定到生命周期的具体阶段(phase)来完成的。如:将maven-compiler-plugin插件的compile目标绑定到default生命周期的compile阶段,完成项目的源代码编译:

maven详解_生命周期_04

自定义插件

  1. 新建项目
<groupId>com.learn</groupId>
<artifactId>maven-project</artifactId>
<version>1.0-SNAPSHOT</version>
  1. 修改pom.xml,添加​​<packaging>maven-plugin</packaging>​
  2. 添加dependency
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.5</version>
<scope>provided</scope>
</dependency>
  1. 添加代码
    新建类MyMojo
@Mojo(name="learn",defaultPhase = LifecyclePhase.PACKAGE)
public class MyMojo extends AbstractMojo {
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
System.out.println("start plugin...");
}
}

这里挂载的phase是package

执行​​mvn clean install​

  1. 其他项目中使用
<plugin>
<groupId>com.learn</groupId>
<artifactId>maven-project</artifactId>
<version>1.0-SNAPSHOT</version>
</plugin>

执行​​mvn project:learn​

执行日志如下:

[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building spring-cloud-config-client 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-project:1.0-SNAPSHOT:learn (default-cli) @ spring-cloud-config-client ---
start plugin...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

可以看到我们的插件生效了

但是如果我们直接执行​​mvn install​​​时是看不到这条打印的,说明没有生效。
需要添加executions

<plugin>
<groupId>com.learn</groupId>
<artifactId>maven-project</artifactId>
<version>1.0-SNAPSHOT</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>learn</goal>
</goals>
</execution>
</executions>
</plugin>

然后再执行​​mvn install​​,可以看到:

[INFO] --- maven-project:1.0-SNAPSHOT:learn (default) @ spring-cloud-config-client ---
start plugin...

profile

maven详解_生命周期_05

在项目中定义三个环境

修改xml:

<profiles>
<profile>
<id>dev</id>
<properties>
<profiles.active>dev</profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<profiles.active>test</profiles.active>
</properties>
</profile>
<profile>
<id>pro</id>
<properties>
<profiles.active>pro</profiles.active>
</properties>
</profile>
</profiles>

<build>

<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<excludes>
<exclude>conf/**</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources/conf/${profiles.active}</directory>
</resource>
</resources>
</build>

执行​​mvn clean install -P pro​

maven详解_xml_06

可以看到target中app.properties是pro的

搭建私服

待补充

archetype

模板化脚手架

自定义模板

新建一个普通maven项目,添加一些包和类

①在项目目录下执行 ​​mvn archetype:create-from-project​

②cd 到"target\generated-sources\archetype"

执行​​mvn install​

此时,我们的脚手架已经生成成功了

maven详解_xml_07

根据上图的操作引用它

如果上面的生成工程失败,可以通过​​mvn archetype:generate -DarchetypeCatalog=local​​命令来生成,选择我们的archetype即可

E:\workspace\idea_java>mvn archetype:generate -DarchetypeCatalog=local
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.0.1:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.0.1:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: local -> com.learn:maven-plugin-archetype (maven-plugin-archetype)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 1
Define value for property 'groupId': com.learn
Define value for property 'artifactId': archetype-demo
Define value for property 'version' 1.0-SNAPSHOT: :
Define value for property 'package' com.learn: :
Confirm properties configuration:
groupId: com.learn
artifactId: archetype-demo
version: 1.0-SNAPSHOT
package: com.learn
Y: :
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: maven-plugin-archetype:1.0-SNAPSHOT
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.learn
[INFO] Parameter: artifactId, Value: archetype-demo
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: com.learn
[INFO] Parameter: packageInPathFormat, Value: com/learn
[INFO] Parameter: package, Value: com.learn
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: groupId, Value: com.learn
[INFO] Parameter: artifactId, Value: archetype-demo
[WARNING] Don't override file E:\workspace\idea_java\archetype-demo\.idea\inspectionProfiles\Project_Default.xml
[INFO] Project created from Archetype in dir: E:\workspace\idea_java\archetype-demo
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 28.770 s
[INFO] Finished at: 2018-10-14T13:13:19+08:00
[INFO] Final Memory: 14M/245M
[INFO] ------------------------------------------------------------------------

然后可以看到,生成的项目中有我们脚手架模板里面的类

父子工程

模块