- 1. settings.xml文件配置
- 1.1 mirrors配置
- 1.2 配置Maven工程的默认JDK版本
- 2. mvn命令大全
- 2.1 mvn archetype:generate-通过模板创建项目
- 2.2 mvn compile-编译项目
- 2.3 mvn test-测试操作
- 2.4 mvn install-安装操作
- 2.5 mvn-dependency-查看依赖
配置maven central的国内镜像地址
alimaven
aliyun maven
central
https://maven.aliyun.com/repository/central
默认会从maven的中央仓库https://repo1.maven.org/maven2/进行依赖包拉取,其id为central。这里配置mirrorOf表示拦截从id为central的仓库进行依赖包拉取,改为从https://maven.aliyun.com/repository/central进行依赖包拉取,id和name自定义
可以配置多个mirrorOf指向central的mirror,只有第一个连接不上,才会使用第二个。如果第一个连接上了,但没有该依赖包,也不会使用第二个
1.2 配置Maven工程的默认JDK版本如果默认的JDK不是我们想要的,可以参考如下方式进行配置
jdk-11
true
11
11
11
11
2. mvn命令大全
2.1 mvn archetype:generate-通过模板创建项目
方式一:通过maven的插件archetype进行交互式的generate
C:\Users\dell\Desktop>mvn archetype:generate
......省略部分......
Choose archetype:
1: internal -> org.apache.maven.archetypes:maven-archetype-archetype (An archetype which contains a sample archetype.)
2: internal -> org.apache.maven.archetypes:maven-archetype-j2ee-simple (An archetype which contains a simplifed sample J2EE application.)
3: internal -> org.apache.maven.archetypes:maven-archetype-plugin (An archetype which contains a sample Maven plugin.)
4: internal -> org.apache.maven.archetypes:maven-archetype-plugin-site (An archetype which contains a sample Maven plugin site.
This archetype can be layered upon an existing Maven plugin project.)
5: internal -> org.apache.maven.archetypes:maven-archetype-portlet (An archetype which contains a sample JSR-268 Portlet.)
6: internal -> org.apache.maven.archetypes:maven-archetype-profiles ()
7: internal -> org.apache.maven.archetypes:maven-archetype-quickstart (An archetype which contains a sample Maven project.)
8: internal -> org.apache.maven.archetypes:maven-archetype-site (An archetype which contains a sample Maven site which demonstrates
some of the supported document types like APT, XDoc, and FML and demonstrates how
to i18n your site. This archetype can be layered upon an existing Maven project.)
9: internal -> org.apache.maven.archetypes:maven-archetype-site-simple (An archetype which contains a sample Maven site.)
10: internal -> org.apache.maven.archetypes:maven-archetype-webapp (An archetype which contains a sample Maven Webapp project.)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 7: 7
Define value for property 'groupId': com.hh
Define value for property 'artifactId': maven-learn
Define value for property 'version' 1.0-SNAPSHOT: : 0.1
Define value for property 'package' com.hh: : com.hh
Confirm properties configuration:
groupId: com.hh
artifactId: maven-learn
version: 0.1
package: com.hh
Y: : y
......省略部分......
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:33 min
[INFO] Finished at: 2022-06-23T16:35:15+08:00
[INFO] ------------------------------------------------------------------------
C:\Users\dell\Desktop>
说明如下:
- 这里选择了模板7,为maven-archetype-quickstart
- pom.xml种junit版本为3.8.1,可以改成最新的4.13.2版本
- 自动生成的App.java和AppTest.java可以删除
pom(Project Object Model),即项目对象模型。自动生成的pom.xml解读
4.0.0
com.hh
maven-learn
0.1
jar
maven-learn
http://maven.apache.org
UTF-8
junit
junit
4.13.2
test
方式二:通过maven的插件archetype进行非交互式的generate
C:\Users\dell\Desktop>mvn archetype:generate -DarchetypeGroupId=org.apache.flink -DarchetypeArtifactId=flink-quickstart-java -DarchetypeVersion=1.15.0 -DgroupId=com.hh -DartifactId=flink-test -Dversion=0.1 -Dpackage=my-flink -DinteractiveMode=false
C:\Users\dell\Desktop>
根据指导的模板,生成我们自己的项目
2.2 mvn compile-编译项目默认使用3.1版本的maven-compiler-plugin插件
org.apache.maven.plugins
maven-compiler-plugin
3.10.1
11
11
主程序编译命令
C:\Users\dell\Desktop\maven-learn>mvn clean compile
编译后的文件位于target/classes目录下
测试程序编译命令
C:\Users\dell\Desktop\maven-learn>mvn clean test-compile
编译后的文件位于target/test-classes目录下
2.3 mvn test-测试操作编写一个java测试程序。然后执行
C:\Users\dell\Desktop\maven-learn> mvn clean test
......省略部分......
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.hh.Test
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.083 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.573 s
[INFO] Finished at: 2022-06-23T17:42:16+08:00
[INFO] ------------------------------------------------------------------------
C:\Users\dell\Desktop\maven-learn>
测试报告存放在target/surefire-reports目录下
2.4 mvn install-安装操作会对项目进行打包,同时将jar包保存到本地的repository仓库中
C:\Users\dell\Desktop\maven-learn>mvn clean install
2.5 mvn-dependency-查看依赖
以列表形式查看所有dependency,所有依赖没有层级关系
C:\Users\dell\Desktop\maven-learn>mvn dependency:list
.....省略部分......
[INFO] The following files have been resolved:
[INFO] junit:junit:jar:4.13.2:test
[INFO] org.hamcrest:hamcrest-core:jar:1.3:test
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
.....省略部分......
C:\Users\dell\Desktop\maven-learn>
以树形结构查看所有dependency,所有依赖有层级关系
C:\Users\dell\Desktop\maven-learn>mvn dependency:tree
.....省略部分......
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ maven-learn ---
[INFO] com.hh:maven-learn:jar:0.1
[INFO] \- junit:junit:jar:4.13.2:test
[INFO] \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
.....省略部分......
C:\Users\dell\Desktop\maven-learn>