gkns
gkns

Reputation: 720

Running a 'Main' class before starting integration tests

I have a few integration tests that I want to run against a custom server that is started in the main method. I have the below in the 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>

The tests will be later executed using the failsafe plugin. But currently I am getting a ClassNotFoundException for the Main class:

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] ------------------------------------------------------------------------

It looks like a classpath issue, But the Main class is part of the project being complied by the maven.

Is there a way to execute the Main method without manually specifying the classpath during the build? (like without : -Dexec.args="%classpath") Should I be using another maven plugin to execute this class?

Upvotes: 0

Views: 1480

Answers (1)

gkns
gkns

Reputation: 720

I solved the issue by the method described in: http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/

With below change to the execution section:

<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>

Upvotes: 1

Related Questions