在IDEA中将SpringBoot项目打包成jar包的方法 一文中介绍了使用Intellij Idea中的build artifacts功能自动编译相关jar包,实际上就是先根据项目配置生成MANIFEST.MF配置文件,然后将对应的依赖库打包至指定目录。

除了使用Intellij Idea自带的方式打包以外,还可以使用mvn相关指令打包,主要就是在pom.xml中添加配置说明,然后进行打包。

pom.xml

Intellij Idea中自动生成的pom.xml,较为简洁,基本上都是最基本的依赖。

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.1.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>demo</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-websocket</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

网上有些说法说,需要配置configuration中的mainClass,但是貌似我的项目配置中并未指定入口,最终也正常运行了,不清楚是因为之前的配置影响了什么,还是自动检查了main入口。暂且不提。

常用的mvn 指令

  • 创建Maven的普通java项目

    • mvn archetype:create -DgroupId=packageName   -DartifactId=projectName 
  • 创建Maven的Web项目
    • mvn archetype:create  -DgroupId=packageName  -DartifactId=webappName -DarchetypeArtifactId=maven-archetype-webapp
  • 编译源代码 mvn compile
  • 编译测试代码:mvn test-compile
  • 运行测试:mvn test
  • 产生site:mvn site
  • 打包:mvn package
  • 在本地Repository中安装jar:mvn install
  • 清除产生的项目:mvn clean
  • 生成eclipse项目:mvn eclipse:eclipse
  • 生成idea项目:mvn idea:idea
  • 组合使用goal命令,如只打包不测试:mvn -Dtest package
  • 编译测试的内容:mvn test-compile
  • 只打jar包: mvn jar:jar
  • 只测试而不编译,也不测试编译:mvn test -skipping compile -skipping test-compile 

          ( -skipping 的灵活运用,当然也可以用于其他组合命令) 

  • 清除eclipse的一些系统设置:mvn eclipse:clean

参考链接:https://blog.csdn.net/helloworldouyang/article/details/91874971

mvn clean package

先使用mvn clean 指令清除上次编译信息,然后使用mvn package打包。(此前已通过mvn install等指令将依赖包下载到本地)

单元测试相关报错

[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 2.36 s <<< FAILURE! - in com.example.demo.DemoApplicationTests
[ERROR] contextLoads  Time elapsed: 0.002 s  <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serverEndpointExporter' defined in class path resource [com/example/demo/WebSocketConfig.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: javax.websocket.server.ServerContainer not available
Caused by: java.lang.IllegalStateException: javax.websocket.server.ServerContainer not available[INFO]
[INFO] Results:
[INFO]
[ERROR] Errors:
[ERROR]   DemoApplicationTests.contextLoads » IllegalState Failed to load ApplicationCon...
[INFO]
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  6.505 s
[INFO] Finished at: 2019-12-15T10:52:47+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test) on project demo: There are test failures.
[ERROR]
[ERROR] Please refer to /Users/i5robot/Projects/JavaProjects/TestWebsocket_001/target/surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
[ERROR] -> [Help 1]
[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/MojoFailureException

搜索相关问题,最后确定,是在单元测试代码打包时出错(实际过程可以将单元测试相关过滤,具体操作后续再研究)。

参考链接:https://blog.csdn.net/qq_42651904/article/details/90477684

解决:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

在springbootTest注解加入 webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT

这个注解的意思是:

If you need to start a full running server, we recommend that you use random ports. If you use @SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT), an available port is picked at random each time your test runs.

大致意思是:我们在测试使用 websocket的时候需要启动一个完整的服务器,而使用这个注解就是说每次测试都会选用一个随即可用的端口模拟启动一个完整的服务器,此时问题完美解决!!
————————————————
版权声明:本文为CSDN博主「weihubeats」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_42651904/article/details/90477684

单元测试相关代码中修改如下:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//@SpringBootTest
class DemoApplicationTests {@Testvoid contextLoads() {}}

PoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- maven-jar-plugin:3.1.2:jar (default-jar) @ demo ---
[INFO] Building jar: /Users/i5robot/Projects/JavaProjects/TestWebsocket_001/target/demo-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.2.1.RELEASE:repackage (repackage) @ demo ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  9.557 s
[INFO] Finished at: 2019-12-15T11:17:25+08:00
[INFO] ------------------------------------------------------------------------

直接使用 java -jar demo-0.0.1-SNAPSHOT.jar 运行对应代码即可。

