在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
- mvn archetype:create -DgroupId=packageName
- 建立Maven的Web項目
- mvn archetype:create -DgroupId=packageName
-DarchetypeArtifactId=maven-archetype-webapp-DartifactId=webappName
- mvn archetype:create -DgroupId=packageName
- 編譯源代碼 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 {
@Test
void 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 運作對應代碼即可。