1 Maven Command (mvn)
The basic syntax for running Maven is:
mvn [options] [<plugin:goal>] [<phase>]
The execution order depends on how goals and build phases are specified on the command line.
2 Running Maven Phases
Example: Compile a project
mvn clean compile
mvn clean test
Suppose you want to experiment with a new Java feature. You create a Maven project with source files and unit/integration tests.
- to compile the project, run
mvn clean compile. - to compile and run tests, use
mvn clean test.
Example: Package a project
mvn clean package
This command compiles the code, runs tests, and packages the application into a distributable format such as a JAR or WAR.
Example: Run a Spring Web application
For example, assume you created a Spring Boot web application named my-spring-webapp.
- to compile the project and produce and artifact, run
mvn clean packagefrom the project root. - to run the application, execute:
java -jar target/my-spring-webapp-1.0.0.jarfrom the project root.
The embedded Tomcat server starts on port 8080. Open your browser and go to http://localhost:8080.
Example: Install to the local repository
mvn clean install
This is one of the most common Maven commands. It builds the project and installs the generated artifact into your local repository, making it available for other local projects.
Maven executes all default's lifecycle phases up to install.
Example: Deploy to a shared repository
mvn clean deploy
In a build environment, this command cleans, builds, and deploys artifacts to a remote (shared) repository. In multi-module projects, Maven processes each submodule in order and deploys their artifacts.
3 Running Maven Goals
A build phase can have zero or more goals bound to it. If no goals are bound, the phase does nothing. If goals are bound, all of them are executed when the phase runs.
Example: Generate a project using the Archetype plugin
mvn archetype:generate
Example: Run Checkstyle
mvn checkstyle:check
This command runs the check goal of the Maven Checkstyle plugin. It analyzes your source code to ensure it follows a defined coding standard (style rules). Checkstyle verifies aspects such as: code formatting (indentation, spacing, line length), naming conventions (classes, methods, variables) and code structure and organization.
Example: Execute a goal before packaging
mvn clean dependency:copy-dependencies package
In this command:
cleanandpackageare lifecycle phasesdependency:copy-dependenciesis a plugin goal
Execution order:
- First, the
cleanphase runs (including its lifecycle) - Next, the
dependency:copy-dependenciesgoal executes - Finally, the
packagephase runs (including all preceding phases)
Maven Command Cheat Sheet
| Command | Description | Typical Use Case |
|---|---|---|
mvn clean |
Deletes the target directory (previous build output). |
Start a fresh build. |
|
Cleans previous output and compiles the project. | Rebuild source code from scratch. |
mvn clean test |
Cleans, compiles, and runs tests. | Full verification of code and tests. |
mvn clean package |
Clean, compiles, tests, and packages the application. | Prepare application for execution or distribution. |
mvn clean install |
Full clean build and install into local repository. | Most common development build command. |
mvn clean deploy |
Clean build and deploy to remote repository. | Share builds with teams or CI/CD systems. |
mvn validate |
Validates project structure and configuration. | Early sanity check before building. |
mvn verify |
Runs checks to verify the package is valid. | Final validation before install/deploy. |
mvn site |
Generates project documentation. | Create project reports and documentation site. |
No comments:
Post a Comment