Categories
Maven

Running classes via maven from console

If you use maven to compile and handle your applications, you may want to directly call it from console. Here you can use the Maven-Exec-Plugin. This plugin provides an easy method to execute Java-Classes. Of course you can configure the plugin as usual in you pom.xml, but you also can directly use it from the console.

To run a simple class you can use:

1
mvn exec:java -Dexec.mainClass=info.javahelp.examples.Example

The advantage is that java is correctly called with all used jar-dependencies and the resulting classpath.

To affect the used dependencies you can also define the used scope (default: compile). For example your application is in testing scope you can run it with:

1
mvn exec:java -Dexec.mainClass=info.javahelp.examples.tests.Example -Dexec.classpathScope=test