maven testNG打成jar包运行报错repackage failed: Unable to find main class

本文详细介绍了如何使用Maven和TestNG在Spring Boot项目中打包jar包,并解决了执行测试时遇到的问题,包括跳过测试和指定测试文件。解决方案包括创建启动类和调整pom.xml配置以确保测试代码正确执行。
摘要由CSDN通过智能技术生成

一.maven testNG如何打jar包

1.pom文件引入插件

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.7.1</version>
                <configuration>
                    <suitXmlFiles>
                        <suitXmlFile>
                        <!--相对路径 -->
                            ./src/main/resources/testng.xml
                        </suitXmlFile>
                    </suitXmlFiles>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中 -->
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

    </build>

2.切换到项目目录下,执行mvn clean package命令

darli@LAPTOP-LMMPH4VI MINGW64 ~/.ssh/workspace/muke/AutoTest/Chapter12/target (master)
$ cd ..

darli@LAPTOP-LMMPH4VI MINGW64 ~/.ssh/workspace/muke/AutoTest/Chapter12 (master)
$ mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------< com.course.code:Chapter12 >----------------------
[INFO] Building Chapter12 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
Downloading from spring-milestone: https://repo.spring.io/milestone/org/apache/maven/plugins/maven-resources-plu
gin/2.4.3/maven-resources-plugin-2.4.3.pom
Downloading from spring-snapshot: https://repo.spring.io/snapshot/org/apache/maven/plugins/maven-resources-plugi
n/2.4.3/maven-resources-plugin-2.4.3.pom
......
......
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ Chapter12 ---
[INFO] Building jar: C:\Users\darli\.ssh\workspace\muke\AutoTest\Chapter12\target\Chapter12-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.0.0.RELEASE:repackage (default) @ Chapter12 ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  40.349 s
[INFO] Finished at: 2021-06-11T21:15:39+08:00
[INFO] ------------------------------------------------------------------------

3.进入target目录,执行命令

darli@LAPTOP-LMMPH4VI MINGW64 ~/.ssh/workspace/muke/AutoTest/Chapter12/target (master)

$ ll

total 48

-rw-r--r-- 1 darli 197609 39401  6月 11 19:37 Chapter12-1.0-SNAPSHOT.jar

drwxr-xr-x 1 darli 197609     0  6月 11 19:56 classes

drwxr-xr-x 1 darli 197609     0  6月 11 19:37 generated-sources

drwxr-xr-x 1 darli 197609     0  6月 11 19:37 maven-archiver

drwxr-xr-x 1 darli 197609     0  6月 11 19:37 maven-status

drwxr-xr-x 1 darli 197609     0  6月 11 19:37 surefire

drwxr-xr-x 1 darli 197609     0  6月 11 19:37 surefire-reports



darli@LAPTOP-LMMPH4VI MINGW64 ~/.ssh/workspace/muke/AutoTest/Chapter12/target (master)

$ java -jar Chapter12-1.0-SNAPSHOT.jar

Chapter12-1.0-SNAPSHOT.jar中没有主清单属性

