29

我正在尝试运行 TestNG 测试。我的项目组织是 - src->test->java->com->shn->library 以下命令在 Windows 中运行良好,但在 Linux 中失败。

mvn -X clean exec:java -Dexec.mainClass="com.shn.library.RunSuitesInParallel" -Dexec.classpathScope=test -e

在 Linux 中运行相同命令时出现错误 -

[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli) on project UAF: An exception occured while executing the Java class. com.shn.library.RunSuitesInParallel -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli) on project UAF: An exception occured while executing the Java class. com.shn.library.RunSuitesInParallel
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:217)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
Caused by: org.apache.maven.plugin.MojoExecutionException: An exception occured while executing the Java class. com.shn.library.RunSuitesInParallel
        at org.codehaus.mojo.exec.ExecJavaMojo.execute(ExecJavaMojo.java:352)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
        ... 19 more
Caused by: java.lang.ClassNotFoundException: com.shn.library.RunSuitesInParallel
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
        at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:285)
        at java.lang.Thread.run(Thread.java:722)
[ERROR]
[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/MojoExecutionException
4

4 回答 4

46

我跑了——

mvn clean install.

当我跑步时发布 -

mvn -X clean exec:java -Dexec.mainClass="com.shn.library.RunSuitesInParallel" -Dexec.classpathScope=test -e

编译的类被删除并且错误很明显。

所以解决方案是 -

mvn -X clean install exec:java -Dexec.mainClass="com.shn.library.RunSuitesInParallel" -Dexec.classpathScope=test -e
于 2013-07-04T10:04:40.120 回答
20

尽管接受的答案很好,但这也可能对某人有所帮助。

看来您需要确保在运行任何依赖于已编译类的插件目标之前构建 Maven 项目。

如果您创建一个新的 java 类,当您要使用插件目标时,ClassNotFoundException由于没有该类的编译版本(插件依赖于该类的编译版本)而被抛出。

假设您在 pom.xml 中有一个如下所示的插件配置(注意:原始 SO 问题它提到直接运行主类而不在 pom.xml 中指定它,以及如何做到这一点在praneel接受的答案中进行了解释),

<plugin>
     <groupId>org.codehaus.mojo</groupId>
     <artifactId>exec-maven-plugin</artifactId>
     <version>1.2.1</version>
     <configuration>                 
         <mainClass>com.myproj.java.Main</mainClass>
     </configuration>
</plugin>

所以在你运行任何插件目标之前,做

mvn clean install

然后

mvn exec:java

或者做,

mvn install exec:java
于 2017-02-16T06:56:25.700 回答
15

使用exec-maven-plugin时对ClassNotFoundException的解决方法很可能是将默认类路径范围(src\main\java) 更改为您的测试类路径(src\test\java)。

它可以在 mvn 命令 ( -Dexec.classpathScope="test" ) 或 pom.xml 中传递:

<classpathScope>test</classpathScope>

例如:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.6.0</version>
        <executions>
            <execution>
                <phase>test-compile</phase>
                <goals>
                    <goal>java</goal>
                </goals>
                <configuration>
                    <mainClass>your.package.test.class</mainClass>
                    <arguments>
                        ...
                    </arguments>
                    <classpathScope>test</classpathScope>
                </configuration>
            </execution>
        </executions>
</plugin>
于 2017-10-25T15:53:05.697 回答
0

ClassNotFoundException在尝试使用exec:javaJenkins 的插件时,我们遇到了同样的问题。我们的编译是在使用插件之前发生的,但我们仍然看到异常:

[INFO] --- exec-maven-plugin:3.0.0:java (default-cli) @ org.mycompany.myapp ---
[WARNING] 
java.lang.ClassNotFoundException: "org.mycompany.myapp.MyAppEntry"
    at java.net.URLClassLoader.findClass (URLClassLoader.java:610)
    at java.lang.ClassLoader.loadClassHelper (ClassLoader.java:945)
    at java.lang.ClassLoader.loadClass (ClassLoader.java:890)
    at java.lang.ClassLoader.loadClass (ClassLoader.java:873)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:246)
    at java.lang.Thread.run (Thread.java:825)

检查 Jenkins 工作区显示该类已按预期编译,所以我们很困惑。

问题原来是如何从 Jenkins 传递属性。我们在 Maven 构建步骤的“属性”字段中添加了引号:

exec.mainClass="org.mycompany.myapp.MyAppEntry"

解决方案是从 Maven 构建步骤属性配置中删除引号

exec.mainClass=org.mycompany.myapp.MyAppEntry
于 2021-09-27T17:05:29.170 回答