javamaven

Maven Run Project


Is there a Maven "phase" or "goal" to simply execute the main method of a Java class? I have a project that I'd like to test manually by simply doing something like "mvn run".


Solution

  • See the exec maven plugin. You can run Java classes using:

    mvn exec:java -Dexec.mainClass="com.example.Main" [-Dexec.args="argument1"] ...
    

    The invocation can be as simple as mvn exec:java if the plugin configuration is in your pom.xml. The plugin site on Mojohaus has a more detailed example.

    <project>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>3.2.0</version>
                    <configuration>
                        <mainClass>com.example.Main</mainClass>
                        <arguments>
                            <argument>argument1</argument>
                        </arguments>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>