个人简介:Java领域新星创作者;阿里云技术博主、星级博主、专家博主;正在Java学习的路上摸爬滚打,记录学习的过程~
个人主页:.29.的博客
学习社区:进去逛一逛~

Maven的构建命令

  • 一、注意
  • 二、清理操作的命令
  • 三、编译操作的命令
  • 四、测试操作的命令
  • 五、打包操作的命令
  • 六、安装操作的命令

一、注意

运行 Maven 构建命令时,必须进入到含有 pom.xml文件的目录中。如果没有在 pom.xml 所在的目录运行 Maven 的构建命令,就会发生错误:The goal you specified requires a project to execute but there is no POM in this directory


二、清理操作的命令

命令

mvn clean

效果

删除 target 目录
(代码为成功效果演示,可忽略…)

D:\maven-workspace\spaceVedio\pro03-maven-parent>mvn clean[INFO] Scanning for projects...[INFO] ------------------------------------------------------------------------[INFO] Reactor Build Order:[INFO][INFO] pro03-maven-parent [pom][INFO] pro06-maven-module [jar][INFO] pro05-maven-module [jar][INFO] pro04-maven-module [jar][INFO][INFO] ----------------< cpm.haojin.maven:pro03-maven-parent >-----------------[INFO] Building pro03-maven-parent 1.0-SNAPSHOT [1/4][INFO] --------------------------------[ pom ]---------------------------------[INFO][INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ pro03-maven-parent ---[INFO][INFO] ----------------< cpm.haojin.maven:pro06-maven-module >-----------------[INFO] Building pro06-maven-module 1.0-SNAPSHOT [2/4][INFO] --------------------------------[ jar ]---------------------------------[INFO][INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ pro06-maven-module ---[INFO] Deleting D:\maven-workspace\spaceVedio\pro03-maven-parent\pro06-maven-module\target[INFO][INFO] ----------------< cpm.haojin.maven:pro05-maven-module >-----------------[INFO] Building pro05-maven-module 1.0-SNAPSHOT [3/4][INFO] --------------------------------[ jar ]---------------------------------[INFO][INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ pro05-maven-module ---[INFO] Deleting D:\maven-workspace\spaceVedio\pro03-maven-parent\pro05-maven-module\target[INFO][INFO] ----------------< com.haojin.maven:pro04-maven-module >-----------------[INFO] Building pro04-maven-module 1.0-SNAPSHOT [4/4][INFO] --------------------------------[ jar ]---------------------------------[INFO][INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ pro04-maven-module ---[INFO] Deleting D:\maven-workspace\spaceVedio\pro03-maven-parent\pro04-maven-module\target[INFO] ------------------------------------------------------------------------[INFO] Reactor Summary for pro03-maven-parent 1.0-SNAPSHOT:[INFO][INFO] pro03-maven-parent ................................. SUCCESS [0.104 s][INFO] pro06-maven-module ................................. SUCCESS [0.024 s][INFO] pro05-maven-module ................................. SUCCESS [0.030 s][INFO] pro04-maven-module ................................. SUCCESS [0.030 s][INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time:0.289 s[INFO] Finished at: 2022-11-27T12:55:29+08:00[INFO] ------------------------------------------------------------------------

三、编译操作的命令

命令 - 效果

编译主程序的命令:mvn compile编译测试程序的命令:mvn test-compile
  • 主体程序编译后的结果存放在target/classes目录下
  • 测试程序编译后的结果存放在target/test-classes目录下

四、测试操作的命令

命令

mvn test

效果
对工程中 test目录 下的程序进行测试,测试的报告放在target/surefire-reports目录


五、打包操作的命令

命令

mvn package

效果

执行命令的工程会被打包成 —— jar包,得到的jar包被存放在target目录中


六、安装操作的命令

命令

mvn intall

效果

将本地构建过程中生成的 jar 包存入 Maven 本地仓库。
(代码为成功效果演示,可忽略… 示例的pro03-maven-parent工程是打包方式为pom的管理子工程的工程,其与其子工程生成的jar包就会被存放到本地仓库的指定路径中去)

D:\maven-workspace\spaceVedio\pro03-maven-parent>mvn install[INFO] Scanning for projects...[INFO] ------------------------------------------------------------------------[INFO] Reactor Build Order:[INFO][INFO] pro03-maven-parent [pom][INFO] pro06-maven-module [jar][INFO] pro05-maven-module [jar][INFO] pro04-maven-module [jar][INFO][INFO] ----------------< cpm.haojin.maven:pro03-maven-parent >-----------------[INFO] Building pro03-maven-parent 1.0-SNAPSHOT [1/4][INFO] --------------------------------[ pom ]---------------------------------[INFO][INFO] --- maven-install-plugin:2.4:install (default-install) @ pro03-maven-parent ---[INFO] Installing D:\maven-workspace\spaceVedio\pro03-maven-parent\pom.xml to d:\maven-repository\cpm\haojin\maven\pro03-maven-parent\1.0-SNAPSHOT\pro03-maven-parent-1.0-SNAPSHOT.pom[INFO][INFO] ----------------< cpm.haojin.maven:pro06-maven-module >-----------------[INFO] Building pro06-maven-module 1.0-SNAPSHOT [2/4][INFO] --------------------------------[ jar ]---------------------------------[INFO][INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ pro06-maven-module ---[INFO] Using 'UTF-8' encoding to copy filtered resources.[INFO] skip non existing resourceDirectory D:\maven-workspace\spaceVedio\pro03-maven-parent\pro06-maven-module\src\main\resources[INFO][INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ pro06-maven-module ---[INFO] Changes detected - recompiling the module![INFO] Compiling 1 source file to D:\maven-workspace\spaceVedio\pro03-maven-parent\pro06-maven-module\target\classes[INFO][INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ pro06-maven-module ---[INFO] Using 'UTF-8' encoding to copy filtered resources.[INFO] skip non existing resourceDirectory D:\maven-workspace\spaceVedio\pro03-maven-parent\pro06-maven-module\src\test\resources[INFO][INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ pro06-maven-module ---[INFO] Changes detected - recompiling the module![INFO] Compiling 1 source file to D:\maven-workspace\spaceVedio\pro03-maven-parent\pro06-maven-module\target\test-classes[INFO][INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ pro06-maven-module ---[INFO] Surefire report directory: D:\maven-workspace\spaceVedio\pro03-maven-parent\pro06-maven-module\target\surefire-reports------------------------------------------------------- T E S T S-------------------------------------------------------Running cpm.haojin.maven.AppTestTests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.007 secResults :Tests run: 1, Failures: 0, Errors: 0, Skipped: 0[INFO][INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ pro06-maven-module ---[INFO] Building jar: D:\maven-workspace\spaceVedio\pro03-maven-parent\pro06-maven-module\target\pro06-maven-module-1.0-SNAPSHOT.jar[INFO][INFO] --- maven-install-plugin:2.4:install (default-install) @ pro06-maven-module ---[INFO] Installing D:\maven-workspace\spaceVedio\pro03-maven-parent\pro06-maven-module\target\pro06-maven-module-1.0-SNAPSHOT.jar to d:\maven-repository\cpm\haojin\maven\pro06-maven-module\1.0-SNAPSHOT\pro06-maven-module-1.0-SNAPSHOT.jar[INFO] Installing D:\maven-workspace\spaceVedio\pro03-maven-parent\pro06-maven-module\pom.xml to d:\maven-repository\cpm\haojin\maven\pro06-maven-module\1.0-SNAPSHOT\pro06-maven-module-1.0-SNAPSHOT.pom[INFO][INFO] ----------------< cpm.haojin.maven:pro05-maven-module >-----------------[INFO] Building pro05-maven-module 1.0-SNAPSHOT [3/4][INFO] --------------------------------[ jar ]---------------------------------[INFO][INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ pro05-maven-module ---[INFO] Using 'UTF-8' encoding to copy filtered resources.[INFO] skip non existing resourceDirectory D:\maven-workspace\spaceVedio\pro03-maven-parent\pro05-maven-module\src\main\resources[INFO][INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ pro05-maven-module ---[INFO] Changes detected - recompiling the module![INFO] Compiling 1 source file to D:\maven-workspace\spaceVedio\pro03-maven-parent\pro05-maven-module\target\classes[INFO][INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ pro05-maven-module ---[INFO] Using 'UTF-8' encoding to copy filtered resources.[INFO] skip non existing resourceDirectory D:\maven-workspace\spaceVedio\pro03-maven-parent\pro05-maven-module\src\test\resources[INFO][INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ pro05-maven-module ---[INFO] Changes detected - recompiling the module![INFO] Compiling 1 source file to D:\maven-workspace\spaceVedio\pro03-maven-parent\pro05-maven-module\target\test-classes[INFO][INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ pro05-maven-module ---[INFO] Surefire report directory: D:\maven-workspace\spaceVedio\pro03-maven-parent\pro05-maven-module\target\surefire-reports------------------------------------------------------- T E S T S-------------------------------------------------------Running cpm.haojin.maven.AppTestTests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 secResults :Tests run: 1, Failures: 0, Errors: 0, Skipped: 0[INFO][INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ pro05-maven-module ---[INFO] Building jar: D:\maven-workspace\spaceVedio\pro03-maven-parent\pro05-maven-module\target\pro05-maven-module-1.0-SNAPSHOT.jar[INFO][INFO] --- maven-install-plugin:2.4:install (default-install) @ pro05-maven-module ---[INFO] Installing D:\maven-workspace\spaceVedio\pro03-maven-parent\pro05-maven-module\target\pro05-maven-module-1.0-SNAPSHOT.jar to d:\maven-repository\cpm\haojin\maven\pro05-maven-module\1.0-SNAPSHOT\pro05-maven-module-1.0-SNAPSHOT.jar[INFO] Installing D:\maven-workspace\spaceVedio\pro03-maven-parent\pro05-maven-module\pom.xml to d:\maven-repository\cpm\haojin\maven\pro05-maven-module\1.0-SNAPSHOT\pro05-maven-module-1.0-SNAPSHOT.pom[INFO][INFO] ----------------< com.haojin.maven:pro04-maven-module >-----------------[INFO] Building pro04-maven-module 1.0-SNAPSHOT [4/4][INFO] --------------------------------[ jar ]---------------------------------[INFO][INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ pro04-maven-module ---[INFO] Using 'UTF-8' encoding to copy filtered resources.[INFO] skip non existing resourceDirectory D:\maven-workspace\spaceVedio\pro03-maven-parent\pro04-maven-module\src\main\resources[INFO][INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ pro04-maven-module ---[INFO] Changes detected - recompiling the module![INFO] Compiling 1 source file to D:\maven-workspace\spaceVedio\pro03-maven-parent\pro04-maven-module\target\classes[INFO][INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ pro04-maven-module ---[INFO] Using 'UTF-8' encoding to copy filtered resources.[INFO] skip non existing resourceDirectory D:\maven-workspace\spaceVedio\pro03-maven-parent\pro04-maven-module\src\test\resources[INFO][INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ pro04-maven-module ---[INFO] Changes detected - recompiling the module![INFO] Compiling 1 source file to D:\maven-workspace\spaceVedio\pro03-maven-parent\pro04-maven-module\target\test-classes[INFO][INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ pro04-maven-module ---[INFO] Surefire report directory: D:\maven-workspace\spaceVedio\pro03-maven-parent\pro04-maven-module\target\surefire-reports------------------------------------------------------- T E S T S-------------------------------------------------------Running com.haojin.maven.AppTestTests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.008 secResults :Tests run: 1, Failures: 0, Errors: 0, Skipped: 0[INFO][INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ pro04-maven-module ---[INFO] Building jar: D:\maven-workspace\spaceVedio\pro03-maven-parent\pro04-maven-module\target\pro04-maven-module-1.0-SNAPSHOT.jar[INFO][INFO] --- maven-install-plugin:2.4:install (default-install) @ pro04-maven-module ---[INFO] Installing D:\maven-workspace\spaceVedio\pro03-maven-parent\pro04-maven-module\target\pro04-maven-module-1.0-SNAPSHOT.jar to d:\maven-repository\com\haojin\maven\pro04-maven-module\1.0-SNAPSHOT\pro04-maven-module-1.0-SNAPSHOT.jar[INFO] Installing D:\maven-workspace\spaceVedio\pro03-maven-parent\pro04-maven-module\pom.xml to d:\maven-repository\com\haojin\maven\pro04-maven-module\1.0-SNAPSHOT\pro04-maven-module-1.0-SNAPSHOT.pom[INFO] ------------------------------------------------------------------------[INFO] Reactor Summary for pro03-maven-parent 1.0-SNAPSHOT:[INFO][INFO] pro03-maven-parent ................................. SUCCESS [0.171 s][INFO] pro06-maven-module ................................. SUCCESS [1.400 s][INFO] pro05-maven-module ................................. SUCCESS [0.370 s][INFO] pro04-maven-module ................................. SUCCESS [0.410 s][INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time:2.452 s[INFO] Finished at: 2022-11-27T13:18:56+08:00[INFO] ------------------------------------------------------------------------

——————————

被存放的 jar 包在 Maven 仓库中的路径是根据它的坐标生成的:

  • 某个工程坐标:
<groupId>cpm.haojin.maven</groupId><artifactId>pro04-maven-module</artifactId><version>1.0-SNAPSHOT</version>
  • 生成的路径:
D:\maven-repository\com\haojin\maven\pro04-maven-module\1.0-SNAPSHOT\pro04-maven-module-1.0-SNAPSHOT.jar