run main class of Maven project [duplicate] run main class of Maven project [duplicate] java java

run main class of Maven project [duplicate]


Try the maven-exec-plugin. From there:

mvn exec:java -Dexec.mainClass="com.example.Main"

This will run your class in the JVM. You can use -Dexec.args="arg0 arg1" to pass arguments.

If you're on Windows, apply quotes for exec.mainClass and exec.args:

mvn exec:java -D"exec.mainClass"="com.example.Main"

If you're doing this regularly, you can add the parameters into the pom.xml as well:

<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>com.example.Main</mainClass>    <arguments>      <argument>foo</argument>      <argument>bar</argument>    </arguments>  </configuration></plugin>


Although maven exec does the trick here, I found it pretty poor for a real test. While waiting for maven shell, and hoping this could help others, I finally came out to this repo mvnexec

Clone it, and symlink the script somewhere in your path. I use ~/bin/mvnexec, as I have ~/bin in my path. I think mvnexec is a good name for the script, but is up to you to change the symlink...

Launch it from the root of your project, where you can see src and target dirs.

The script search for classes with main method, offering a select to choose one (Example with mavenized JMeld project)

$ mvnexec  1) org.jmeld.ui.JMeldComponent 2) org.jmeld.ui.text.FileDocument 3) org.jmeld.JMeld 4) org.jmeld.util.UIDefaultsPrint 5) org.jmeld.util.PrintProperties 6) org.jmeld.util.file.DirectoryDiff 7) org.jmeld.util.file.VersionControlDiff 8) org.jmeld.vc.svn.InfoCmd 9) org.jmeld.vc.svn.DiffCmd10) org.jmeld.vc.svn.BlameCmd11) org.jmeld.vc.svn.LogCmd12) org.jmeld.vc.svn.CatCmd13) org.jmeld.vc.svn.StatusCmd14) org.jmeld.vc.git.StatusCmd15) org.jmeld.vc.hg.StatusCmd16) org.jmeld.vc.bzr.StatusCmd17) org.jmeld.Main18) org.apache.commons.jrcs.tools.JDiff#? 

If one is selected (typing number), you are prompt for arguments (you can avoid with mvnexec -P)

By default it compiles project every run. but you can avoid that using mvnexec -B

It allows to search only in test classes -M or --no-main, or only in main classes -T or --no-test. also has a filter by name option -f <whatever>

Hope this could save you some time, for me it does.