首页 > 其他 > 详细

常用 maven 命令

时间:2020-05-23 20:00:26      阅读:39      评论:0      收藏:0      [点我收藏+]

 

 

首先我们创建一个名为 hello-world 的 Maven 项目,项目的目录结构如下

[root@ci-node2 hello-world]# ll
total 4
-rw-r--r-- 1 root root 1683 Aug 26  2010 pom.xml
drwxr-xr-x 4 root root   30 Oct 10  2009 src
[root@ci-node2 hello-world]# tree .
.
├── pom.xml
└── src  // 这是 helo-world 源代码
    ├── main
    │   └── java
    │       └── com
    │           └── juvenxu
    │               └── mvnbook
    │                   └── helloworld
    │                       └── HelloWorld.java
    └── test  // 这是写的测试 单元测试
        └── java
            └── com
                └── juvenxu
                    └── mvnbook
                        └── helloworld
                            └── HelloWorldTest.java

13 directories, 3 files
[root@ci-node2 hello-world]# 

用测试 测试源代码正确不正确,符合不规范

pom.xml 是maven项目的核心,maven构建先去找pom.xml文件

maven 先去找pom.xml文件

 

 

打包命令 mvn package

mvn package 命令用于项目打包,会在模块下的 target 目录生成 jar 或者 war 等文件

对源码进行编译,打包成war包或者jar包

一定要去这个项目的的根目录,有pom.xml文件 的地方执行 mvn package 

 

 

https://repo.maven.apache.org是maven 中心仓库

maven在执行构建时候,需要一些辅助jar包,这些辅助jar包从 maven的中心仓库下载,把一些依赖的包下载到本地  下载到项目

 

下载到.m2文件,m2也叫maven的本地仓库,远程仓库是maven中心仓库

 

当执行mvn package 命令时候,maven会找相关构建的依赖包,找依赖包先去,本地仓库 .m2文件找,

1.如果这个仓库里面有依赖包,直接从本地仓库.m2复制过来 执行

2.如果没有,就去maven中心仓库去找,maven中心仓库是一定有的,如果没有就报错了,有就下载到本地的m2文件里面,再执行构建,

所以第一次执行命令时候,会很慢,网速依赖,把jar包下完以后才能正常执行

 

为了解决这个慢的问题:

可以搭私服,私服就是内网搭建一台服务器 代替maven中心仓库,先把maven的依赖包 下载到私服服务器上,这样就构建特别快,

每次执行构建时候都要去maven中心仓库拉一次,太慢了

 

 

 

pom.xml文件里面有 包的名称,版本,依赖,执行的构建,要执行的依赖包都在这个pom.xml文件定义的,

执行mvn package 命令时候 一定要在有pom.xml文件的同目录执行

 

 

 

执行完 会在项目文件夹下生成target目录,在target目录就是编译的结果,打包生成的结果都在这里

