一、常用命令:
1.创建Maven的普通Java项目
mvn archetype:create -DgroupId=packageName -DartifactId=projectName
2.创建Maven的Web项目
mvn archetype:create -DgroupId=packageName -DartifactId=webappName -DarchetypeArtifactId=maven-archetype-webapp
3.反向生成maven项目的骨架
mvn artifacttype:generate
4.编译源代码
mvn compile
5.编译测试代码
mvn test-compile
6.运行测试
mvn test
7.产生site
mvn site
8.打包
mvn package
9.在本地Repository中安装jar
mvn install(例:installing D:\xxx\xx.jar to D:\xx\xxxx)
10.清除产生的项目
mvn clean
11.生成Eclipse项目
mvn eclipse:eclipse
12.生成idea项目
mvn idea:idea
13.组合使用goal命令,如只打包不测试
mvn -Dtest package
14.编译测试的内容
mvn test-compile
15.只打jar包
mvn jar:jar
16.只测试而不编译,也不测试编译
mvn test -skipping compile -skipping test-compile
17.清除eclipse的一些系统设置
mvn eclipse:clean
18.查找当前项目已被解析的依赖
mvn dependency:list
19.上传到私服
mvn deploy
20.强制检查更新,由于快照版本的更新策略(一天更新几次、隔断时间更新一次)存在,如果想强制更新就会用到此命令
mvn clean install-U
21.源码打包
mvn source:jar
或
mvn source:jar-no-fork
二、常用标签
<modelVersion>指定当前pom的版本</modelVersion>
<dependencies>依赖列</dependencies>
<dependency>依赖项</dependency>
<groupId>项目标识</groupId>
<artifactId>项目名+模块名,模块的标识</artifactId>
<version>版本名,大版本号.分支版本号.小版本号</version>
//snapshot快照版本
//alpha内部测试版本
//beta公测版本
//release稳定版本
//GA 正式版本
<packaging>maven项目的打包方式:默认是jar-将项目打包成jar、war、zip、pom</packaging>
<build声明控件</build>
<plugins>插件列表</plugins>
<plugins>插件</plugins>
<executions>一组执行事件</executions>
<execution>执行事件</execution>
<phase>事件</phase>
<name>项目描述名,产生项目文档时才会使用</name>
<url>项目地址</url>
<description>项目描述</description>
<developers>开发者们</developers>
<licenses>许可证信息</licenses>
<organization>组织名</organization>
<type>类型</type>
<scope>依赖范围</scope>
<optional>是指依赖是否可选,默认false</optional>
<exclusions>排除依赖传递列表</exclusions>
<exclusion>排除依赖传递(单个)</exclusion>
<dependencyManagement>依赖的管理,一般用于父类的模块中</dependencyManagement>
<parent>用以子模块对父模块的继承</parent>
<modules>聚合列表</modules>
三、第三方jar上传到本地仓库
常用Maven远程仓库:
http://mvnrepository.com/
http://search.maven.org/
https://repository.sonatype.org/content/groups/public/
第一步,去远程仓库查找并下载jar包,以spring-core为例:
点击第一个:Spring Core
选择自己需要的版本:
点击jar包下载
我下载到了D:\libs\spring-core-5.0.0.RELEASE.jar。
Maven安装JAR包的命令是:
mvn intall:install-file -Dfile=jar包的位置 -DgroupId=上面的groupId -DartifactId=上面的artifactId -Dversion=上面的version -Dpackage=jar
那么我在cmd敲入的命令是:
mvn install:install-file-Dfile=D:\libs\spring-core-5.0.0.RELEASE.jar -DgroupId=org.springframework -DartifactId=spring-core -Dversion=5.0.0.RELEASE -Dpackage=jar
回车即显示安装成功。
四、如何设置本地仓库和远程仓库
Maven安装目录:
4.1 配置本地仓库
用文本编辑器工具打开setting.xml文件,然后配置自定义地址:
<localRepository>
F:\DevInstall\yuxx-meven-libs
</localRepository>
4.2 配置远程仓库
Maven默认的远程地址是:http://my.repository.com/repo/path,这个地址是国外网站,下载速度很慢,这里推荐我国阿里云的地址。
用文本编辑器工具打开setting.xml文件:
<mirrors>
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>