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

pra*_*eel 24 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
Run Code Online (Sandbox Code Playgroud)

在运行相同命令的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
Run Code Online (Sandbox Code Playgroud)

pra*_*eel 38

我跑了 -

mvn clean install.
Run Code Online (Sandbox Code Playgroud)

当我跑的时候发布 -

mvn -X clean exec:java -Dexec.mainClass="com.shn.library.RunSuitesInParallel" -Dexec.classpathScope=test -e
Run Code Online (Sandbox Code Playgroud)

编译的类被删除,错误很明显.

所以解决方案是 -

mvn -X clean install exec:java -Dexec.mainClass="com.shn.library.RunSuitesInParallel" -Dexec.classpathScope=test -e
Run Code Online (Sandbox Code Playgroud)


pri*_*ime 10

虽然接受的答案很好,但这也可能对某人有所帮助.

在运行依赖于编译类的任何插件目标之前,您似乎需要确保构建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>
Run Code Online (Sandbox Code Playgroud)

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

mvn clean install
Run Code Online (Sandbox Code Playgroud)

然后

mvn exec:java
Run Code Online (Sandbox Code Playgroud)

或做,

mvn install exec:java
Run Code Online (Sandbox Code Playgroud)


Noa*_*nos 8

使用exec-maven-plugin时,最有可能解决ClassNotFoundException,将默认的类路径范围(src\main\java)更改为测试类路径(src\test\java).

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

<classpathScope>test</classpathScope>
Run Code Online (Sandbox Code Playgroud)

例如:

<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>
Run Code Online (Sandbox Code Playgroud)