3 ways to run Java main from Maven__java

Source: Internet
Author: User
3 ways to run Java main from Maven
Overview

Maven Exec plugin lets you run the main method for a Java class in your project, with the project dependencies Automaticall Y included in the classpath. This is article show for you 3 ways of using the MAVEN exec plugin to run Java with code examples. 1) Running from Command line

Since you are is not running your code in a MAVEN phase, the code of the You-need to compile. Remember Exec:java does not automatically compile code your MVN Compile

Once your code is compiled, the following command runs your class without arguments: view plain Copy to Clipboard print? MVN exec:java-dexec.mainclass= "Com.vineetmanohar.module.Main" with arguments: view plain Copy to CLI Pboard print? MVN exec:java-dexec.mainclass= "Com.vineetmanohar.module.Main"-dexec.args= "arg0 arg1 arg2" with runtime Depen Dencies in the CLASSPATH: View plain copy to clipboard print? MVN exec:java-dexec.mainclass= "Com.vineetmanohar.module.Main"-dexec.classpathscope=runtime 2) Running in a phase In Pom.xml

You can also run the main method in a Maven phase. For example, your can run the Codegenerator.main () method As part of Thetest phase. View plain Copy to clipboard print? <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>  

To run the Exec plugin with above configuration, simply run the corresponding phase. MVN Test 3) Running in a profiles in Pom.xml

Can also run the main method in a different profile. Simply wrap the above config in the <profile> tag. View plain Copy to clipboard print? <profiles> <profile> <id>code-generator</id> <build> <plugins> & Lt;plugin> <groupId>org.codehaus.mojo</groupId> <artifactid>exec-maven-plugin</artif Actid> <version>1.1.1</version> <executions> <

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.