How to run main method using Maven
February 16, 2012 12:27:55 Last update: February 16, 2012 12:34:58
Here are some ways to run a main method using Maven:
- Use the exec plugin:
mvn exec:java -Dexec.mainClass="com.example.App"
or, with arguments:mvn exec:java -Dexec.mainClass="com.example.App" -Dexec.args="arg1 'arg 2' 'arg-\"3'"
- Attach it to a build phase with the
buildelement:<build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</version> <executions> <execution> <phase>test</phase> <goals> <goal>java</goal> </goals> <configuration> <mainClass>com.example.App</mainClass> <arguments> <argument>arg1</argument> <argument>arg 2</argument> <argument>'arg" 3'</argument> </arguments> </configuration> </execution> </executions> </plugin> </plugins> </build>
- If you want to run main from Maven, it's probably just some test code. You are better off just to write a test case, or call the main method from a test class:
package com.example; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; /** * Unit test for simple App. */ public class AppTest extends TestCase { /** * Create the test case * * @param testName name of the test case */ public AppTest( String testName ) { super( testName ); } /** * @return the suite of tests being tested */ public static Test suite() { return new TestSuite( AppTest.class ); } /** * Rigourous Test :-) */ public void testApp() { App.main(new String[] {"arg1", "arg 2", "'arg-3\""}); } }