在Linux上运行时,exec-maven-plugin会出现Class Not Found异常

时间:2013-07-03 21:14:51

标签: maven exec-maven-plugin

我正在尝试运行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

3 个答案:

答案 0 :(得分:37)

我跑了 -

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

答案 1 :(得分:9)

虽然接受的答案很好,但这也可能有助于某人。

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

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

假设你的pom.xml中有一个如下所示的插件配置(注意:它提到了关于直接运行主类而没有在pom.xml中指定它的原始SO问题,并解释了如何解释它在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

答案 2 :(得分:6)

使用 exec-maven-plugin 时对 ClassNotFoundException 的解析很可能是 change the default classpath scope (src \ main) \ java),到你的测试类路径(src \ test \ java)。

它可以在mvn命令中传递(-Dexec.classpathScope =&#34; test&#34;),或者在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>