Is there a Maven "phase" or "goal" to simply execute the main method of a Java class? I have a project that I'd like to test manually by simply doing something like "mvn run".
6 Answers
See the exec maven plugin. You can run Java classes using:
mvn exec:java -Dexec.mainClass="com.example.Main" [-Dexec.args="argument1"] ...
The invocation can be as simple as mvn exec:java if the plugin configuration is in your pom.xml. The plugin site on Mojohaus has a more detailed example.
<project>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>com.example.Main</mainClass>
                    <arguments>
                        <argument>argument1</argument>
                    </arguments>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
 
    
    - 5,736
- 9
- 37
- 49
 
    
    - 67,031
- 36
- 206
- 278
- 
                    23this is how an answer should look like! reference + simple example + complex example. the simple example did the trick for me (no more config needed) – codewing Oct 11 '16 at 09:28
- 
                    2is there a newer version of this plugin (newer than 1.2.1?) – Alexander Mills Sep 22 '17 at 22:44
- 
                    1Yes, there's a newer version, so don't copy the fragment above 1-to-1 unless you want to use version 1.2.1 See http://www.mojohaus.org/exec-maven-plugin/usage.html for the latest version As of now it's 1.6.0 – user1053510 Oct 24 '17 at 06:19
- 
                    Thanks, would you also know how to run a main class if in submodule with dependencies in other modules ? – user1767316 Dec 30 '19 at 14:53
- 
                    1This does not actully run the main class – Dexter May 20 '20 at 08:44
1. Edit POM.xml
Add the following property in pom.xml. Make sure you use the fully qualified class name (i.e. with package name) which contains the main method:
<properties>
        <exec.mainClass>fully-qualified-class-name</exec.mainClass>
</properties>
2. Run Command
Now from the terminal, trigger the following command:
mvn clean compile exec:java
NOTE You can pass further arguments via -Dexec.args="xxx" flag.
 
    
    - 14,222
- 20
- 104
- 125
- 
                    1Does this actually work? I've tried it as both:${foo.bar.SomeMainClass} andfoo.bar.SomeMainClass and it doesn't work. Error is the same: [ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java (default-cli) on project newtrex: The parameters 'mainClass' for goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java are missing or invalid -> [Help 1] – Nenad Bulatović Jun 27 '19 at 12:02
- 
                    It works, you may want to check [this](https://stackoverflow.com/questions/19850956/maven-java-the-parameters-mainclass-for-goal-org-codehaus-mojoexec-maven-p) post – Saikat Jun 27 '19 at 14:48
- 
                    1@NenadBulatovic : It works if you substitute ${foo.bar.SomeMainClass} with foo.bar.SomeMainClass -> without the $ or { } – Krishna Santosh Sampath Mar 23 '20 at 14:23
- 
                    @NenadBulatovic the key is, add the @(myProjectId) after exec:java. To sum up: mvn clean compile exec:java@(myProjectId) – Luis Manrique Nov 18 '21 at 09:18
The above mentioned answers are correct but I am simplifying it for noobs like me.Go to your project's pom file. Add a new property exec.mainClass and give its value as the class which contains your main method. For me it was DriverClass in mainpkg. Change it as per your project. 

Having done this navigate to the folder that contains your project's pom.xml and run this on the command prompt mvn exec:java. This should call the main method.
 
    
    - 1,621
- 14
- 26
No need to add new plugin in pom.xml. Just run this command
mvn org.codehaus.mojo:exec-maven-plugin:1.5.0:java -Dexec.mainClass="com.example.Main" | grep -Ev '(^\[|Download\w+:)' 
See the maven exec plugin for more usage.
 
    
    - 4,579
- 3
- 36
- 37
clean package exec:java -P Class_Containing_Main_Method command is also an option if you have only one Main method(PSVM) in the project, with the following Maven Setup.
Don't forget to mention the class in the <properties></properties> section of pom.xml :
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.main.class>com.test.service.MainTester</java.main.class>
</properties>
<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <configuration>
           <mainClass>${java.main.class}</mainClass>
        </configuration>
</plugin>
STS Run Configuration along with above Maven Setup:
 
    
    - 8,561
- 5
- 70
- 76
- 
                    That doesn't look right to me. Isn't -P for 'profile'? If you're specifying it on the command line why do you need to put it in the plugin properties? – Rup Nov 23 '15 at 19:23
- 
                    @Rup Yes, `-P` is for profile. Shared this, as it is also an option of running Maven project. – Abhijeet Nov 24 '15 at 02:29
 
     
     
    