在 Maven 中创建可执行 test-jar 时出错

发布于 2024-12-18 21:43:57 字数 1790 浏览 2 评论 0原文

我正在尝试创建一个 JAR,我可以将其放在非开发计算机上并运行一些使用 Web 服务的单元测试。我读到有很多方法可以在 Maven 中做到这一点。我专注于使用 test-jar 目标,因为这似乎是最合乎逻辑的方法。

我的测试位于一个单独的 Maven 项目中,其文件夹结构为:

/myProject/src/test/java/org/testplatform/services/test/<name of testng test>.java

这是我在 pom 中的测试 jar 的配置:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <executions>
              <execution>
                <goals>
                  <goal>test-jar</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>org/testplatform/services/test/MainTestClass.java</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

当我执行生成的 JAR 时,我收到此错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/testng/ITestListener
Caused by: java.lang.ClassNotFoundException: org.testng.ITestListener
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

有人可以帮助我了解我需要在中进行哪些更改吗?我的 pom 和/或测试创建一个独立的可执行 JAR 来运行 testng 单元测试?演示项目会很有帮助。

I'm trying to create a JAR that I can put on a non development machine and run some unit tests that exercise a web service. I've read that there are many ways to do this in maven. I'm focusing on using the test-jar goal because that seems the most logical approach.

My tests are in a separate maven project with a folder structure of:

/myProject/src/test/java/org/testplatform/services/test/<name of testng test>.java

Here is my config for the test-jar in the pom:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <executions>
              <execution>
                <goals>
                  <goal>test-jar</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>org/testplatform/services/test/MainTestClass.java</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

When I execute the resulting JAR I get this error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/testng/ITestListener
Caused by: java.lang.ClassNotFoundException: org.testng.ITestListener
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

Can someone help me understand what changes I need to make in my pom and/or tests to create an independent, executable JAR for running testng unit tests? A demo project would be helpful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美人如玉 2024-12-25 21:43:57

该错误表明您正在使用 TestNG 类,并且这些类在生成的类路径中不可用。这是可能的,因为您 test 限定了 testNG 依赖项的范围。

<dependency>
  <groupId>...</groupId>
  <artifactId>...</artifactId>
  <version>...</version>
  <scope>test</scope>           <<< Test scope
</dependency>

这将导致从生成的 JAR 中排除此依赖项。

如果您还没有阅读过,我建议您阅读 Maven 的深入内容解释构建测试 JAR 的推荐方法以及要避免的陷阱。

您已采取正确的步骤将测试类提取到单独的项目中。这确保您可以利用 Maven 的传递依赖项。您还需要将依赖项移出测试范围。

The error is suggesting that you are using TestNG classes and that these aren't available in the resulting classpath. This is possible because you test scoped your testNG dependency

<dependency>
  <groupId>...</groupId>
  <artifactId>...</artifactId>
  <version>...</version>
  <scope>test</scope>           <<< Test scope
</dependency>

This will result in the exclusion of this dependency from the resulting JAR.

In case you haven't already, I suggest reading Maven's in-depth explanation of the recommended way of building a test JAR and the pitfalls to avoid.

You have the made the correct step of extracting your test classes to a separate project. This ensures that you can leverage Maven's transitive dependencies. You also need to move dependencies out of test scope.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文