From Maven, how do I run a class that lives under src/test/java?

Solution 1:

I faced the same problem and figured it out.

Shortly speaking, the class must be compiled before exec:java goal. (It surely works without test-compile phase if the class is already compiled by other user action. Note that Pascal Thivent, in his answer, invoked mvn test before exec:java.)

$ mvn -Dexec.mainClass=... -Dexec.classpathScope=test test-compile exec:java

You can prove it for yourself if you want to see the ClassNotFoundException again.

$ mvn -Dexec.mainClass=... -Dexec.classpathScope=test clean exec:java

Solution 2:

(...) I hope this is clearly explained.

Not bad but I can't reproduce. I created a project:

$ mvn archetype:generate -DgroupId=com.stackoverflow \
                         -DartifactId=Q4060613 \
                         -Dversion=1.0-SNAPSHOT \
                         -DarchetypeArtifactId=maven-archetype-quickstart 

Then cded into it and created a Dog class (under src/main/java):

$ cd Q4060613
$ cat > src/main/java/com/stackoverflow/Dog.java
package com.stackoverflow;

public class Dog {
    public String bark() {
        return "woof!";
    }
}

and created a Demo class (under src/test/java):

$ cat > src/test/java/com/stackoverflow/Demo.java 
package com.stackoverflow;

public class Demo {
    public static void main(String[] args) {
        System.out.println(new Dog().bark());
    }
}

After compiling the source code, running the command you provided works as expected:

$ mvn test
...
$ mvn exec:java -Dexec.mainClass="com.stackoverflow.Demo" -Dexec.classpathScope="test"
[INFO] Scanning for projects...
...
[INFO] --- exec-maven-plugin:1.2:java (default-cli) @ Q4060613 ---
woof!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
...

Something else must be wrong.

Solution 3:

Include following lines in pom.xml in exec-maven-plugin plugin. <classpathScope>test</classpathScope>

plugin section in POM looks something like this

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>my-execution</id>
            <phase>test</phase>
            <goals>
                <goal>java</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <mainClass>com.example.MainClass</mainClass>
            <classpathScope>test</classpathScope>
    </configuration>
</plugin>

Note : com.example.MainClass is the class containing main method.

Solution 4:

Ok, spurred on by it working for everyone else I dug a little harder. The code wasn't reporting its problems very well and I was misreading the stacktrace.

It does:

FileInputStream is = new FileInputStream("lib/other-thing.jar");

which was failing. I symlinked trunk/src/main/assembly/lib/ into trunk/ and now it works. There might be a neater way to fix that then the symlink, though.

Thank you chaps.