优势
- 约定优于配置
- 简单
- 测试支持
- 构建简单
- CI
- 插件丰富
下载
-
安装
-
类加载器机制
超级pom来自于maven-model-builder-3.3.9.jar包的org/apache/maven/model,maven-model-builder-3.3.9.jar/org/apache/maven/model
-
配置 MVM_HOME
1、Windows path
2、.Linux .bash_profile
3、.MAVEN_OPTS
4、配置setting.xml<mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> <mirror> <id>ui</id> <mirrorOf>central</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://uk.maven.org/maven2/</url> </mirror> <mirror> <id>osc</id> <mirrorOf>central</mirrorOf> <url>http://maven.oschina.net/content/groups/public/</url> </mirror> <mirror> <id>osc_thirdparty</id> <mirrorOf>thirdparty</mirrorOf> <url>http://maven.oschina.net/content/repositories/thirdparty/</url> </mirror>
5、MAVEN的setting顺序:
(a)、用户目录/.m2/conf/setting.xml
(b)、解压目录下的/conf/setting.xml
新建一个Maven项目
- 项目结构
- pom.xml
1、groupId com.gupaoedu
2、artfactId 功能命名
3、version 版本号
4、packaging 打包方式 默认是jar
5、dependencyManagement
1、只能出现在父pom
2、统一版本号
3、声明 (子POM里用到再引)
- 6、Dependency
1、Type 默认jar
2、scope
1、compile 编译 例如spring-core
2、test 测试
3、provided编译 例如 servlet
4、runtime运行时 例如JDBC驱动实现
5、system 本地一些jar 例如短信jar
6、依赖传递
mvn dependency:tree > d.txt 看项目依赖包结构
-
第一列表示直接依赖的scope,第一行表示间接依赖的scope
-
存在多个项目时,改其中一个项目的版本号,先使用插件:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>versions-maven-plugin</artifactId> <version>2.3</version> </plugin>
再输入命令:
mvn versions:set -DnewVersion=新版本号名称
-
依赖仲裁
i.最短路径原则 ii.加载先后原则
-
exclusions
1、排除包
-
生命周期 lifecycle/phase/goal
1.A Build Lifecycle is Made Up of Phases 2.A Build Phase is Made Up of Plugin Goals
版本管理
-
1.0-SNAPSHOT
i.repository 删除 ii.mvn clean package -U (强制拉一次)
-
主版本号.次版本号.增量版本号-<里程碑版本>
1.0.0-RELAESE
常用命令
compile 编译
clean 删除target/
test test case junit/testNG
package 打包
install 把项目install到local repo
deploy 发本地jar发布到remote
插件
-
常用插件
apache插件
mojohaus插件1、findbugs 静态代码检查 2、versions 统一升级版本号 mvn versions:set -DnewVersion=1.1 3、source 打包源代码 4、assembly 打包zip、war 5、tomcat7
自定义插件
-
将打包方式改为Maven-pligin
<packaging>maven-plugin</packaging>
-
引入依赖
<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>
-
编写一个类,extends AbstractMojo
-
mvn install
-
参数传递
Profile
- 使用场景dev/test/prod
- setting.xml家和公司两套
Nexus私服仓库
-
下载
-
安装解压
-
i.http://192.168.1.6:8081/nexus ii.admin/admin123
-
pom.xml配置
-
下载jar配置
i.配置mirror ii.Profile
archetype 模版化
-
生成一个archetype
i.mvn archetype:create-from-project ii.cd /target/generated-sources/archetype iii.mvn install
-
从archetype创建项目
mvn archetype:generate -DarchetypeCatalog=local