maven基本命令

命令介绍

  • clean
  • validate
  • compile
  • test
  • package
  • verify
  • install
  • site
  • deploy
    以上整体命令构成了maven的整个生命周期

创建一个简单的项目

1
2
3
#可以使用mvn命令创建一个简单的项目
$mvn archetype:generate -DgroupId=cn.com.wyc -DartifactId=simple -DpackageName=cn.com.wyc.maven.test
#可以使用开发集成idea创建简单的项目

mvn clean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#清理之前构件的工程,说白了就是删除target包下的内容
#执行与显示结果
$ mvn clean
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-test 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-test ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.355 s
[INFO] Finished at: 2019-04-28T11:09:29+08:00
[INFO] Final Memory: 6M/123M
[INFO] ------------------------------------------------------------------------

mvn validate

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#验证工程是否正确,所有需要的资源是否可用
#这里我们自定义一个pom文件的错误
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>cn.com.wyc</groupId>
<artifactId>maven-test</artifactId>
<version>1.0-SNAPSHOT</version>

<xxx></xxx>

</project>
#执行结果如下:
$ mvn validate
[INFO] Scanning for projects...
[ERROR] [ERROR] Some problems were encountered while processing the POMs:
[ERROR] Malformed POM /Users/wyc/company/project/xqy-tax-11-29/maven-test/pom.xml: Unrecognised tag: 'xxx' (position: START_TAG seen ...</version>\n\n <xxx>... @11:10) @ /Users/wyc/company/project/xqy-tax-11-29/maven-test/pom.xml, line 11, column 10
@
[ERROR] The build could not read 1 project -> [Help 1]
[ERROR]
[ERROR] The project cn.com.wyc:maven-test:1.0-SNAPSHOT (/Users/wyc/company/project/xqy-tax-11-29/maven-test/pom.xml) has 1 error
[ERROR] Malformed POM /Users/wyc/company/project/xqy-tax-11-29/maven-test/pom.xml: Unrecognised tag: 'xxx' (position: START_TAG seen ...</version>\n\n <xxx>... @11:10) @ /Users/wyc/company/project/xqy-tax-11-29/maven-test/pom.xml, line 11, column 10 -> [Help 2]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException
[ERROR] [Help 2] http://cwiki.apache.org/confluence/display/MAVEN/ModelParseException
#一般可以用mvn validate验证pom文件是否正确

mvn compile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#测试执行结果如下:
$mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-test 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-test ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-test ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /Users/wyc/company/project/xqy-tax-11-29/maven-test/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.798 s
[INFO] Finished at: 2019-04-28T11:21:24+08:00
[INFO] Final Memory: 13M/165M
[INFO] ------------------------------------------------------------------------
#BUILD SUCCESS说明编译成功了,将 项目路径/src 中的源码文件和配置文件 编译并生成到了 项目路径/target/ 下

mvn test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#执行 项目路径/src/test中的单元测试
$ mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-test 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-test ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-test ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-test ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/wyc/company/project/xqy-tax-11-29/maven-test/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-test ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /Users/wyc/company/project/xqy-tax-11-29/maven-test/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-test ---
[INFO] Surefire report directory: /Users/wyc/company/project/xqy-tax-11-29/maven-test/target/surefire-reports
wycdeMacBook-Pro:maven-test wyc$ mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-test 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-test ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-test ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-test ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/wyc/company/project/xqy-tax-11-29/maven-test/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-test ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-test ---
[INFO] Surefire report directory: /Users/wyc/company/project/xqy-tax-11-29/maven-test/target/surefire-reports

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running cn.com.wyc.MavenTestTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.103 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.843 s
[INFO] Finished at: 2019-04-28T11:32:58+08:00
[INFO] Final Memory: 10M/220M
[INFO] ------------------------------------------------------------------------
#集成测试可以用来验证代码逻辑的修改是否存在影响

mvn package

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#对项目进行打包,jar或者war,具体在pom文件中进行说明
$ mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-test 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-test ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-test ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-test ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/wyc/company/project/xqy-tax-11-29/maven-test/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-test ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-test ---
[INFO] Surefire report directory: /Users/wyc/company/project/xqy-tax-11-29/maven-test/target/surefire-reports

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running cn.com.wyc.MavenTestTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.09 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-test ---
[INFO] Building jar: /Users/wyc/company/project/xqy-tax-11-29/maven-test/target/maven-test-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.227 s
[INFO] Finished at: 2019-04-28T11:36:28+08:00
[INFO] Final Memory: 11M/220M
[INFO] ------------------------------------------------------------------------
#打包命令,看日志可以发现需要执行验证,编译,测试等过程
#有时候测试的会一直过不了,打包时你可能需要跳过测试,可以执行如下命令跳过
$mvn package -DskipTests

$mvn package -Dmaven.test.skip=true

mvn install

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#将jar包上传到本地仓库
$ mvn install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-test 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-test ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-test ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-test ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/wyc/company/project/xqy-tax-11-29/maven-test/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-test ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-test ---
[INFO] Surefire report directory: /Users/wyc/company/project/xqy-tax-11-29/maven-test/target/surefire-reports

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running cn.com.wyc.MavenTestTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.109 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-test ---
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ maven-test ---
[INFO] Installing /Users/wyc/company/project/xqy-tax-11-29/maven-test/target/maven-test-1.0-SNAPSHOT.jar to /Users/wyc/company/xxxx/cn/com/wyc/maven-test/1.0-SNAPSHOT/maven-test-1.0-SNAPSHOT.jar
[INFO] Installing /Users/wyc/company/project/xqy-tax-11-29/maven-test/pom.xml to /Users/wyc/company/xxxx/cn/com/wyc/maven-test/1.0-SNAPSHOT/maven-test-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.311 s
[INFO] Finished at: 2019-04-28T11:40:01+08:00
[INFO] Final Memory: 11M/155M
[INFO] ------------------------------------------------------------------------

mvn site

1
#生成站点报告

mvn deploy

1
#将生成的jar上传到本地仓库和远程仓库
显示 Gitment 评论