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

前端 未结 4 1926
伪装坚强ぢ
伪装坚强ぢ 2020-12-30 18:36

I have inherited a codebase :)

Under src/test/java/ there\'s a file that I need to run (I need to run its public static void main(String[] args), not a

4条回答
  •  情话喂你
    2020-12-30 19:16

    (...) 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.

提交回复
热议问题