Search code examples
javaeclipsemavenpom.xmlexec-maven-plugin

not able to execute mainClass from pom.xml


I am trying to execute pom.xml having mainClass mentioned in it but it is not being executed.

command : mvn test -f "path_to_pom.xml\pom.xml"

Output:

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Project 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @Project ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] Copying 3 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.3:compile (default-compile) @ Project ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ Project ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Personal\selenium\Project\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.3:testCompile (default-testCompile) @Project ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. b
uild is platform dependent!
[INFO] Compiling 5 source files to D:\Personal\selenium\Project\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ Project ---
[WARNING] useSystemClassloader setting has no effect when not forking
[INFO] Surefire report directory: D:\Personal\selenium\Project\target\surefire-reports
Running test.java.com.utilities.TestController
Configuring TestNG with: TestNG652Configurator
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.295 sec - in t
est.java.com.utilities.TestController

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.138 s
[INFO] Finished at: 2016-03-08T11:45:54+05:30
[INFO] Final Memory: 20M/225M
[INFO] ------------------------------------------------------------------------

I tried executing it from eclipse IDE but is not being executed from there too.

However, specifying -DmainClass="<className>" in the maven command on cmd, class is being executed.

Below is the entry from pom.xml:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.2.1</version>
    <executions>
        <execution>
            <goals>
                <goal>java</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <mainClass>test.java.com.utilities.TestController</mainClass>
    </configuration>
</plugin>

I already did mvn clean and Maven > Update Project but it did not helped.

Question: What other changes can I do to make this work?


Solution

  • You want to execute the class during your test phase, hence you should add it to your plugin execution. Moreover, it seems you are running a class from the test packages which may also need test classpath to properly execute.

    I would then apply the following change to the configuration you posted:

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <executions>
            <execution>
                <phase>test</phase> <!-- ADDED -->
                <goals>
                    <goal>java</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <mainClass>test.java.com.utilities.TestController</mainClass>
            <classpathScope>test</classpathScope> <!-- ADDED -->
        </configuration>
    </plugin>
    

    Note the ADDED elements highlighted through comments.