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> <