Java: Using Maven from the console for running classes, unit tests, checking dependencies

In this short article, I’ll provide some Maven commands that I’ve found helpful.

 

Run single class from src/main/java

mvn exec:java -Dexec.mainClass=this.is.MyClass -Dexec.args="myarg1 'my second arg' myarg3"

Run unit test from src/test/java, all methods decorated with @Test

mvn test -Dtest=this.is.MyTestClass

Run unit test from src/test/java, only methods decorated with @Test and that start with ‘testDatabase’

mvn test -Dtest=this.is.MyTestClass#testDatabase*

List all jar dependencies of project

mvn dependency:build-classpath

List all dependencies of project with custom path prefixed

mvn dependency:build-classpath -Dmdep.prefix=mydir

List all dependencies as tree

mvn dependency:tree

Package without running tests

mvn package -Dmaven.test.skip=true

Just compile, but not package

mvn test-compile

Force update of releases/snapshots from remote repo

mvn update -U

Produce artifact with verbose debug enabled

mvn package -X

 

REFERENCES

http://www.mojohaus.org/exec-maven-plugin/usage.html

https://stackoverflow.com/questions/1873995/run-a-single-test-method-with-maven (unit tests)

http://maven.apache.org/plugins-archives/maven-surefire-plugin-2.12.4/examples/single-test.html (unit tests)

https://stackoverflow.com/questions/9846046/run-main-class-of-maven-project/9846103 (main class and pom.xml for params)

http://books.sonatype.com/mvnref-book/reference/running-sect-options.html (lots of good maven commands)