Maven Tutorial - Maven Run Java Main








After packaging the source to a Jar file we can use the following three ways to run the Java main method.

We can use Maven exec plugin to run the main method of a Java class, with the project dependencies automatically included in the classpath.

Running from Command line

Suppose we have the project created in the previous chapters. To run the Java main method from Maven, we can use the following command.

mvn exec:java -Dexec.mainClass="com.java2s.ide.App"  

The code above generates the following result.

c:\mvn_test\xmlFileEditor>mvn exec:java -Dexec.mainClass="com.java2s.ide.App"
[INFO] Scanning for projects...
Downloading: https://repo.maven.apache.org/maven2/org/codehaus/mojo/exec-maven-plugin/maven-metadata.xml
...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building xmlFileEditor 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.3.2:java (default-cli) @ xmlFileEditor ---
Downloading: https://repo.maven.apache.org/maven2/junit/junit/4.11/junit-4.11.pom
...
[WARNING] Warning: killAfter is now deprecated. Do you need it ? Please comment on MEXEC-6.
Hello World!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.847 s
[INFO] Finished at: 2014-11-03T16:25:54-08:00
[INFO] Final Memory: 22M/369M
[INFO] ------------------------------------------------------------------------
c:\mvn_test\xmlFileEditor>

With arguments:

mvn exec:java -Dexec.mainClass="com.java2s.ide.App" -Dexec.args="arg0 arg1 arg2"

With runtime dependencies in the CLASSPATH:

mvn exec:java -Dexec.mainClass="com.java2s.ide.App" -Dexec.classpathScope=runtime




Running in a phase in pom.xml

We can run the main method in a maven phase. For example, you can run the App.main() method as part of the test phase.

<build>  
 <plugins>  
  <plugin>  
   <groupId>org.codehaus.mojo</groupId>  
   <artifactId>exec-maven-plugin</artifactId>  
   <version>1.1.1</version>  
   <executions>  
    <execution>  
     <phase>test</phase>  
     <goals>  
      <goal>java</goal>  
     </goals>  
     <configuration>  
      <mainClass>com.java2s.ide.App</mainClass>  
      <arguments>  
       <argument>arg0</argument>  
       <argument>arg1</argument>  
      </arguments>  
     </configuration>  
    </execution>  
   </executions>  
  </plugin>  
 </plugins>  
</build>  

To run the exec plugin with above configuration, simply run the corresponding phase.

mvn test 




Running in a profile in pom.xml

We can run the main method in a different profile. wrap the above config in the <profile> tag.

<profiles>  
 <profile>  
  <id>code-generator</id>  
  <build>  
   <plugins>  
    <plugin>  
     <groupId>org.codehaus.mojo</groupId>  
     <artifactId>exec-maven-plugin</artifactId>  
     <version>1.1.1</version>  
     <executions>  
      <execution>  
       <phase>test</phase>  
       <goals>  
        <goal>java</goal>  
       </goals>  
       <configuration>  
        <mainClass>com.java2s.ide.App</mainClass>  
        <arguments>  
         <argument>arg0</argument>  
         <argument>arg1</argument>  
        </arguments>  
       </configuration>  
      </execution>  
     </executions>  
    </plugin>  
   </plugins>  
  </build>  
 </profile>  
</profiles>  

To call the above profile, run the following command:

mvn test -Pcode-generator