④【Maven】Maven的构建命令

简介: ④【Maven】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.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.007 sec
Results :
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.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 sec
Results :
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.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.008 sec
Results :
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


目录
相关文章
|
4月前
|
Java 测试技术 Maven
maven总结一: 常用命令
maven总结一: 常用命令
38 6
|
1月前
|
Java 测试技术 Maven
maven 打包命令
maven 打包命令
25 6
|
1月前
|
Java Linux Maven
IDEA如何用maven打包(界面和命令两种方式)
【10月更文挑战第14天】本文介绍了两种Maven项目打包方法:命令行与IDEA界面。首先确保已安装Maven并配置环境变量,通过`mvn -v`检查安装。命令行打包需进入项目目录,执行`mvn package`,之后在`target`目录查看结果。IDEA打包则需配置Maven路径,打开Maven Projects窗口,双击Lifecycle下的`package`阶段,同样在`target`目录查找生成文件,并在Build窗口查看日志以排查问题。
648 1
|
16天前
|
Java 测试技术 项目管理
maven编译本地代码的命令是什么?
【10月更文挑战第26天】maven编译本地代码的命令是什么?
35 0
|
3月前
|
Java 持续交付 项目管理
Maven是一款基于Apache许可的项目管理和构建自动化工具,在Java开发中极为流行。
Maven是一款基于Apache许可的项目管理和构建自动化工具,在Java开发中极为流行。它采用项目对象模型(POM)来描述项目,简化构建流程。Maven提供依赖管理、标准构建生命周期、插件扩展等功能,支持多模块项目及版本控制。在Java Web开发中,Maven能够自动生成项目结构、管理依赖、自动化构建流程并运行多种插件任务,如代码质量检查和单元测试。遵循Maven的最佳实践,结合持续集成工具,可以显著提升开发效率和项目质量。
52 1
|
4月前
|
自然语言处理 Java Maven
Maven 自动化构建
Maven自动化构建确保依赖稳定性。`bus-core-api(1.0-SNAPSHOT)`构建后,自动触发`app-web-ui`和`app-desktop-ui`的构建,两者均依赖bus-core-api的1.0快照版。 ``` 纯文本摘要: Maven自动化构建保证依赖项的稳定性:当`bus-core-api`的1.0-SNAPSHOT版本完成构建时,它会触发依赖它的`app-web-ui`和`app-desktop-ui`项目的构建,这两个项目都依赖`bus-core-api`的相同版本。
|
4月前
|
Java API Maven
Maven 自动化构建
**摘要 (Markdown 格式)**: Maven自动化构建确保依赖稳定性。当`bus-core-api` (1.0-SNAPSHOT) 构建后,依赖它的`app-web-ui`和`app-desktop-ui`(均依赖bus-core-api的1.0快照)会自动触发构建,保证与最新核心API兼容。
|
4月前
|
XML Java Maven
Maven 构建配置文件
Maven构建配置文件用于定制不同环境的构建,如生产与开发。配置在`pom.xml`的`profiles`中,可通过命令行、设置文件、环境变量等方式激活。配置文件分项目级、用户级和全局级。例如,`env.properties`为默认,`env.test.properties`和`env.prod.properties`代表测试和生产环境。激活配置文件可影响如数据库URL等参数。示例中用AntRun插件展示配置应用,但实际配置文件功能不限于此。
|
4月前
|
Java 测试技术 Maven
Maven 构建生命周期
Maven生命周期包括Clean(清除)、Default(构建)和Site(站点)三个阶段,用于项目构建与发布。Default生命周期含validate、compile、test、package、verify、install和deploy,确保代码编译、测试、打包和部署。Clean清除目标目录,Site生成项目文档。每个阶段由插件目标组成,如mvn clean package,先执行清理,再打包。
|
4月前
|
Java Maven
Maven 自动化构建
**Maven自动化构建确保依赖稳定性:** 当`bus-core-api`(1.0-SNAPSHOT)构建后,依赖它的`app-web-ui`和`app-desktop-ui`自动触发构建。`app-web-ui`与`app-desktop-ui`都引用`bus-core-api`的1.0快照版本,实现版本同步。