在开始集成测试之前运行“ Main”类

时间:2019-08-29 09:16:40

标签: maven maven-failsafe-plugin exec-maven-plugin

我有一些集成测试,我想对在main方法中启动的自定义服务器运行。 我在pom.xml中有以下内容:

<project>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <execution>
                    <id>start the server for integration tests</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                    <configuration>
                        <async>false</async>
                        <executable>java</executable>
                        <arguments>
                        <argument>-classpath</argument>
                        <classpath/>
                            <argument>com.abc.def.integration.Main</argument>
                        </arguments>
                    </configuration>
                </execution>
                <execution>
                    <phase>post-integration-test</phase>
                    <goals/>
                </execution>
           </plugin>
        </plugins>
    </build>
</project>

稍后将使用故障安全插件执行测试。 但目前我正在为Main类获取ClassNotFoundException:

INFO] --- exec-maven-plugin:1.6.0:exec (start the server for integration tests) @ rest ---
Error: Could not find or load main class com.abc.def.integration.Main
Caused by: java.lang.ClassNotFoundException: com.abc.def.integration.Main
[ERROR] Command execution failed.
org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
        at org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:404)
        at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:166)
        at org.codehaus.mojo.exec.ExecMojo.executeCommandLine(ExecMojo.java:804)
        at org.codehaus.mojo.exec.ExecMojo.executeCommandLine(ExecMojo.java:751)
        at org.codehaus.mojo.exec.ExecMojo.execute(ExecMojo.java:313)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:154)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:146)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
        at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:309)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:194)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:107)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:993)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:345)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:191)
        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:606)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.161 s
[INFO] Finished at: 2019-08-29T14:10:46+05:30
[INFO] Final Memory: 47M/506M
[INFO] ------------------------------------------------------------------------

这看起来像是类路径问题,但是Main类是maven正在编译的项目的一部分。

有没有一种方法可以执行Main方法而无需在构建过程中手动指定类路径? (如无:-Dexec.args =“%classpath”) 我应该使用另一个Maven插件执行此类吗?

1 个答案:

答案 0 :(得分:0)

我通过http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/

中描述的方法解决了该问题。

在下面更改为执行部分:

<execution>
        <id>start the server for integration tests</id>
        <phase>pre-integration-test</phase>
        <goals>
                <goal>java</goal>
        </goals>
        <configuration>
                <classpathScope>test</classpathScope>
                <mainClass>com.abc.def.integration.Main</mainClass>
        </configuration>
</execution>