【问题标题】:How to create fat jar with tests sources and all dependencies on spring-boot application with TestNG and run tests?如何使用 TestNG 创建带有测试源的胖 jar 和对 spring-boot 应用程序的所有依赖项并运行测试?
【发布时间】:2021-01-04 03:22:11
【问题描述】:

如何使用 TestNG 创建包含测试源和所有依赖于 spring-boot 应用程序的 fat jar 并运行测试?

【问题讨论】:

    标签: spring-boot jar testng fat fatjar


    【解决方案1】:

    这是我的 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
       
        <packaging>jar</packaging>
    
        <properties>
        <!-- the main function-->
            <start-class>org.kuohai.TestNGMain</start-class>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.uncommons</groupId>
                <artifactId>reportng</artifactId>
                <version>1.1.4</version>
            </dependency>
            <dependency>
                <groupId>org.testng</groupId>
                <artifactId>testng</artifactId>
                <version>6.8</version>
            </dependency>
        </dependencies>
        <build>
            <finalName>packageName</finalName>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <includes>
                        <include>**/*.*</include>
                    </includes>
                </resource>
            </resources>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <version>2.16</version>
                    <executions>
                        <execution>
                            <id>default-integration-test</id>
                            <goals>
                                <goal>integration-test</goal>
                            </goals>
                            <configuration>
                                <excludes>
                                    <exclude>none</exclude>
                                </excludes>
                                <includes>
                                    <include>**/*Test.java</include>
                                </includes>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.3</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.17</version>
                    <configuration>
                        <suiteXmlFiles>
                            <suiteXmlFile>src/main/resources/testng.xml</suiteXmlFile>
                        </suiteXmlFiles>
    
                        <properties>
                            <property>
                                <name>usedefaultlisteners</name>
                                <value>false</value>
                            </property>
                            <property>
                                <name>listener</name>
                                <value>org.uncommons.reportng.HTMLReporter,
                                    org.uncommons.reportng.JUnitXMLReporter
                                </value>
                            </property>
                        </properties>      
                        <forkMode>always</forkMode>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>3.2.1</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <transformers>
                                    <transformer
                                            implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                        <resource>META-INF/spring.handlers</resource>
                                    </transformer>
                                    <transformer
                                            implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
                                        <resource>META-INF/spring.factories</resource>
                                    </transformer>
                                    <transformer
                                            implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                        <resource>META-INF/spring.schemas</resource>
                                    </transformer>
                                    <transformer
                                            implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                                    <transformer
                                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                        <mainClass>${start-class}</mainClass>
                                    </transformer>
                                </transformers>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>
    

    这是主函数

    package org.kuohai;
    
    public class TestNGMain {
        public static void main(String[] args) {
            org.testng.TestNG.main(args);
        }
    }
    

    然后你可以使用:

    mvn clean package -DskipTests
    

    打包运行

    java -jar packageName.jar -testjar packageName.jar 
    

    如果还有其他问题,可以跑java -jar packageName.jar

    你可以看到消息。

    【讨论】:

      猜你喜欢
      • 2017-12-31
      • 2020-04-08
      • 1970-01-01
      • 2020-07-05
      • 2019-07-14
      • 2019-03-15
      • 1970-01-01
      • 2015-02-02
      • 1970-01-01
      相关资源
      最近更新 更多