maven 打jar包:mvn clean package
1.打包插件配置:
在maven项目的pom.xml文件中加入maven的打包插件:
<build> <plugins> <!--Spring Boot包含的maven打包插件 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
2.在cmd中,先把命令行切换到Maven项目的根目录,如:D:\微服务架构\ch3 ,执行打包命令:
mvn clean package
执行结果如下:
[INFO] Scanning for projects... [INFO] [INFO] --------------------------< com.example:demo >-------------------------- [INFO] Building demo 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ demo --- [INFO] Deleting D:\微服务架构\ch3\target [INFO] [INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ demo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Using 'UTF-8' encoding to copy filtered properties files. [INFO] Copying 1 resource [INFO] Copying 892 resources [INFO] [INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ demo --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 8 source files to D:\微服务架构\ch3\target\classes [INFO] [INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ demo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Using 'UTF-8' encoding to copy filtered properties files. [INFO] skip non existing resourceDirectory D:\微服务架构\ch3\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ demo --- [INFO] Changes detected - recompiling the module! [INFO] [INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ demo --- [INFO] [INFO] --- maven-jar-plugin:3.2.2:jar (default-jar) @ demo --- [INFO] Building jar: D:\微服务架构\ch3\target\demo-0.0.1-SNAPSHOT.jar [INFO] [INFO] --- spring-boot-maven-plugin:2.6.4:repackage (repackage) @ demo --- [INFO] Replacing main artifact with repackaged archive [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 49.376 s [INFO] Finished at: 2022-04-11T16:49:42+08:00 [INFO] ------------------------------------------------------------------------
上述执行顺序:
(1)使用清理插件:maven-clean-plugin:2.5执行清理删除已有target目录;
(2)使用资源插件:maven-resources-plugin:2.6执行资源文件的处理;
(3)使用编译插件:maven-compiler-plugin:3.1编译所有源文件生成class文件至target\classes目录下;
(4)使用资源插件:maven-resources-plugin:2.6执行测试资源文件的处理;
(5)使用编译插件:maven-compiler-plugin:3.1编译测试目录下的所有源代码;
(6)使用插件:maven-surefire-plugin:2.12运行测试用例;
(7)使用插件:maven-jar-plugin:2.4对编译后生成的文件进行打包,包名称默认为:artifactId-version,比如本例生成的jar文件:demo-0.0.1-SNAPSHOT.jar,包文件保存在target目录下。
3.执行jar包,启动项目:
进入 D:\微服务架构\ch3\target 目录,执行jar包
java -jar demo-0.0.1-SNAPSHOT.jar