What it does
mvn runs Maven. The project is described in pom.xml: name, version, dependencies and plugins. Maven works in phases that run in order: validate → compile → test → package → verify → install → deploy. Running mvn package runs all phases up to package.
Many company apps and CI pipelines (Jenkins, GitLab) still build Java with Maven, so DevOps engineers meet it often.
How to write it
mvn [OPTIONS] PHASE|PLUGIN:GOAL ...Main parts
Phases
clean- Delete the
target/folder. compile/test- Compile / run the unit tests.
package- Make the JAR or WAR in
target/. verify- Run integration tests and checks.
install- Copy the artifact to the local repository (~/.m2).
deploy- Upload the artifact to the remote repository (Nexus, Artifactory).
dependency:tree- Show all dependencies.
versions:set-DnewVersion=X- Change the project version.
Options 12
Most used
Options
Nothing matches this filter.
Examples 11
$ run as a normal user# run as root (or with sudo)
1Build and test
mvn -B clean verify2Make the JAR without running tests (only when tests ran before)
mvn -B -DskipTests package ls target/*.jar3Build in a container, run on a small image
cat Dockerfile FROM maven:3.9-eclipse-temurin-21 AS build WORKDIR /src COPY pom.xml . RUN mvn -B -ntp dependency:go-offline COPY src ./src RUN mvn -B -ntp -DskipTests package FROM eclipse-temurin:21-jre COPY --from=build /src/target/app.jar /app.jar USER 1000 CMD ["java", "-jar", "/app.jar"]4Upload to Nexus with credentials from settings.xml
mvn -B -s ci-settings.xml deploy5Show the dependency tree
mvn dependency:tree -Dincludes=org.apache.logging.log4j6Build one module of a big project
mvn -B -pl services/orders -am package7Set the release version in CI
mvn -B versions:set -DnewVersion=1.4.0 -DgenerateBackupPoms=false8Use the production profile
mvn -B -P prod package9Faster build on many cores
mvn -B -T 1C verify10Build without internet (all in ~/.m2)
mvn -o package11Check for known vulnerable dependencies (OWASP plugin)
mvn -B org.owasp:dependency-check-maven:check -DfailBuildOnCVSS=7
Nothing matches this filter.
Watch out
- Do not put passwords in pom.xml; use settings.xml with values from CI secrets (
${env.NEXUS_PASSWORD}). -DskipTestsin a release pipeline hides broken code.- Cache
~/.m2/repositoryin CI, or every build downloads the internet again.
Files
pom.xml- The project model.
~/.m2/settings.xml- Repositories, mirrors and credentials.
~/.m2/repository/- The local cache of downloaded artifacts.
Tips to remember
- The Maven Wrapper (
./mvnw) pins the Maven version for the project.
Exit codes
0 | BUILD SUCCESS. |
1 | BUILD FAILURE. |