[持续更新] Spring Boot -Maven 指令打包相关记录相关推荐

  1. spring boot maven profiles,打包不同的配置文件

    1. 在pom.xml添加 <profiles><profile><id>dev</id><properties><environme ...

  2. maven打包插件----Spring Boot Maven Plugin

    官方文档链接地址:http://docs.spring.io/spring-boot/docs/1.3.0.BUILD-SNAPSHOT/maven-plugin/index.html 第一部分 Sp ...

  3. Spring Boot+Vue项目打包部署

    在前后端分离的项目中,最后前后端项目开发完毕都需要进行打包部署发布到服务器上面运行,所以需要对前端开发的项目进行打包,然后将打包后的文件放在spring boot项目中的resource/static ...

  4. Spring Boot+Maven实现车牌训练、识别系统

    目录 1.项目功能 2.项目概述 3.项目环境 之前为各位朋友分享过Python+OpenCV实现车牌检测与识别,本篇博文为各位分享Spring Boot+Maven实现车牌训练.识别系统. 1.项目 ...

  5. Spring Boot Maven插件

    Spring Boot Maven插件提供了使用Spring Boot应用程序步骤如下:   重新打包:创建一个可自动执行的jar或war文件.它可以替换常规工件,或者可以使用单独的分类器附加到构建生 ...

  6. spring boot maven项目返回值乱码的解决方法

    spring boot maven项目返回值乱码的解决方法 1.先看乱码效果: spring boot maven项目,返回值乱码,如下图: 控制台打印log乱码,如下图: 有swagger的话,sw ...

  7. spring boot项目Intellij 打包

    spring boot项目Intellij 打包 学习了:http://blog.csdn.net/hzt_fighting_up/article/details/78174291 在edit con ...

  8. Spring Boot的Maven插件Spring Boot Maven plugin详解

    pring Boot的Maven插件(Spring Boot Maven plugin)能够以Maven的方式为应用提供Spring Boot的支持,即为Spring Boot应用提供了执行Maven ...

  9. spring boot + maven + opencv 车牌识别系统,包含车牌检测、车牌号识别训练

    yx-image-recognition 介绍 这是一个基于spring boot + maven + opencv 实现的图像识别及训练的Demo项目 包含车牌识别.人脸识别等功能,贯穿样本处理.模 ...

  10. Spring Boot——Maven使用SystemPath引用本地jar:ClassNotFoundException

    问题描述 <dependency><groupId>com.dingtalk</groupId><artifactId>dingtalk-api-sdk ...

最新文章

  1. 2015年编程之美(资格赛) ---2月29日
  2. 没有最好,只有A/B测试!
  3. 更改MySQL数据库目录位置
  4. libevent事件驱动库的学习视频教程
  5. oracle数据库path,利用Path环境变量解决oracle数据库和owb工具不兼容问题!
  6. 长这么大了,一件事也没做好过
  7. [HTTP] HTTP是什么
  8. RTT——IO设备管理篇·基本概念理解
  9. vue:无法将“vue”识别为脚本_「前端架构」React和Vue -CTO的选择正确框架的指南...
  10. 为什么微信推荐这么快?SimSvr在微信推荐系统中的应用实践
  11. utf-8编码引起js输出中文乱码的解决办法
  12. mysql 触发器详情
  13. 2022年定位系泊系统市场深度分析及发展研究预测报告
  14. springboot项目部署在服务器上
  15. 【C语言】飞翔的小鸟游戏
  16. JWT 避坑指南:nbf 验签失效问题的解决
  17. 通过Alertmanager实现Prometheus的告警告警配置(邮箱加钉钉)
  18. 基于STM32+OV7670+TFT显示(升级篇:将摄像头采集到的画面显示在TFT屏)
  19. DeepMind爆发史:决定AI高峰的“游戏玩家”|深度学习崛起十年
  20. Python-Flask开发微电影网站(四)

热门文章

  1. 集群、分布式架构与SOA架构
  2. Luogu3810 三维偏序(陌上花开)
  3. copyWebpackPlugin的使用及常见问题(glob及Path ............... is not in cwd)
  4. 华为平板m5鸿蒙,华为平板M5系列发布:搭载麒麟960 售价2088元起
  5. matlab中unique的作用,matlab中的unique函数详解
  6. sklearn kfold_sklearn函数:cross_val_score(交叉验证评分)
  7. java 求两点的角度_计算两点之间的角度 – java
  8. 关于2020idea不能创建web项目问题
  9. PHP网站从Apache转移到Nginx后产生404错误的原因和解决办法
  10. 后端如何接收对象类型的数据_javascript基本数据类型赋值和对象引用的内存情况分析...