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