天天看点

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

在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 {

    @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 运行对应代码即可。

继续阅读