Downloaded: https://repo.maven.apache.org/maven2/asm/asm-commons/3.1/asm-commons-3.1.jar (32 KB at 61.2 KB/sec)
Downloaded: https://repo.maven.apache.org/maven2/asm/asm-tree/3.1/asm-tree-3.1.jar (22 KB at 40.5 KB/sec)
Downloaded: https://repo.maven.apache.org/maven2/jdom/jdom/1.0/jdom-1.0.jar (150 KB at 262.1 KB/sec)
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-dependency-tree/1.1/maven-dependency-tree-1.1.jar (34 KB at 33.2 KB/sec)
Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.5.5/plexus-utils-1.5.5.jar (246 KB at 167.6 KB/sec)
[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing /root/hello-world/target/hello-world-1.0-SNAPSHOT.jar with /root/hello-world/target/hello-world-1.0-SNAPSHOT-shaded.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12:58 min
[INFO] Finished at: 2020-04-21T00:50:57+08:00
[INFO] Final Memory: 15M/37M
[INFO] ------------------------------------------------------------------------

 

在当前目录生成一个target文件夹

[root@ci-node2 hello-world]# ll
total 4
-rw-r--r-- 1 root root 1683 Aug 26  2010 pom.xml
drwxr-xr-x 4 root root   30 Oct 10  2009 src
drwxr-xr-x 7 root root  188 Apr 21 00:50 target

生成的包,全部都在这里面

[root@ci-node2 target]# ll
total 8
drwxr-xr-x 3 root root   17 Apr 21 00:50 classes
-rw-r--r-- 1 root root 3132 Apr 21 00:50 hello-world-1.0-SNAPSHOT.jar
drwxr-xr-x 2 root root   28 Apr 21 00:50 maven-archiver
drwxr-xr-x 3 root root   35 Apr 21 00:50 maven-status
-rw-r--r-- 1 root root 2874 Apr 21 00:50 original-hello-world-1.0-SNAPSHOT.jar
drwxr-xr-x 2 root root  125 Apr 21 00:50 surefire-reports
drwxr-xr-x 3 root root   17 Apr 21 00:50 test-classes
[root@ci-node2 target]# 

 jar包可以执行的,有输出 这个jar包就是生产Hello Maven

[root@ci-node2 target]# java -jar hello-world-1.0-SNAPSHOT.jar 
Hello Maven

我们可以看看源码

进入此路径

[root@ci-node2 hello-world]# cd src/main/java/com/juvenxu/mvnbook/helloworld/
[root@ci-node2 helloworld]# pwd
/root/hello-world/src/main/java/com/juvenxu/mvnbook/helloworld

vim 类的源码

[root@ci-node2 helloworld]# vim HelloWorld.java 

 

package com.juvenxu.mvnbook.helloworld;

public class HelloWorld {

        public String sayHello()
        {
                return "Hello Maven";
        }

        public static void main(String[] args)
        {
                System.out.print( new HelloWorld().sayHello() );
        }
}

再执行 第二次mvn package时候 ,就很快,因为 依赖包都下载到.m2文件。

找依赖包,先去.m2文件找,.m2文件有依赖包就直接获取,不用去maven中心仓库。 如果没有,就要去maven中心仓库下载

[root@ci-node2 hello-world]# mvn package
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.juvenxu.mvnbook:hello-world:jar:1.0-SNAPSHOT
[WARNING] ‘build.plugins.plugin.version‘ for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 21, column 14
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Hello World Project 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-world ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /root/hello-world/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello-world ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello-world ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /root/hello-world/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hello-world ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-world ---
[INFO] Surefire report directory: /root/hello-world/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.juvenxu.mvnbook.helloworld.HelloWorldTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.036 sec

Results :

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

[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hello-world ---
[INFO] 
[INFO] --- maven-shade-plugin:1.2.1:shade (default) @ hello-world ---
[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing /root/hello-world/target/hello-world-1.0-SNAPSHOT.jar with /root/hello-world/target/hello-world-1.0-SNAPSHOT-shaded.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.878 s
[INFO] Finished at: 2020-04-22T00:40:18+08:00
[INFO] Final Memory: 9M/21M
[INFO] -----------------------------------------------------------------------

 

mvn clear

我们在此项目的基本上测试常用的 maven 命令:
mvn clean 命令用于清理项目生产的临时文件,一般是模块下的 target 目录

每次编译都会生成一些构建产物 执行mvn clean 把构建产物清除

把上次的结果清除掉

 

执行完mvn clean target目标没有了

 

[root@ci-node2 hello-world]# mvn clean
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.juvenxu.mvnbook:hello-world:jar:1.0-SNAPSHOT
[WARNING] ‘build.plugins.plugin.version‘ for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 21, column 14
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Hello World Project 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom (4 KB at 2.2 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.jar
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.jar (25 KB at 28.7 KB/sec)
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-world ---
[INFO] Deleting /root/hello-world/target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.363 s
[INFO] Finished at: 2020-04-22T00:48:46+08:00
[INFO] Final Memory: 7M/18M
[INFO] ------------------------------------------------------------------------


[root@ci-node2 hello-world]# ll
total 4
-rw-r--r-- 1 root root 1683 Aug 26  2010 pom.xml
drwxr-xr-x 4 root root   30 Oct 10  2009 src

 

我们再执行 mvn package,这次 很快,生成构建

 

[root@ci-node2 hello-world]# mvn package

[root@ci-node2 hello-world]# ll
total 4
-rw-r--r-- 1 root root 1683 Aug 26  2010 pom.xml
drwxr-xr-x 4 root root   30 Oct 10  2009 src
drwxr-xr-x 7 root root  188 Apr 22 00:51 target

 

 

 

 

 mvn test

mvn test 命 令 用 于 测 试 , 用 于 执 行 src/test/java/ 下 的 测 试 用 例 , 使 用
-Dmaven.test.skip=true 
参数可以跳过测试。

 

 

一个项目目录 有一套是源码和有一套是测试代码(单元测试),当执行mvn test 他会自动帮你 调用测试代码去测试类 有没有问题

如果没有问题,可以执行编译

如果有问题,会报错,解决错误,解决错误后才能继续

 

我们先执行 make clean 清除上次的构建

[root@ci-node2 hello-world]# mvn clean 

[root@ci-node2 hello-world]# mvn test
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.juvenxu.mvnbook:hello-world:jar:1.0-SNAPSHOT
[WARNING] build.plugins.plugin.version for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 21, column 14
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Hello World Project 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-world ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /root/hello-world/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello-world ---
[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 /root/hello-world/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello-world ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /root/hello-world/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hello-world ---
[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 /root/hello-world/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-world ---
[INFO] Surefire report directory: /root/hello-world/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.juvenxu.mvnbook.helloworld.HelloWorldTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.07 sec

Results :

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

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.064 s
[INFO] Finished at: 2020-04-22T00:57:41+08:00
[INFO] Final Memory: 13M/31M
[INFO] ------------------------------------------------------------------------

 

 

我们进去生成的target目录,生成的 taget目录 没有jar包,他只是做了测试,通过与不通过

[root@ci-node2 hello-world]# ll
total 4
-rw-r--r-- 1 root root 1683 Aug 26  2010 pom.xml
drwxr-xr-x 4 root root   30 Oct 10  2009 src
drwxr-xr-x 6 root root   85 Apr 22 00:57 target
[root@ci-node2 hello-world]# cd target/
[root@ci-node2 target]# ll
total 0
drwxr-xr-x 3 root root  17 Apr 22 00:57 classes
drwxr-xr-x 3 root root  35 Apr 22 00:57 maven-status
drwxr-xr-x 2 root root 125 Apr 22 00:57 surefire-reports
drwxr-xr-x 3 root root  17 Apr 22 00:57 test-classes

 

 

 mvn install

 mvn install 命令用于模块安装,将打包好的 jar/war 文件复制(安装到)到你的本地仓库.m2文件中,供其他模块使用

 

我们先看看m2文件 ,去根目录 cd /root/,看到隐藏文件.m2文件

 

 

[root@ci-node2 ~]# ls -al
total 52
dr-xr-x---.  7 root root  277 Apr 22 00:39 .
dr-xr-xr-x. 19 root root  250 Apr 12 22:16 ..
-rw-------.  1 root root 1824 Aug 24  2019 anaconda-ks.cfg
-rw-------.  1 root root 4718 Apr 20 02:41 .bash_history
-rw-r--r--.  1 root root   18 Dec 29  2013 .bash_logout
-rw-r--r--.  1 root root  176 Dec 29  2013 .bash_profile
-rw-r--r--.  1 root root  176 Dec 29  2013 .bashrc
-rw-r--r--.  1 root root  100 Dec 29  2013 .cshrc
-rw-r--r--   1 root root   51 Mar 30 00:33 .gitconfig
drwxr-xr-x   3 root root   20 Apr 13 01:19 .groovy
drwxr-xr-x   4 root root   46 Apr 22 00:57 hello-world
-rw-r--r--   1 root root 1325 Dec 17  2018 hello-world.tar.gz
drwxr-xr-x   3 root root   24 Apr 21 00:37 .m2
drwxr-xr-x   2 root root   40 Apr  6 18:08 .oracle_jre_usage
drwx------   2 root root   57 Apr 12 22:33 .ssh
-rw-r--r--.  1 root root  129 Dec 29  2013 .tcshrc
-rw-------   1 root root 8407 Apr 22 00:39 .viminfo

 

我们进入.m2目录看看

[root@ci-node2 ~]# cd .m2/
[root@ci
-node2 .m2]# ll total 0 drwxr-xr-x 13 root root 190 Apr 21 00:50 repository
[root@ci
-node2 .m2]# cd repository/
[root@ci-node2 repository]# ll total 0 drwxr-xr-x 6 root root 70 Apr 21 00:50 asm drwxr-xr-x 3 root root 38 Apr 21 00:50 backport-util-concurrent drwxr-xr-x 3 root root 25 Apr 21 00:38 classworlds drwxr-xr-x 3 root root 20 Apr 21 00:39 com drwxr-xr-x 3 root root 25 Apr 21 00:38 commons-cli drwxr-xr-x 3 root root 26 Apr 21 00:50 commons-lang drwxr-xr-x 3 root root 33 Apr 21 00:50 commons-logging drwxr-xr-x 3 root root 18 Apr 21 00:50 jdom drwxr-xr-x 3 root root 19 Apr 21 00:38 junit drwxr-xr-x 3 root root 19 Apr 21 00:50 log4j drwxr-xr-x 6 root root 65 Apr 21 00:39 org

我们到com目录,找不到项目 heloworld 的包

[root@ci-node2 repository]# cd com
[root@ci-node2 com]# ll
total 0
drwxr-xr-x 5 root root 51 Apr 21 00:50 google

 

 

到项目目录

[root@ci-node2 com]# cd /root/hello-world
[root@ci-node2 hello-world]# ll
total 4
-rw-r--r-- 1 root root 1683 Aug 26  2010 pom.xml
drwxr-xr-x 4 root root   30 Oct 10  2009 src
drwxr-xr-x 6 root root   85 Apr 22 00:57 target

 

 

我们先执行 make clean 清除上次的构建

 

[root@ci-node2 hello-world]# mvn clean

执行 mvn install  ,执行完以后,他会把 jar包 复制(安装到,部署到)到.m2文件 的本地仓库里面

 

执行完,本地有jar包

 

[root@ci-node2 hello-world]# ll
total 4
-rw-r--r-- 1 root root 1683 Aug 26  2010 pom.xml
drwxr-xr-x 4 root root   30 Oct 10  2009 src
drwxr-xr-x 7 root root  188 Apr 22 01:12 target

[root@ci-node2 hello-world]# cd target/
[root@ci-node2 target]# ll
total 8
drwxr-xr-x 3 root root   17 Apr 22 01:12 classes
-rw-r--r-- 1 root root 3131 Apr 22 01:12 hello-world-1.0-SNAPSHOT.jar
drwxr-xr-x 2 root root   28 Apr 22 01:12 maven-archiver
drwxr-xr-x 3 root root   35 Apr 22 01:12 maven-status
-rw-r--r-- 1 root root 2873 Apr 22 01:12 original-hello-world-1.0-SNAPSHOT.jar
drwxr-xr-x 2 root root  125 Apr 22 01:12 surefire-reports
drwxr-xr-x 3 root root   17 Apr 22 01:12 test-classes

 

 

 

部署 提示信息有

[INFO] Installing /root/hello-world/target/hello-world-1.0-SNAPSHOT.jar to /root/.m2/repository/com/juvenxu/mvnbook/hello-world/1.0-SNAPSHOT/hello-world-1.0-SNAPSHOT.jar
[INFO] Installing /root/hello-world/pom.xml to /root/.m2/repository/com/juvenxu/mvnbook/hello-world/1.0-SNAPSHOT/hello-world-1.0-SNAPSHOT.pom

 

 看到juvenxu 目录

[root@ci-node2 target]# cd /root/.m2/repository/com/
You have new mail in /var/spool/mail/root
[root@ci-node2 com]# ll
total 0
drwxr-xr-x 5 root root 51 Apr 21 00:50 google
drwxr-xr-x 3 root root 21 Apr 22 01:12 juvenxu
[root@ci-node2 com]# pwd
/root/.m2/repository/com

 

用tree命令执行下,看到jar包了

[root@ci-node2 com]# tree juvenxu/
juvenxu/
└── mvnbook
    └── hello-world
        ├── 1.0-SNAPSHOT
        │   ├── hello-world-1.0-SNAPSHOT.jar
        │   ├── hello-world-1.0-SNAPSHOT.pom
        │   ├── maven-metadata-local.xml
        │   └── _remote.repositories
        └── maven-metadata-local.xml

3 directories, 5 files

 

 

 

 

maven仓库就是存放依赖、构建的地方,有maven两种、

1.本地仓库 .m2文件

2.远程仓库 maven 中央仓库 ,私服

 

常用 maven 命令

原文:https://www.cnblogs.com/mingerlcm/p/12742077.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!