官网:http://maven.apache.org/index.html
中央仓库:https://repo.maven.apache.org/maven2
插件列表:http://maven.apache.org/plugins/index.html
仓库搜索服务:https://repository.sonatype.org、http://mvnrepository.com/
历史:美国人Jason Van Zyl,2003年在Apache基金会,创始Maven
POM:Project Object Model,项目对象模型
其他构建工具
Make:美国人Stuart Feldman 于1977年在贝尔实验室创立,配置文件为 Makefile
Ant:Another Neat Tool,美国人James Duncan Davidson 于2000年创始了Ant,配置文件为build.xml
Gradle:美国Gradle公司于2012年发布,采用Apache Groovy作为构建语言
Maven 与 Ant 对比
1、Ant 的配置文件 build.xml 是指令式(过程式)的,即相当于用xml编程;Maven的配置文件pom.xml是声明式的,即设置一些参数。
2、Maven通过默认的约定,以减少配置,例如默认的源 和 目标的位置
3、Maven提供一个中央仓库
4、Ant 是 Maven的一个核心插件,Maven自定义插件可以用Ant来实现
Windows 安装
前提:JDK已经安装配置好,必须配置JAVA_HOME,修改环境变量后要重启IDE
1、官网上下载二进制包,解压
2、把bin目录配置进 环境变量 PATH
3、检测 mvn -v
4、添加环境变量 MAVEN_OPTS = -Xms256m -Xmx512m,意指JVM初始内存 和 最大内存
5、将 /conf/settings.xml 复制到 ~/.m2/settings.xml,作为本用户的配置
Linux安装
1、官网上下载二进制包tar.gz,上传到/usr,解压tar -zxvf
2、 在 /etc/profile 追加
export MAVEN_HOME=/usr/apache-maven-3.5.4
export PATH=$PATH:${MAVEN_HOME}/bin
立即生效:source /etc/profile
3、测试:mvn -v;echo $PATH
全员全局配置文件:/conf/settings.xml
本用户全局配置文件:~/.m2/settings.xml
本地仓库(存放从远程仓库下载的依赖):~.m2\repository
Maven 约定
pom.xml所在目录:项目根目录
主代码目录:src/main/java
配置文件目录:src/main/java
web资源目录:src/main/webapp
测试代码目录:src/test/java
输出目录:target
传递依赖:本项目依赖A,A依赖B,那么本项目传递依赖B
术语
goal:maven插件提供的指令
resource:配置文件
filter:将配置文件中的${}替换为属性值
profile:构建场景
IDEA 开发 Maven 项目
1、Settings -> Maven -> 设置好Maven安装目录、本地仓库目录、本用户全局配置文件
2、Settings -> java compiler -> 选好 bytecode version
3、New -> Project -> Maven -> 1、选JDK;2、勾选 Create from archetype,选项目模板maven-archetype-webapp
4、项目右键 -> Open Module Settings -> 选好language level
5、设置自动下载依赖,Settings -> Maven -> Importing -> 勾选 Import automatically
6、提交代码时不要提交.idea,打开已有项目得用 import 而不是 open
项目配置文件 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project> <!-- 根元素 -->
<modelVersion>4.0.0</modelVersion> <!-- pom规范的版本 -->
<groupId>com.hogen</groupId> <!-- 项目坐标之项目ID, 与包结构一致最佳 -->
<artifactId>hogen</artifactId> <!-- 项目坐标之模块ID,与项目名一致最佳 -->
<version>1.0-SNAPSHOT</version> <!-- 版本号,SNAPSHOT指非稳定版本 -->
<name>Maven Webapp</name> <!-- 项目名 -->
<url>http://www.example.com</url> <!-- 项目网站 -->
<packaging>war</packaging> <!-- 打包目标,默认为jar。war包含jar、web资源、jsp等 -->
<repositories> <!-- 远程仓库列表 -->
<repository>
<id>Aliyun</id> <!-- id为central,则会覆盖中央仓库配置,否则不会 -->
<name>Maven Repository Switchboard</name>
<mirrorOf>central</mirrorOf> <!-- 是谁的镜像 -->
<url>http://repo1.maven.org/maven2</url>
<snapshots>
<enabled>false</enabled> <!-- 不下载snapshots版本 -->
</snapshots>
</repository>
</repositories>
<properties> <!-- 自定义属性列表,可以是一些配置项 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies> <!-- 依赖列表 -->
<dependency> <!-- 依赖项 -->
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope> <!-- 作用范围,test:只能被测试代码依赖,默认compile:主代码和测试代码都能依赖 -->
<exclusions> <!-- 排除传递依赖列表,不希望通过junit引入的传递依赖 -->
<exclusion> <!-- 排除传递依赖项 -->
<groupId>${project.build.sourceEncoding}</groupId> <!-- 使用自定义属性 -->
<artifactId>xx</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build> <!-- 构建相关 -->
<defaultGoal>install</defaultGoal> <!-- mvn不指定指令时的默认指令 -->
<directory>${basedir}/target</directory> <!-- 构建目标目录 -->
<finalName>${artifactId}-${version}</finalName> <!-- 目标文件名 -->
<filters>
<filter>filters/filter.properties</filter> <!-- filter文件中定义键值对key=value,用于替换resource中配置文件里的${name},默认filter文件夹为${basedir}/src/main/filters -->
</filters>
<resources> <!-- 配置文件转移规则 -->
<resource> <!-- 配置文件转移规则项 -->
<directory>${basedir}/src/main/plexus</directory> <!-- 源目录 -->
<targetPath>META-INF/plexus</targetPath> <!-- 目标目录 -->
<filtering>true</filtering> <!-- 是否替换变量 -->
<includes>
<include>configuration.xml</include>
</includes>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
</resource>
</resources>
<pluginManagement> <!-- 有这一层,则这些plugins能被继承 -->
<plugins> <!-- 插件列表 -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<webResources> <!-- 打入web资源 -->
<resource> <!-- 可以有多个,最佳是一个filtering=true,一个filtering=false -->
<filtering>true</filtering> <!-- 是否替换${} -->
<directory>web</directory> <!-- 打包目录 -->
<targetPath>web</targetPath> <!-- 目标目录 -->
<includes>
<include>/**</include>
</includes>
<excludes> <!-- 从includes里排除 -->
<exclude>README</exclude> <!-- 排除文件 -->
<exclude>.gitignore</exclude>
<exclude>node_modules/**</exclude> <!-- 排除目录 -->
</excludes>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<profiles>
<profile> <!-- 特定profile下生效的配置 -->
<id>linux</id>
<properties>
<db.driver>xxx</db.driver>
</properties>
<build>…</build>
</profile>
</profiles>
</project>
安装插件
1、在官网上找到所需的插件
2、在pom.xml中配置该插件
3、执行该插件相关指令,就会先下载该插件,再执行指令
Maven命令
清空target目录:mvn clean
编译到target/classes:mvn compile,连写 mvn clean compile
打包到target:mvn package,连写 mvn clean package
安装到本地仓库:mvn install,连写 mvn clean install,供本地的其他项目使用
查看依赖列表:mvn dependency:list,dependency是插件,list是该插件的指令
mvn dependency:tree,树形显示依赖关系
全局配置文件 settings.xml
<?xml version="1.0" encoding="UTF-8"?>
<settings>
<localRepository>本地仓库目录</localRepository> <!-- 默认为~\.m2\repository -->
<pluginGroups> </pluginGroups>
<proxies> </proxies>
<servers>远程仓库需要用户名密码才能访问,那么需要配置该项</servers>
<mirrors>镜像仓库配置,pom.xml里的repositories用于追加,这里用于替代</mirrors>
<profiles>构建场景配置</profiles>
</settings>
pom.xml 和 resource 中能使用的属性
1、内置属性:项目根目录 basedir、项目版本 version(等价于project.version)
2、pom属性:project.groupId、project.artifectId、project.version等
3、自定义属性
4、Settings属性:例如 settings.localRepository
5、环境变量属性:env.JAVA_HOME
确定profile(激活)
1、默认激活
<profile>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
2、属性判断激活
<profile>
<activation>
<property>
<name>xx</name>
<value>yy</value> <!-- 属性xx的值为yy时,激活 -->
</property>
</activation>
</profile>
3、操作系统判断激活
<profile>
<activation>
<os>
<family>Windows</family>
</os>
</activation>
</profile>
4、settings.xml中确定
<activeProfiles>
<activeProfile>linux</activeProfile>
</activeProfiles>
5、构建时确定:mvn -plinux,这里的linux是profile的id
Maven生命周期
clean -> generate-resources -> compile -> generate-test-resouces -> test-compile -> test -> package -> install -> deploy
pom 聚合
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.hogen</groupId>
<artifactId>hogen</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Maven Webapp</name>
<packaging>pom</packaging>
<modules>
<module>./project1</module>
<module>./project2</module>
</modules>
</project>
pom 继承
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.hogen</groupId>
<artifactId>hogen</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../project1/pom.xml</relativePath>
</parent>
<artifactId>hogen</artifactId>
<name>Maven Webapp</name>
</project>












网友评论