Advanced
Searching inInstall guides
How to search
  • Paste an error message in quotes, e.g. "permission denied", to find the fix table that explains it.
  • Choose an area and a part of the guide (install, first settings, daily use, security, fixes) to go straight to the right step.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

Guide 97Maven / Java (OpenJDK) · Languages and build

Install and set up Java (OpenJDK) and Maven

Java runs a huge part of company software — Spring Boot services, banking and ERP systems, Jenkins, Kafka, Elasticsearch, Keycloak. Maven builds most Java projects: it downloads dependencies, compiles, tests and packages them. As a DevOps engineer you install the right JDK, configure Maven for a company repository, run reproducible CI builds, check dependencies for CVEs, and run Java apps safely. This guide does that on RHEL and Ubuntu.

  • 9 Lessons
  • 4 Commands

$ run as a normal user# run as root (or with sudo)

97.1

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.

ItemValue
JDK packagesRHEL: java-21-openjdk-devel; Ubuntu: openjdk-21-jdk (also 17/25)
Maven packagesRHEL: maven (or maven-openjdk21); Ubuntu: maven
Choose Javaalternatives --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)

97.2

Install on RHEL, Rocky, Alma and Fedora

dnf -y install java-21-openjdk-devel maven-openjdk21
java -version
openjdk version "21.0.9" 2025-10-21 LTS
mvn -version
alternatives --config java

97.3

Install on Ubuntu and Debian

apt-get update
apt-get install -y openjdk-21-jdk maven
java -version
mvn -version
update-alternatives --config java

At work

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.

97.4

First configuration: settings.xml for a company repository

~/.m2/settings.xml
<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>
SettingWhy
<mirror> with mirrorOf *All downloads go through your repository (fast, cached, controlled).
<server> idsLogins 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>.

Commands in this lesson

97.5

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/
OptionWhy
-B (batch) and -ntp (no transfer progress)Clean CI logs.
verifyCompile, test, package and run checks (better than only package in CI).
-T 1CBuild modules in parallel (1 thread per CPU core).
-pl … -amBuild one module and what it needs.
dependency:treeFind 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:

/etc/systemd/system/shop-api.service
[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).

97.6

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.xml in 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.

97.7

Update and remove

dnf -y upgrade 'java-21-openjdk*' maven-openjdk21
apt-get install --only-upgrade -y openjdk-21-jdk maven
dnf -y remove java-17-openjdk-devel

Commands in this lesson

97.8

Fix common problems

ProblemCauseFix
error: invalid target release: 21 / release version 21 not supportedMaven runs with an older JDK.mvn -version shows the JDK; set JAVA_HOME to JDK 21.
401 Unauthorized from the repositoryThe server id does not match, or wrong credentials.Same id in settings.xml and the pom.xml/mirror; check the variables.
PKIX path building failedJava 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 dependenciesArtifact 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 spaceHeap too small for the app or the build.-XX:MaxRAMPercentage / -Xmx; for Maven: MAVEN_OPTS=-Xmx2g.
CI builds are slowDependencies downloaded every run.Cache ~/.m2/repository between jobs; use a mirror.

Commands in this lesson

97.9

Next steps

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.

Commands in this lesson

All commands in this chapter