3 ways to run Java main using Maven

Source: Internet
Author: User

3 ways to run Java main using Maven Originalhttp://blog.csdn.net/qbg19881206/article/details/19850857 ThemesMaven

MAVEN uses the Exec plugin to run the Java Main method, and here are 3 different ways to do it.

One, run from the command line

1, compile the code before running, Exec:java will not automatically compile the code, you need to manually execute MVN compile to complete the compilation.

MVN Compile

2. After compiling, execute exec to run the main method.

There is no need to pass parameters:

MVN exec:java-dexec.mainclass= "Com.vineetmanohar.module.Main"

Parameters need to be passed:

MVN exec:java-dexec.mainclass= "Com.vineetmanohar.module.Main"-dexec.args= "arg0 arg1 arg2"

Specify run-time dependencies on classpath:

MVN exec:java-dexec.mainclass= "Com.vineetmanohar.module.Main"-dexec.classpathscope=runtime

Second, specify a stage to execute in Pom.xml

<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.vineetmanohar.module.codegenerator</mainclass>      <arguments>       <argument>arg0</ argument>       <argument>arg1</argument>      </arguments>     </configuration>    </execution>   </executions>  </plugin> </plugins></build>

Binding the execution of the Codegenerator.main () method to the test phase of MAVEN, the following command allows you to execute the Main method:

MVN test

Third, specify a configuration in Pom.xml to perform

<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.vineetmanohar.module.codegenerator</ mainclass>        <arguments>         <argument>arg0</argument>         <argument>arg1</ argument>        </arguments>       </configuration>      </execution>     </executions >    </plugin>   </plugins>  </build> </profile></profiles>

After wrapping the configuration in 2 with the <profile> tag, you can execute the main method by specifying the configuration file, as follows:

MVN Test-pcode-generator

Note: Additional configuration parameter descriptions for MVN exec can be obtained from the following command.

MVN Exec:help-ddetail=true-dgoal=java

3 ways to run Java main using Maven

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.