Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easier way to run a Java application from Maven?

Tags:

java

maven

If I have understood correctly, I need to type this to run my project from maven:

mvn compile
mvn exec:java -Dexec.mainClass="com.foo.bar.blah.Main"

Is there a way I can make this simpler? Optimally I would like to just do

mvn run
like image 537
Svish Avatar asked Jan 25 '11 10:01

Svish


People also ask

What is mvn exec Java?

Maven exec plugin allows us to execute system and Java programs from the maven command. There are two goals of the maven exec plugin: exec:exec - can be used to execute any program in a separate process.


1 Answers

1) Create a new profile called "run" (or another name of your choice)

  <profiles>
    <profile>
      <id>run</id>

2) Set the profile's default goal to "verify" (or you can choose "install", choosing a phase after compile will ensure that the code will automatically be compiled before running the class)

 <profiles>
    <profile>
      <id>run</id>
      <build>
        <defaultGoal>verify</defaultGoal>

3) Add the exec-maven-plugin to this profile (see this), but configure it to run in the 'verify' phase.

   <execution>  
    <phase>test</phase>  

4) You can now run your class using the following:

mvn -Prun
like image 87
Vineet Manohar Avatar answered Nov 09 '22 04:11

Vineet Manohar