Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
Know it before you install
The JDK (compiler + runtime) builds code; a JRE-only install just runs it. Use an LTS Java version (17, 21, 25). Maven reads pom.xml, downloads dependencies into ~/.m2/repository from repositories (Maven Central or your Nexus/Artifactory mirror set in settings.xml), and runs the lifecycle phases: validate → compile → test → package → verify → install → deploy.
| Item | Value |
|---|---|
| JDK packages | RHEL: java-21-openjdk-devel; Ubuntu: openjdk-21-jdk (also 17/25) |
| Maven packages | RHEL: maven (or maven-openjdk21); Ubuntu: maven |
| Choose Java | alternatives --config java (RHEL), update-alternatives --config java (Ubuntu) |
| Maven settings | ~/.m2/settings.xml (user), $MAVEN_HOME/conf/settings.xml (global) |
| Project wrapper | ./mvnw (Maven Wrapper — a fixed Maven version per project) |
Install on RHEL, Rocky, Alma and Fedora
Install on Ubuntu and Debian
apt-get update
apt-get install -y openjdk-21-jdk maven
java -version
mvn -version
update-alternatives --config javaOp het werk
Several projects need different JDKs? Install them side by side and set JAVA_HOME per project or CI job (export JAVA_HOME=/usr/lib/jvm/java-17-openjdk). The Maven Wrapper (./mvnw) pins the Maven version in the repository — prefer it in CI.
First configuration: settings.xml for a company repository
<settings>
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>https://nexus.example.com/repository/maven-public/</url>
</mirror>
</mirrors>
<servers>
<server>
<id>nexus</id>
<username>${env.NEXUS_USER}</username>
<password>${env.NEXUS_PASSWORD}</password>
</server>
<server>
<id>nexus-releases</id>
<username>${env.NEXUS_USER}</username>
<password>${env.NEXUS_PASSWORD}</password>
</server>
</servers>
</settings>| Setting | Why |
|---|---|
<mirror> with mirrorOf * | All downloads go through your repository (fast, cached, controlled). |
<server> ids | Logins for the mirror and for mvn deploy (the id must match the pom.xml repository id). |
${env.…} | Passwords from CI secrets / environment, never in Git. |
In the project pom.xml, set the Java version once: <properties><maven.compiler.release>21</maven.compiler.release></properties>.
Daily use: build, test, check
./mvnw -B -ntp verify
mvn -B -ntp clean package -DskipTests
mvn -B -ntp -T 1C verify
mvn -pl shop-api -am package
mvn dependency:tree -Dincludes=org.apache.logging.log4j
mvn versions:display-dependency-updates
mvn help:effective-settings
mvn -B deploy -DaltDeploymentRepository=nexus-releases::https://nexus.example.com/repository/maven-releases/| Option | Why |
|---|---|
-B (batch) and -ntp (no transfer progress) | Clean CI logs. |
verify | Compile, test, package and run checks (better than only package in CI). |
-T 1C | Build modules in parallel (1 thread per CPU core). |
-pl … -am | Build one module and what it needs. |
dependency:tree | Find where a library (and its version) comes from. |
Check dependencies for known CVEs: trivy fs --scanners vuln . (reads pom.xml), or the OWASP dependency-check-maven plugin in the build.
Run a Spring Boot (or any) jar as a service, not as root:
[Unit]
Description=Shop API (Java)
After=network.target
[Service]
User=shopapi
WorkingDirectory=/opt/shop-api
ExecStart=/usr/bin/java -XX:MaxRAMPercentage=75 -XX:+ExitOnOutOfMemoryError -jar /opt/shop-api/shop-api.jar
SuccessExitStatus=143
Restart=on-failure
EnvironmentFile=-/etc/shop-api.env
[Install]
WantedBy=multi-user.target-XX:MaxRAMPercentage=75 sizes the heap from the memory limit (also in containers); ExitOnOutOfMemoryError lets systemd/Kubernetes restart a broken JVM.
Real uses at work:
- CI builds of Java services with a Nexus mirror and cached
~/.m2. - Publishing internal libraries to Nexus (
mvn deploy). - Running and tuning JVM apps (heap, GC logs, thread dumps with
jcmd PID Thread.print).
Make it safe
Security checklist:
- LTS JDK, updated every quarter (Oracle/OpenJDK critical patch updates).
- All dependencies through your repository; scan for CVEs in CI; update libraries.
- No passwords in
settings.xmlin Git — environment variables or encrypted settings. - Pin plugin versions and use the Maven Wrapper for reproducible builds.
- Run apps as their own user; no remote JMX/debug ports open in production.
Update and remove
Fix common problems
| Problem | Cause | Fix |
|---|---|---|
error: invalid target release: 21 / release version 21 not supported | Maven runs with an older JDK. | mvn -version shows the JDK; set JAVA_HOME to JDK 21. |
401 Unauthorized from the repository | The server id does not match, or wrong credentials. | Same id in settings.xml and the pom.xml/mirror; check the variables. |
PKIX path building failed | Java does not trust the company CA (proxy or Nexus). | Add the CA to the system trust store (RHEL: update-ca-trust) or the JDK cacerts with keytool. |
Could not resolve dependencies | Artifact missing in the mirror, or no access to the proxy repo. | Check in Nexus; mvn -U to retry; check mvn help:effective-settings. |
java.lang.OutOfMemoryError: Java heap space | Heap too small for the app or the build. | -XX:MaxRAMPercentage / -Xmx; for Maven: MAVEN_OPTS=-Xmx2g. |
| CI builds are slow | Dependencies downloaded every run. | Cache ~/.m2/repository between jobs; use a mirror. |
Next steps
- Command page: mvn.
- At work: Maven — Java builds.
- The repository: Nexus. Quality and security: SonarQube, Trivy. CI: Jenkins.
1Maven says invalid target release: 21. What is wrong?
Maven runs on an older JDK; point JAVA_HOME to JDK 21.
2Why use -XX:MaxRAMPercentage=75 for Java in containers?
The heap is sized from the container's memory limit, so the JVM does not get killed for using too much.