4.偶然发现再次运行又报错

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.0.0.RELEASE:repackage (defaul
t) on project Chapter12: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:2.0.0.RELEA
SE:repackage failed: Unable to find main class -> [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/PluginExecutionException

二.问题解决【方式1】

1.尝试在com.course下新建一个启动类

package com.course;

import org.testng.TestNG;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

import java.util.ArrayList;
import java.util.List;

public class Main {
            public static void main(String[] args) {
                XmlSuite suite = new XmlSuite();
                suite.setName("用户管理系统测试套件");
                List<XmlClass> classes = new ArrayList<>();
                classes.add(new XmlClass("com.course.cases.LoginTest"));
                classes.add(new XmlClass("com.course.cases.AddUserTest"));
                classes.add(new XmlClass("com.course.cases.GetUserInfoTest"));
                classes.add(new XmlClass("com.course.cases.GetUserListTest"));
                classes.add(new XmlClass("com.course.cases.UpdateUserInfoTest"));
                XmlTest test = new XmlTest(suite);
                test.setName("用户管理系统测试");
                test.setXmlClasses(classes);
                List<XmlSuite> suites = new ArrayList<>();
                suites.add(suite);

                TestNG tng = new TestNG();
                tng.setXmlSuites(suites);
                tng.run();
     }
}

再次执行打包命令mvn clean package

[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ Chapter12 ---
[INFO] Building jar: C:\Users\darli\.ssh\workspace\muke\AutoTest\Chapter12\target\Chapter12-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.0.0.RELEASE:repackage (default) @ Chapter12 ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  5.931 s
[INFO] Finished at: 2021-06-11T21:50:34+08:00
[INFO] ------------------------------------------------------------------------

再次执行测试命令

......
22:04:45.421 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Opening JDBC Connection
22:04:45.424 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - Created connection 1908923184.
22:04:45.425 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Setting autocommit to false on JD
BC Connection [com.mysql.cj.jdbc.ConnectionImpl@71c7db30]
22:04:45.425 [main] DEBUG com.course.model.getUpdateUserInfo - ==>  Preparing: select * from user where id=? and
 username=? and age=?
22:04:45.425 [main] DEBUG com.course.model.getUpdateUserInfo - ==> Parameters: 12(Integer), 小兰9(String), 19(St
ring)
22:04:45.425 [main] DEBUG com.course.model.getUpdateUserInfo - <==      Total: 1
db is:User(id=12, userName=小兰9, password=111, age=19, sex=1, permission=14, isDelete=0)
22:04:45.427 [main] INFO com.course.cases.UpdateUserInfoTest - ********updateUserInfo api 验证通过*********

===============================================
用户管理系统测试套件
Total tests run: 7, Failures: 0, Skips: 0
===============================================

-------------》main函数代码执行

darli@LAPTOP-LMMPH4VI MINGW64 ~/.ssh/workspace/muke/AutoTest/Chapter12/target (master)


测试代码成功启动且测试用例执行通过;

这里默认执行了Main类的main方法(这个方法的作用和testng.xml作用一样的,都是运行测试用例代码)

三.问题解决【方式2】

1.修改pom文件如下:

<!--    打包插件-->
    <build>
        <plugins>
            <!--maven-surefire-plugin执行test测试包-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.7.1</version>
                <configuration>
                    <!--跳过测试代码执行-->
                    <!--<skipTests>true</skipTests>-->
                    <!--忽略测试代码执行失败-->
                    <testFailureIgnore>true</testFailureIgnore>
                    <suiteXmlFiles>
                        <suiteXmlFile>
                            ./src/main/resources/testng.xml
                        </suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
            <!-- maven 打依赖jar包 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

2.再次执行mvn clean package

........
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "http://testng.org/test
ng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected.
10:11:08.314 [main] DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ib
atis.logging.slf4j.Slf4jImpl' adapter.
....
10:11:09.325 [main] DEBUG com.course.model.getUpdateUserInfo - ==>  Preparing: select * from user where id=? an
d is_delete=?
10:11:09.325 [main] DEBUG com.course.model.getUpdateUserInfo - ==> Parameters: 13(Integer), 1(String)
10:11:09.325 [main] DEBUG com.course.model.getUpdateUserInfo - <==      Total: 1
db is:User(id=13, userName=tom, password=1221, age=26, sex=0, permission=1, isDelete=1)
10:11:09.325 [main] INFO com.course.cases.UpdateUserInfoTest - ********deleteUserInfo api 验证通过*********
Tests run: 7, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.112 sec

Results :

Tests run: 7, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ Chapter12 ---
[INFO] Building jar: C:\Users\darli\.ssh\workspace\muke\AutoTest\Chapter12\target\Chapter12-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  11.236 s
[INFO] Finished at: 2021-06-13T10:11:10+08:00
[INFO] ------------------------------------------------------------------------


如图所示在打包过程中执行完所有的testng.xml里面的测试代码

如果是需要用jenkins来部署的话更推荐第二种方式

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值