Maven 是一个项目管理工具,可以对 Java 项目进行构建、依赖管理。
Maven是apache开源的Java编写的开源项目,基于pom(Project Object Model)来构建项目。
总之,Maven 简化了工程的构建过程,并对其标准化。它无缝衔接了编译、发布、文档生成、团队合作和其他任务。
当创建 Maven 工程时,Maven 会创建默认的工程结构。开发者只需要合理的放置文件,而在 pom.xml 中不再需要定义任何配置。
Maven 使用约定优于配置的原则,大家尽可能的遵守这样的目录结构:
配置项 | 默认值 |
---|---|
项目的java源代码 | ${basedir}/src/main/java |
项目的资源,比如说property文件,springmvc.xml | ${basedir}/src/main/resources |
项目的测试类,比如说Junit代码 | ${basedir}/src/test/java |
测试用的资源 | ${basedir}/src/test/resources |
web应用文件目录,web项目的信息,比如存放web.xml、本地图片、jsp视图页面 | ${basedir}/src/main/webapp/WEB-INF |
打包输出目录 | {basedir}/target |
编译输出目录 | ${basedir}/target/classes |
测试编译输出目录 | ${basedir}/target/test-classes |
Maven只会自动运行符合该命名规则的测试类 | Test.java |
Maven默认的本地仓库目录位置 | ~/.m2/repository |
Maven是基于Java实现的,所以要先按照JDK
同时要配置maven的环境变量:
export MAVEN_HOME=/usr/local/apache-maven-3.3.9
export PATH=${
PATH}:${MAVEN_HOME}/bin
1、去官网下载:http://maven.apache.org/download.cgi
2、放到本地解压
3、配置环境变量
POM( Project Object Model,项目对象模型 ) 是 Maven 工程的基本xml配置,用于描述项目如何构建,声明项目依赖,等等。
POM 中可以指定以下配置:
<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">
<modelVersion>4.0.0modelVersion>
<groupId>com.companyname.project-groupgroupId>
<artifactId>projectartifactId>
<version>1.0version>
project>
所有 POM 文件都需要 project 元素和三个必需字段:groupId,artifactId,version。
1)groupId: 公司或者组织的唯一标志,也是配置时生成的路径地址
2)artifactId:项目的唯一ID,一个groupId下面可能多个项目,就是靠artifactId来区分的
3)version:版本号
父(Super)POM是 Maven 默认的 POM。所有的 POM 都继承自一个父 POM(无论是否显式定义了这个父 POM)。
父 POM 包含了一些可以被继承的默认设置。因此,当 Maven 发现需要下载 POM 中的 依赖时,它会到 Super POM 中配置的默认仓库 http://repo1.maven.org/maven2 去下载。
Maven 使用 effective pom(Super pom 加上工程自己的配置)来执行相关的目标,它帮助开发者在 pom.xml 中做尽可能少的配置,当然这些配置可以被重写。
示例:先创建目录 MVN/project/,在该目录下创建 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">
<modelVersion>4.0.0modelVersion>
<groupId>com.companyname.project-groupgroupId>
<artifactId>projectartifactId>
<version>1.0version>
project>
然后执行:
mvn help:effective-pom
执行结果:
servers>\n\n <mirror>... @19:11) @ /Users/shipeng/.m2/settings.xml, line 19, column 11
[WARNING]
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building project 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-help-plugin:2.2:effective-pom (default-cli) @ project ---
Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-container-default/1.0-alpha-9/plexus-container-default-1.0-alpha-9.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-container-default/1.0-alpha-9/plexus-container-default-1.0-alpha-9.jar (195 kB at 129 kB/s)
[INFO]
Effective POMs, after inheritance, interpolation, and profiles are applied:
<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">
<modelVersion>4.0.0modelVersion>
<groupId>com.companyname.project-groupgroupId>
<artifactId>projectartifactId>
<version>1.0version>
<repositories>
<repository>
<snapshots>
<enabled>falseenabled>
snapshots>
<id>centralid>
<name>Central Repositoryname>
<url>https://repo.maven.apache.org/maven2url>
repository>
repositories>
<pluginRepositories>
<pluginRepository>
<releases>
<updatePolicy>neverupdatePolicy>
releases>
<snapshots>
<enabled>falseenabled>
snapshots>
<id>centralid>
<name>Central Repositoryname>
<url>https://repo.maven.apache.org/maven2url>
pluginRepository>
pluginRepositories>
<build>
<sourceDirectory>/Users/shipeng/MVN/project/src/main/javasourceDirectory>
<scriptSourceDirectory>/Users/shipeng/MVN/project/src/main/scriptsscriptSourceDirectory>
<testSourceDirectory>/Users/shipeng/MVN/project/src/test/javatestSourceDirectory>
<outputDirectory>/Users/shipeng/MVN/project/target/classesoutputDirectory>
<testOutputDirectory>/Users/shipeng/MVN/project/target/test-classestestOutputDirectory>
<resources>
<resource>
<directory>/Users/shipeng/MVN/project/src/main/resourcesdirectory>
resource>
resources>
<testResources>
<testResource>
<directory>/Users/shipeng/MVN/project/src/test/resourcesdirectory>
testResource>
testResources>
<directory>/Users/shipeng/MVN/project/targetdirectory>
<finalName>project-1.0finalName>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-antrun-pluginartifactId>
<version>1.3version>
plugin>
<plugin>
<artifactId>maven-assembly-pluginartifactId>
<version>2.2-beta-5version>
plugin>
<plugin>
<artifactId>maven-dependency-pluginartifactId>
<version>2.8version>
plugin>
<plugin>
<artifactId>maven-release-pluginartifactId>
<version>2.3.2version>
plugin>
plugins>
pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-pluginartifactId>
<version>2.5version>
<executions>
<execution>
<id>default-cleanid>
<phase>cleanphase>
<goals>
<goal>cleangoal>
goals>
execution>
executions>
plugin>
<plugin>
<artifactId>maven-resources-pluginartifactId>
<version>2.6version>
<executions>
<execution>
<id>default-testResourcesid>
<phase>process-test-resourcesphase>
<goals>
<goal>testResourcesgoal>
goals>
execution>
<execution>
<id>default-resourcesid>
<phase>process-resourcesphase>
<goals>
<goal>resourcesgoal>
goals>
execution>
executions>
plugin>
<plugin>
<artifactId>maven-jar-pluginartifactId>
<version>2.4version>
<executions>
<execution>
<id>default-jarid>
<phase>packagephase>
<goals>
<goal>jargoal>
goals>
execution>
executions>
plugin>
<plugin>
<artifactId>maven-compiler-pluginartifactId>
<version>3.1version>
<executions>
<execution>
<id>default-compileid>
<phase>compilephase>
<goals>
<goal>compilegoal>
goals>
execution>
<execution>
<id>default-testCompileid>
<phase>test-compilephase>
<goals>
<goal>testCompilegoal>
goals>
execution>
executions>
plugin>
<plugin>
<artifactId>maven-surefire-pluginartifactId>
<version>2.12.4version>
<executions>
<execution>
<id>default-testid>
<phase>testphase>
<goals>
<goal>testgoal>
goals>
execution>
executions>
plugin>
<plugin>
<artifactId>maven-install-pluginartifactId>
<version>2.4version>
<executions>
<execution>
<id>default-installid>
<phase>installphase>
<goals>
<goal>installgoal>
goals>
execution>
executions>
plugin>
<plugin>
<artifactId>maven-deploy-pluginartifactId>
<version>2.7version>
<executions>
<execution>
<id>default-deployid>
<phase>deployphase>
<goals>
<goal>deploygoal>
goals>
execution>
executions>
plugin>
<plugin>
<artifactId>maven-site-pluginartifactId>
<version>3.3version>
<executions>
<execution>
<id>default-siteid>
<phase>sitephase>
<goals>
<goal>sitegoal>
goals>
<configuration>
<outputDirectory>/Users/shipeng/MVN/project/target/siteoutputDirectory>
<reportPlugins>
<reportPlugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-project-info-reports-pluginartifactId>
reportPlugin>
reportPlugins>
configuration>
execution>
<execution>
<id>default-deployid>
<phase>site-deployphase>
<goals>
<goal>deploygoal>
goals>
<configuration>
<outputDirectory>/Users/shipeng/MVN/project/target/siteoutputDirectory>
<reportPlugins>
<reportPlugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-project-info-reports-pluginartifactId>
reportPlugin>
reportPlugins>
configuration>
execution>
executions>
<configuration>
<outputDirectory>/Users/shipeng/MVN/project/target/siteoutputDirectory>
<reportPlugins>
<reportPlugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-project-info-reports-pluginartifactId>
reportPlugin>
reportPlugins>
configuration>
plugin>
plugins>
build>
<reporting>
<outputDirectory>/Users/shipeng/MVN/project/target/siteoutputDirectory>
reporting>
project>
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.531 s
[INFO] Finished at: 2021-10-03T11:19:07+08:00
[INFO] Final Memory: 16M/302M
[INFO] ------------------------------------------------------------------------
从上面的 pom.xml 中,可以看到 Maven 在执行目标时需要用到的默认工程源码目录结构、输出目录、需要的插件、仓库和报表目录。
Maven 的 pom.xml 文件也不需要手工编写。Maven 提供了大量的原型插件来创建工程,包括工程结构和 pom.xml。看下面的POM标签大全:
<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.0http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId />
<groupId />
<version />
<relativePath />
parent>
<modelVersion>4.0.0modelVersion>
<groupId>asia.banseongroupId>
<artifactId>banseon-maven2artifactId>
<packaging>jarpackaging>
<version>1.0-SNAPSHOTversion>
<name>banseon-mavenname>
<url>http://www.baidu.com/banseonurl>
<description>A maven project to study maven.description>
<prerequisites>
<maven />
prerequisites>
<issueManagement>
<system>jirasystem>
<url>http://jira.baidu.com/banseonurl>
issueManagement>
<ciManagement>
<system />
<url />
<notifiers>
<notifier>
<type />
<sendOnError />
<sendOnFailure />
<sendOnSuccess />
<sendOnWarning />
<address />
<configuration />
notifier>
notifiers>
ciManagement>
<inceptionYear />
<mailingLists>
<mailingList>
<name>Demoname>
<post>[email protected]post>
<subscribe>[email protected]subscribe>
<unsubscribe>[email protected]unsubscribe>
<archive>http:/hi.baidu.com/banseon/demo/dev/archive>
mailingList>
mailingLists>
<developers>
<developer>
<id>HELLO WORLDid>
<name>banseonname>
<email>[email protected]email>
<url />
<roles>
<role>Project Managerrole>
<role>Architectrole>
roles>
<organization>demoorganization>
<organizationUrl>http://hi.baidu.com/banseonorganizationUrl>
<properties>
<dept>Nodept>
properties>
<timezone>-5timezone>
developer>
developers>
<contributors>
<contributor>
<name />
<email />
<url />
<organization />
<organizationUrl />
<roles />
<timezone />
<properties />
contributor>
contributors>
<licenses>
<license>
<name>Apache 2name>
<url>http://www.baidu.com/banseon/LICENSE-2.0.txturl>
<distribution>repodistribution>
<comments>A business-friendly OSS licensecomments>
license>
licenses>
<scm>
<connection>
scm:svn:http://svn.baidu.com/banseon/maven/banseon/banseon-maven2-trunk(dao-trunk)
connection>
<developerConnection>
scm:svn:http://svn.baidu.com/banseon/maven/banseon/dao-trunk
developerConnection>
<tag />
<url>http://svn.baidu.com/banseonurl>
scm>
<organization>
<name>demoname>
<url>http://www.baidu.com/banseonurl>
organization>
<build>
<sourceDirectory />
<scriptSourceDirectory />
<testSourceDirectory />
<outputDirectory />
<testOutputDirectory />
<extensions>
<extension>
<groupId />
<artifactId />
<version />
extension>
extensions>
<defaultGoal />
<resources>
<resource>
<targetPath />
<filtering />
<directory />
<includes />
<excludes />
resource>
resources>
<testResources>
<testResource>
<targetPath />
<filtering />
<directory />
<includes />
<excludes />
testResource>
testResources>
<directory />
<finalName />
<filters />
<pluginManagement>
<plugins>
<plugin>
<groupId />
<artifactId />
<version />
<extensions />
<executions>
<execution>
<id />
<phase />
<goals />
<inherited />
<configuration />
execution>
executions>
<dependencies>
<dependency>
......
dependency>
dependencies>
<inherited />
<configuration />
plugin>
plugins>
pluginManagement>
<plugins>
<plugin>
<groupId />
<artifactId />
<version />
<extensions />
<executions>
<execution>
<id />
<phase />
<goals />
<inherited />
<configuration />
execution>
executions>
<dependencies>
<dependency>
......
dependency>
dependencies>
<goals />
<inherited />
<configuration />
plugin>
plugins>
build>
<profiles>
<profile>
<id />
<activation>
<activeByDefault />
<jdk />
<os>
<name>Windows XPname>
<family>Windowsfamily>
<arch>x86arch>
<version>5.1.2600version>
os>
<property>
<name>mavenVersionname>
<value>2.0.3value>
property>
<file>
<exists>/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/
exists>
<missing>/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/
missing>
file>
activation>
<build>
<defaultGoal />
<resources>
<resource>
<targetPath />
<filtering />
<directory />
<includes />
<excludes />
resource>
resources>
<testResources>
<testResource>
<targetPath />
<filtering />
<directory />
<includes />
<excludes />
testResource>
testResources>
<directory />
<finalName />
<filters />
<pluginManagement>
<plugins>
<plugin>
<groupId />
<artifactId />
<version />
<extensions />
<executions>
<execution>
<id />
<phase />
<goals />
<inherited />
<configuration />
execution>
executions>
<dependencies>
<dependency>
......
dependency>
dependencies>
<goals />
<inherited />
<configuration />
plugin>
plugins>
pluginManagement>
<plugins>
<plugin>
<groupId />
<artifactId />
<version />
<extensions />
<executions>
<execution>
<id />
<phase />
<goals />
<inherited />
<configuration />
execution>
executions>
<dependencies>
<dependency>
......
dependency>
dependencies>
<goals />
<inherited />
<configuration />
plugin>
plugins>
build>
<modules />
<repositories>
<repository>
<releases>
<enabled />
<updatePolicy />
<checksumPolicy />
releases>
<snapshots>
<enabled />
<updatePolicy />
<checksumPolicy />
snapshots>
<id />
<name />
<url />
<layout />
repository>
repositories>
<pluginRepositories>
<pluginRepository>
<releases>
<enabled />
<updatePolicy />
<checksumPolicy />
releases>
<snapshots>
<enabled />
<updatePolicy />
<checksumPolicy />
snapshots>
<id />
<name />
<url />
<layout />
pluginRepository>
pluginRepositories>
<dependencies>
<dependency>
......
dependency>
dependencies>
<reports />
<reporting>
......
reporting>
<dependencyManagement>
<dependencies>
<dependency>
......
dependency>
dependencies>
dependencyManagement>
<distributionManagement>
......
distributionManagement>
<properties />
profile>
profiles>
<modules />
<repositories>
<repository>
<releases>
<enabled />
<updatePolicy />
<checksumPolicy />
releases>
<snapshots>
<enabled />
<updatePolicy />
<checksumPolicy />
snapshots>
<id>banseon-repository-proxyid>
<name>banseon-repository-proxyname>
<url>http://192.168.1.169:9999/repository/url>
<layout>defaultlayout>
repository>
repositories>
<pluginRepositories>
<pluginRepository>
......
pluginRepository>
pluginRepositories>
<dependencies>
<dependency>
<groupId>org.apache.mavengroupId>
<artifactId>maven-artifactartifactId>
<version>3.8.1version>
<type>jartype>
<classifier>classifier>
<scope>testscope>
<systemPath>systemPath>
<exclusions>
<exclusion>
<artifactId>spring-coreartifactId>
<groupId>org.springframeworkgroupId>
exclusion>
exclusions>
<optional>trueoptional>
dependency>
dependencies>
<reports>reports>
<reporting>
<excludeDefaults />
<outputDirectory />
<plugins>
<plugin>
<groupId />
<artifactId />
<version />
<inherited />
<configuration />
<reportSets>
<reportSet>
<id />
<configuration />
<inherited />
<reports />
reportSet>
reportSets>
plugin>
plugins>
reporting>
<dependencyManagement>
<dependencies>
<dependency>
......
dependency>
dependencies>
dependencyManagement>
<distributionManagement>
<repository>
<uniqueVersion />
<id>banseon-maven2id>
<name>banseon maven2name>
<url>file://${basedir}/target/deployurl>
<layout />
repository>
<snapshotRepository>
<uniqueVersion />
<id>banseon-maven2id>
<name>Banseon-maven2 Snapshot Repositoryname>
<url>scp://svn.baidu.com/banseon:/usr/local/maven-snapshoturl>
<layout />
snapshotRepository>
<site>
<id>banseon-siteid>
<name>business api websitename>
<url>
scp://svn.baidu.com/banseon:/var/www/localhost/banseon-web
url>
site>
<downloadUrl />
<relocation>
<groupId />
<artifactId />
<version />
<message />
relocation>
<status />
distributionManagement>
<properties />
project>
Maven 构建生命周期定义了一个项目构建和发布的过程。
Maven 构建(build)生命周期是由以下几个阶段的序列组成的:
阶段 | 处理 | 描述 |
---|---|---|
验证validate | 验证项目 | 验证项目是否正确且所有必须信息是可用的 |
编译 compile | 执行编译 | 源代码编译在此阶段完成 |
测试Test | 测试 | 使用适当的单元测试框架(例如JUnit)运行测试。 |
包装 package | 打包 | 创建JAR/WAR包如在 pom.xml 中定义提及的包 |
检查verify | 检查 | 对集成测试的结果进行检查,以保证质量达标 |
安装 install | 安装 | 安装打包的项目到本地仓库,以供其他项目使用 |
部署 deploy | 部署 | 拷贝最终的工程包到远程仓库中,以共享给其他开发人员和工程 |
为了完成 default 生命周期,这些阶段(包括其他未在上面罗列的生命周期阶段)将被按顺序地执行。
Maven 有以下三个标准的生命周期:
构建阶段由插件目标构成。
一个插件目标代表一个特定的任务
以下面命令为例:
mvn clean dependency:copy-dependencies package
clean 和 pakage 是构建阶段,dependency:copy-dependencies 是目标
这里的 clean 阶段将会被首先执行,然后 dependency:copy-dependencies 目标会被执行,最终 package 阶段被执行。
当执行 mvn post-clean 命令时,Maven 调用 clean 生命周期,它包含以下阶段:
mvn clean 中的 clean 就是上面的 clean,在一个生命周期中,运行某个阶段的时候,它之前的所有阶段都会被运行,也就是说,如果执行 mvn clean 将运行以下两个生命周期阶段:
pre-clean, clean
如果我们运行 mvn post-clean ,则运行以下三个生命周期阶段:
pre-clean, clean, post-clean
这是 Maven 的主要生命周期,被用于构建应用,包括下面的 23 个阶段:
生命周期阶段 | 描述 |
---|---|
validate(校验) | 校验项目是否正确并且所有必要的信息可以完成项目的构建过程。 |
initialize(初始化) | 初始化构建状态,比如设置属性值。 |
generate-sources(生成源代码) | 生成包含在编译阶段中的任何源代码。 |
process-sources(处理源代码) | 处理源代码,比如说,过滤任意值。 |
generate-resources(生成资源文件) | 生成将会包含在项目包中的资源文件。 |
process-resources (处理资源文件) | 复制和处理资源到目标目录,为打包阶段最好准备。 |
compile(编译) | 编译项目的源代码。 |
process-classes(处理类文件) | 处理编译生成的文件,比如说对Java class文件做字节码改善优化。 |
generate-test-sources(生成测试源代码) | 生成包含在编译阶段中的任何测试源代码。 |
process-test-sources(处理测试源代码) | 处理测试源代码,比如说,过滤任意值。 |
generate-test-resources(生成测试资源文件) | 为测试创建资源文件。 |
process-test-resources(处理测试资源文件) | 复制和处理测试资源到目标目录。 |
test-compile(编译测试源码) | 编译测试源代码到测试目标目录. |
process-test-classes(处理测试类文件) | 处理测试源码编译生成的文件。 |
test(测试) | 使用合适的单元测试框架运行测试(Juint是其中之一)。 |
prepare-package(准备打包) | 在实际打包之前,执行任何的必要的操作为打包做准备。 |
package(打包) | 将编译后的代码打包成可分发格式的文件,比如JAR、WAR或者EAR文件。 |
pre-integration-test(集成测试前) | 在执行集成测试前进行必要的动作。比如说,搭建需要的环境。 |
integration-test(集成测试) | 处理和部署项目到可以运行集成测试环境中。 |
post-integration-test(集成测试后) | 在执行集成测试完成后进行必要的动作。比如说,清理集成测试环境。 |
verify (验证) | 运行任意的检查来验证项目包有效且达到质量标准。 |
install(安装) | 安装项目包到本地仓库,这样项目包可以用作其他本地项目的依赖。 |
deploy(部署) | 将最终的项目包复制到远程仓库中与其他开发者和项目共享。 |