Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

mvn

Build, test and package Java projects with Maven, and push the result (a JAR or WAR) to an artifact repository.

Niveau
DevOps-tool
Groep
Buildtools en talen
Root nodig
Nee
Pakket
maven

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

Wat het doet

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.

Zo schrijf je het

mvn [OPTIONS] PHASE|PLUGIN:GOAL ...

Belangrijkste delen

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.

Opties 12

Meest gebruikt

Options

-B, --batch-mode
No colours or questions (CI).
-DskipTests
Compile tests but do not run them.
-Dmaven.test.skip=true
Do not compile or run tests.
-pl MODULE -am
Build one module (and the modules it needs).
-P PROFILE
Use a profile from pom.xml (for example prod).
-s FILE
Use another settings.xml (registry, credentials).
-o, --offline
Use only the local repository.
-U
Check for newer snapshot versions.
-T 1C
Build with one thread per CPU core.
-q / -X
Quiet / debug output.
-D NAME=VALUE
Set a property.
-ntp
Do not show download progress (shorter CI logs).

Voorbeelden 11

$ als gewone gebruiker# als root (of met sudo)

  1. 1Build and test

    mvn -B clean verify
  2. 2Make the JAR without running tests (only when tests ran before)

    mvn -B -DskipTests package
    ls target/*.jar
  3. 3Build 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"]
  4. 4Upload to Nexus with credentials from settings.xml

    mvn -B -s ci-settings.xml deploy
  5. 5Show the dependency tree

    mvn dependency:tree -Dincludes=org.apache.logging.log4j
  6. 6Build one module of a big project

    mvn -B -pl services/orders -am package
  7. 7Set the release version in CI

    mvn -B versions:set -DnewVersion=1.4.0 -DgenerateBackupPoms=false
  8. 8Use the production profile

    mvn -B -P prod package
  9. 9Faster build on many cores

    mvn -B -T 1C verify
  10. 10Build without internet (all in ~/.m2)

    mvn -o package
  11. 11Check for known vulnerable dependencies (OWASP plugin)

    mvn -B org.owasp:dependency-check-maven:check -DfailBuildOnCVSS=7

Let op

  • Do not put passwords in pom.xml; use settings.xml with values from CI secrets (${env.NEXUS_PASSWORD}).
  • -DskipTests in a release pipeline hides broken code.
  • Cache ~/.m2/repository in CI, or every build downloads the internet again.

Bestanden

pom.xml
The project model.
~/.m2/settings.xml
Repositories, mirrors and credentials.
~/.m2/repository/
The local cache of downloaded artifacts.

Tips om te onthouden

  • The Maven Wrapper (./mvnw) pins the Maven version for the project.

Exitcodes

0BUILD SUCCESS.
1BUILD FAILURE.

Volledige handleiding

Deze pagina legt het commando uit in eenvoudige woorden. De originele handleiding heeft elk detail.

In je terminal: man mvn