Apache Maven Build Automation Tool Commands
Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information. Maven can also be used to build and manage projects written in C, C#, Ruby, Scala, etc.,
Maven Lifecycle
It has 3 built-in lifecycle's. Each lifecycle has a sequence of phases which are executed in a specific order. A maven phase represents a stage in the maven build lifecycle. Each phase is responsible for a specific task.
Default
-
It is the main build lifecycle.
-
It has 23 phases.
-
The following is a list of some important phases in the Default build lifecycle.
- validate - Checks if all the information necessary for the build are available.
- compile - Compiles the source code of the project.
test-compile
compiles the test source code. - test - Runs the unit tests of the application.
- package - Packages the compiled source code into a distributable format (WAR/JAR).
- install - Install the package to a local repository.
- deploy - The last phase in the default build lifecycle copies the package to a remote repository.
Clean
-
It cleans the project and removes the files from the previous build.
-
It has 3 phases
- pre-clean
- clean
- post-clean
Site
-
Creates project's site documentation.
-
It has 4 phases
- pre-site
- site
- post-site
- site-deploy
Maven Goals
Each maven phase is a sequence of goals. Each goal is responsible for a specific task. When a phase is run, all goals bound to this phase is executed in order.
Maven Plugin
A maven plugin is a group of goals. All execution in maven is done by plugins. A plugin is mapped to a goal and executed as apart of it. Phase is mapped to multiple goals. These goals are executed by a plugin. A plugin configuration can be modified using the plugin declaration.
The following is a non-exhaustive list of commonly used Maven Commands for quick reference.
Quick Links :
Maven Dependency Scopes mapped with phases
- compile (maven default scope) - build, test, and run
- provided - build and test
- runtime - test and run
- test - compile and test
- system - similar to ones with scope provided
- import
Packaging
-
mvn package -Dmaven.test.skip=true
The project will be built without compiling the tests. -
mvn clean package
Compiles the code and also packages it.
Running Tests
-
mvn test
Run all the unit test classes. -
mvn -Dtest=TestApp1 test
Run a single test class. -
mvn -Dtest=TestApp1,TestApp2 test
Run multiple test classes. -
mvn -Dtest=TestApp1#methodname test
Run a single test method from a test class. -
mvn -Dtest=TestApp1#testHello* test
Run all test methods that match pattern 'testHello*' from a test class. -
mvn -Dtest=TestApp1#testHello*+testMagic* test
Run all test methods match pattern 'testHello*' and 'testMagic*' from a test class.
Others
-
mvn cobertura:cobertura
Generates Cobertura code coverage reports. -
mvn sonar:sonar
Run Sonar Code analysis.