在 Maven 中创建可执行 test-jar 时出错
我正在尝试创建一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该错误表明您正在使用 TestNG 类,并且这些类在生成的类路径中不可用。这是可能的,因为您
test
限定了 testNG 依赖项的范围。这将导致从生成的 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 dependencyThis 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.