الفلاتر
طريقة البحث
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • "كلمتان" تجد العبارة بالضبط.
  • --name أو -x تجد خيارًا.
  • #كلمة تبحث داخل الأوامر فقط.
  • @grep تجد الدروس والمعامل التي تستخدم grep.
  • الفلاتر: cat:network · in:labs · level:rhcsa · ch:8 · root:yes. ضع ! قبل كلمة لاستبعادها. OR بين الكلمات تعني إحداها.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث

mvn

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

المستوى
أداة DevOps
المجموعة
أدوات البناء ولغات البرمجة
يحتاج root
لا
الحزمة
maven

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

ماذا يفعل

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.

طريقة الكتابة

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

الأجزاء الرئيسية

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.

الخيارات 12

الأكثر استخدامًا

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).

أمثلة 11

$ مستخدم عادي# بصلاحية root (أو مع 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

انتبه

  • 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.

الملفات

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

نصائح للحفظ

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

رموز الخروج

0BUILD SUCCESS.
1BUILD FAILURE.

الدليل الكامل

هذه الصفحة تشرح الأمر بكلمات بسيطة. الدليل الأصلي فيه كل التفاصيل.

في الطرفية: man mvn