Maven
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
认识Maven
优势
- 约定优于配置
- 简单
- 测试支持
- 构建简单
- CI
- 插件丰富
下载
https://maven.apache.org/down…
安装
maven-model-builder-3.6.0.jar/org/apache/maven/model
文件分析
自己的classloader
所有自己的仓库配置
超级父pom
配置 MVM_HOME
- Windows path
Linux .bash_profile
export M2_HOME=/Users/xx/tools/apache/apache-maven-3.6.0
- MAVEN_OPTS
配置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>
Pom
groupId
一般是域名反写
com.zzjson
artfactId
功能命名
version
版本号
packaging
打包方式 默认是jar
dependencyManagement
只能出现在父pom
统一版本号
声明 (子POM里用到再引)
Dependency
Type 默认jar
scope
compile
默认
编译(maven编译整个项目)
例如spring-core
test
测试
provided
编译 例如 servlet,tomcat已经有了
打包不会打进去,相当于compile但是在打包阶段做了exclude操作
runtime
运行时 例如JDBC驱动实现
system
本地一些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
如果都定义了版本,那么以最近的为准
最短路径原则
加载先后原则
exclusions
排除包
生命周期
clean 清理项目
-
pre-clean
执行一些清理前需要完成的工作 -
clean
清理上一次构建生成的文件 -
post-clean
执行一些清理后需要完成的工作
-
- default
- Site
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组成的
生命周期运行会从前往后
版本管理
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>plugin</artifactId> <groupId>com.zzjson</groupId> <version>1.1</version> <configuration> <msg> ddd </msg> <list> <list> d </list> <list> d1 </list> </list> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>myplugin</goal> </goals> </execution> </executions> </plugin> </plugins>
- install 把项目install到local repo
- deploy 发本地jar发布到remote
插件
常用插件
- https://maven.apache.org/plug…
- http://www.mojohaus.org/plugi…
- findbugs 静态代码检查
versions 统一升级版本号
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>versions-maven-plugin</artifactId> <version>2.3</version> </plugin>
http://www.mojohaus.org/versions-maven-plugin/index.html
mvn versions:set -DnewVersion=1.1-SNAPSHOT
source 打包源代码
<plugin> <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 打包zip、war
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<mainClass>shuncom.dubbo.task.main.BootStrap</mainClass>
</manifest>
</archive>
<descriptorRefs>jar-with-dependencies</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
tomcat7
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<path>/</path>
</configuration>
</plugin>
自定义插件
https://maven.apache.org/guides/plugin/guide-java-plugin-development.html
<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>
</dependency>
extends AbstractMojo
mvn install
参数传递
Profile
使用场景 dev/test/pro
setting.xml 家和公司两套
仓库
下载
http://books.sonatype.com/nex…
访问地址
http://lohost:8081/nexus(自带jetty)
admin/admin123
内容配置
Type:
依赖jar包的方式
group
把group作为合集提供,挨个jar去仓库查找
hosted
本地的
三方的
阿里短信jar包
released
snapshosts
proxy
当maven dependce本地仓库没有找到,私服也没有,则去下载,从远端下载到私服
发布
在pom.xml配置
<distributionManagement>
<repository>
<id>repo-mirrors</id>
<name>center-repository</name>
<url>http://uk.maven.org/maven2</url>
</repository>
<snapshotRepository>
<id>repo-mirrors</id>
<name>center-repository</name>
<url>http://uk.maven.org/maven2</url>
</snapshotRepository>
</distributionManagement>
mvn deploy
登录
也需要验证用户名密码,id需要和pom中一致
下载jar配置
- 配置mirror
- Profile
archetype 模版化
生成一个archetype
mvn archetype:create-from-project
进入目录
cd /target/generated-sources/archetype
mvn install
b) 从archetype创建项目
mvn archetype:generate -DarchetypeCatalog=local
Profile
配置文件
<profiles>
<profile>
<id>dev</id>
<properties>
<profiles.active>dev</profiles.active>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
<profile>
<id>prod</id>
<properties>
<profiles.active>prod</profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<profiles.active>test</profiles.active>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
</profiles>
<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>
代码,指定不同的环境
Setting.xml
<profile>
<id>ali</id>
<repositories>
<repository>
<id>nexus</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<checksumPolicy>fail</checksumPolicy>
<updatePolicy>always</updatePolicy>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<profile>
<id>external</id>
<repositories>
<repository>
<id>repo-mirrors</id>
<url>http://uk.maven.org/maven2</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>plugin-repo-mirror</id>
<url>http://uk.maven.org/maven2</url>
</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.order</groupId>
<artifactId>order-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>
版本
SNAPSHOT
版本会替换仓库中的jar
release
版本不会替换