mvn -Dmaven.test.skip=true -U clean install
mvn clean package -U
(强制拉一次)
mvn archetype:create-from-project
mvn archetype:generate -DarchetypeCatalog=local
mvn dependency:tree
mvn clean package -Dmaven.test.skip=true -Ptest
约定优于配置
简单
测试支持
构建简单
CI
插件丰富
https://maven.apache.org/download.cgi
maven-model-builder-3.6.0.jar/org/apache/maven/model
自己的classloader
所有自己的仓库配置
超级父pom
Windows path
Linux .bash_profile
export M2_HOME=/Users/xx/tools/apache/apache-maven-3.6.0
MAVEN_OPTS
配置setting.xml
<mirror>
<id>alimavenid>
<name>aliyun mavenname>
<url>http://maven.aliyun.com/nexus/content/groups/public/url>
<mirrorOf>centralmirrorOf>
mirror>
<mirror>
<id>uiid>
<mirrorOf>centralmirrorOf>
<name>Human Readable Name for this Mirror.name>
<url>http://uk.maven.org/maven2/url>
mirror>
<mirror>
<id>oscid>
<mirrorOf>centralmirrorOf>
<url>http://maven.oschina.net/content/groups/public/url>
mirror>
<mirror>
<id>osc_thirdpartyid>
<mirrorOf>thirdpartymirrorOf>
<url>http://maven.oschina.net/content/repositories/thirdparty/url>
mirror>
一般是域名反写
com.zzjson
功能命名
版本号
打包方式 默认是jar
只能出现在父pom
统一版本号
声明 (子POM里用到再引)
Type 默认jar
默认
编译(maven编译整个项目)
例如spring-core
测试
编译 例如 servlet,tomcat已经有了
打包不会打进去,相当于compile但是在打包阶段做了exclude操作
运行时 例如JDBC驱动实现
本地一些jar 例如短信jar
compile:编译依赖范围,在编译,测试,运行时都需要。
test: 测试依赖范围,测试时需要。编译和运行不需要。如Junit
runtime: 运行时依赖范围,测试和运行时需要。编译不需要。如JDBC驱动包
provided:已提供依赖范围,编译和测试时需要。运行时不需要。如servlet-api
system:系统依赖范围。本地依赖,不在maven中央仓库。
第一列表示直接依赖的scope,第一行表示间接依赖的scope
compile | test | provided | runtime | |
---|---|---|---|---|
compile | compile | - | - | runtime |
test | test | - | - | test |
provided | provided | - | provided | provided |
runtime | runtime | - | - | runtime |
mvn dependency:tree
如果都定义了版本,那么以最近的为准
最短路径原则
加载先后原则
排除包
pre-clean
执行一些清理前需要完成的工作clean
清理上一次构建生成的文件post-clean
执行一些清理后需要完成的工作 lifecycle/phase/goal
1.A Build Lifecycle is Made Up of Phases
一个生命周期由多个phase组成的
2.A Build Phase is Made Up of Plugin Goals
一个phases是由多个goals组成的
生命周期运行会从前往后
![image-20190307174012501](/Users/zzy/Library/Application Support/typora-user-images/image-20190307174012501.png)
1.0-SNAPSHOT
i. repository 删除
ii. mvn clean package -U
(强制拉一次)
主版本号.次版本号.增量版本号-<里程碑版本>
1.0.0-RELAESE
compile
clean 删除target/
test test case junit/testNG
package 打包
<plugins>
<plugin>
<artifactId>pluginartifactId>
<groupId>com.zzjsongroupId>
<version>1.1version>
<configuration>
<msg>
ddd
msg>
<list>
<list>
d
list>
<list>
d1
list>
list>
configuration>
<executions>
<execution>
<phase>packagephase>
<goals>
<goal>myplugingoal>
goals>
execution>
executions>
plugin>
plugins>
install 把项目install到local repo
deploy 发本地jar发布到remote
https://maven.apache.org/plugins/
http://www.mojohaus.org/plugins.html
findbugs 静态代码检查
<plugin>
<groupId>org.codehaus.mojogroupId>
<artifactId>versions-maven-pluginartifactId>
<version>2.3version>
plugin>
http://www.mojohaus.org/versions-maven-plugin/index.html
mvn versions:set -DnewVersion=1.1-SNAPSHOT
<plugin>
<artifactId>maven-source-pluginartifactId>
<version>2.3version>
<executions>
<execution>
<id>attach-sourcesid>
<phase>installphase>
<goals>
<goal>jar-no-forkgoal>
goals>
execution>
executions>
plugin>
<plugin>
<artifactId>maven-assembly-pluginartifactId>
<version>3.1.0version>
<configuration>
<archive>
<manifest>
<mainClass>shuncom.dubbo.task.main.BootStrapmainClass>
manifest>
archive>
<descriptorRefs>jar-with-dependenciesdescriptorRefs>
configuration>
<executions>
<execution>
<id>make-assemblyid>
<phase>packagephase>
<goals>
<goal>singlegoal>
goals>
execution>
executions>
plugin>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.2version>
<configuration>
<port>8080port>
<path>/path>
configuration>
plugin>
maven-plugin
导入依赖
<dependency>
<groupId>org.apache.mavengroupId>
<artifactId>maven-plugin-apiartifactId>
<version>3.5.0version>
dependency>
<dependency>
<groupId>org.apache.maven.plugin-toolsgroupId>
<artifactId>maven-plugin-annotationsartifactId>
<version>3.5version>
dependency>
extends AbstractMojo
mvn install
参数传递
Profile
使用场景 dev/test/pro
setting.xml 家和公司两套
http://books.sonatype.com/nexus-book/reference3/index.html
http://lohost:8081/nexus(自带jetty)
admin/admin123
Type:
依赖jar包的方式
把group作为合集提供,挨个jar去仓库查找
本地的
三方的
阿里短信jar包
released
snapshosts
当maven dependce本地仓库没有找到,私服也没有,则去下载,从远端下载到私服
在pom.xml配置
<distributionManagement>
<repository>
<id>repo-mirrorsid>
<name>center-repositoryname>
<url>http://uk.maven.org/maven2url>
repository>
<snapshotRepository>
<id>repo-mirrorsid>
<name>center-repositoryname>
<url>http://uk.maven.org/maven2url>
snapshotRepository>
distributionManagement>
mvn deploy
也需要验证用户名密码,id需要和pom中一致
配置mirror
Profile
mvn archetype:create-from-project
cd /target/generated-sources/archetype
mvn install
b) 从archetype创建项目
mvn archetype:generate -DarchetypeCatalog=local
配置文件
<profiles>
<profile>
<id>devid>
<properties>
<profiles.active>devprofiles.active>
properties>
<activation>
<activeByDefault>falseactiveByDefault>
activation>
profile>
<profile>
<id>prodid>
<properties>
<profiles.active>prodprofiles.active>
properties>
<activation>
<activeByDefault>trueactiveByDefault>
activation>
profile>
<profile>
<id>testid>
<properties>
<profiles.active>testprofiles.active>
properties>
<activation>
<activeByDefault>falseactiveByDefault>
activation>
profile>
profiles>
<resources>
<resource>
<directory>${basedir}/src/main/resourcesdirectory>
<excludes>
<exclude>conf/**exclude>
excludes>
resource>
<resource>
<directory>src/main/resources/conf/${profiles.active}directory>
resource>
resources>
代码,指定不同的环境
<profile>
<id>aliid>
<repositories>
<repository>
<id>nexusid>
<url>http://maven.aliyun.com/nexus/content/groups/public/url>
<releases>
<enabled>trueenabled>
<updatePolicy>alwaysupdatePolicy>
<checksumPolicy>warnchecksumPolicy>
releases>
<snapshots>
<enabled>trueenabled>
<updatePolicy>alwaysupdatePolicy>
<checksumPolicy>failchecksumPolicy>
snapshots>
repository>
repositories>
<pluginRepositories>
<pluginRepository>
<id>nexusid>
<url>http://maven.aliyun.com/nexus/content/groups/public/url>
<releases>
<enabled>trueenabled>
<updatePolicy>alwaysupdatePolicy>
<checksumPolicy>warnchecksumPolicy>
releases>
<snapshots>
<enabled>trueenabled>
<checksumPolicy>failchecksumPolicy>
<updatePolicy>alwaysupdatePolicy>
snapshots>
pluginRepository>
pluginRepositories>
profile>
<profile>
<id>externalid>
<repositories>
<repository>
<id>repo-mirrorsid>
<url>http://uk.maven.org/maven2url>
repository>
repositories>
<pluginRepositories>
<pluginRepository>
<id>plugin-repo-mirrorid>
<url>http://uk.maven.org/maven2url>
pluginRepository>
pluginRepositories>
profile>
mvn clean package -Dmaven.test.skip=true -Ptest
[root@snails ~]# wget http://download.sonatype.com/nexus/3/latest-unix.tar.gz
dependencyManagement使用能够帮助我们进行子项目的版本号管理
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.zzjson.dubbo.ordergroupId>
<artifactId>order-apiartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
dependencies>
dependencyManagement>
版本会替换仓库中的jar
版本不会替换