2

I have a multi-module Maven project with a master pom and numerous sub-directories each containing projects (and pom.xml files) which refer to the master pom.

In projectA contains an invocation of exec-maven-plugin, which executes the java goal and successfully invokes a test class that resides somewhere in projectA (in projectA/test/com/mycompany/Testclass.java). The plugin is declared as follows: org.codehaus.mojo exec-maven-plugin 1.2.1

                <execution>
                    <id>execute-test</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>java</goal>
                    </goals>
                    <configuration>
                        <mainClass>com.mycompany.Testclass</mainClass>
                        <classpathScope>test</classpathScope>
                    </configuration>
                </execution>
            </executions>
        </plugin>

I execute maven with the following command, and it works fine: mvn verify -P <profile name> -pl projectA -am

I have a second project, projectB, which depends on projectA. I have configured an exec-maven-plugin section in projectB's pom.xml file that is identical to the one above. When I run maven with the same command as above (except with projectB in the -pl parameter), I get the exception java.lang.ClassNotFoundException: com.mycompany.Testclass.

Clearly this is happening because the classpath when I run maven for projectB does not include the directories in projectA, even though the pom.xml has a dependency on projectA (actually it depends on projectA.1, which in turn depends on projectA).

I have tried switching to use the exec goal rather than the java goal, in projectB, and then providing a classpath in the arguments, like this:

                <execution>
                    <id>execute-test</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                    <configuration>
                        <executable>java</executable>
                        <arguments>
                            <argument>-classpath</argument>
                            <argument>"../projectA/target/projectA-6.6.1-bSOURCE-tests.jar;../projectA/target/projectA-6.6.1-bSOURCE.jar"</argument>
                            <argument>com.mycompany.Testclass</argument>
                        </arguments>
                        <classpathScope>test</classpathScope>
                    </configuration>
                </execution>

When I've done that I successfully load the class, but get a ClassNotFoundException on com.google.gson.JsonSyntaxException

That happens to be the same error I get if I run the class from the command line, as follows

java -classpath "projectA/target/projectA-6.6.1-bSOURCE-tests.jar;projectA/target/projectA-6.6.1-bSOURCE.jar" com.mycompany.Testclass

In projectB I think I want to use the java goal of the exec-maven-plugin rather than the exec goal, but one way or the other I have to be able to specify the classpath.

Any ideas how I can do that?

Thanks.

alexbt
  • 16,415
  • 6
  • 78
  • 87

1 Answers1

0

I figured this out myself, and am posting my answer in case anybody else needs it, or in case anybody wants to dispute it (or suggest something better).

What I did was add a dependency in the subordinate project, projectB, as follows:

    <dependency>
        <groupId>com.mycompany</groupId>
        <artifactId>projectA</artifactId>
        <version>6.6-SNAPSHOT</version>
        <scope>test</scope>
        <classifier>tests</classifier>
    </dependency>

I believe the clincher was the element, which directed maven to get the dependent classes from the jar file containing the test classes in projectA.