How do you make Java builds reproducible and secure in a CI/CD pipeline?
AdvancedProductionJava builds (Maven, Gradle, JAR, WAR)
Answer
Short answer
Pin every version (Java, Maven via wrapper, plugins, dependencies), resolve dependencies only through a trusted internal repository, never use SNAPSHOTs in releases, scan dependencies and the image for vulnerabilities, generate an SBOM, and publish immutable, versioned artifacts that are promoted — not rebuilt — across environments.
Checklist
- Pinned toolchain —
./mvnw, a fixed JDK insetup-javaand the Docker base image tag (or digest). - Pinned plugin and dependency versions — no
LATEST, no version ranges;dependencyManagement/ BOMs for consistency. - Internal repository — all downloads through Nexus/Artifactory (
mirrorOf *), which also protects against public repo outages and dependency-confusion attacks. - No SNAPSHOTs in releases — enforce with the
maven-enforcer-plugin(requireReleaseDeps). - Tests and quality gates — unit tests, JaCoCo coverage, SonarQube quality gate.
- Dependency scanning — OWASP Dependency-Check, Trivy
fs, Snyk or Dependabot. - SBOM — CycloneDX Maven plugin or Syft, stored with the release.
- Immutable artifacts — release versions can't be redeployed; Docker image tagged with version + Git SHA.
- Build once, promote — the same JAR/image moves dev → staging → prod.
- Secrets — Nexus credentials from the CI secret store, never in
pom.xmlorsettings.xmlin Git.
Example enforcer rule
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<executions><execution><goals><goal>enforce</goal></goals>
<configuration><rules>
<requireMavenVersion><version>[3.9,)</version></requireMavenVersion>
<requireJavaVersion><version>[21,)</version></requireJavaVersion>
<requireReleaseDeps/>
</rules></configuration>
</execution></executions>
</plugin>
What to say in the interview
"Reproducible means same inputs, same output: pinned JDK, Maven wrapper, pinned dependencies through our Nexus, no snapshots in releases enforced by the enforcer plugin. Secure means dependency and image scanning, an SBOM per release, secrets from the CI vault, and immutable artifacts promoted through environments instead of rebuilt."
All Build Tools (Java, Node.js, Python, .NET) questions Roadmap: Java apps: Maven, Gradle, JAR & WAR
More on Java builds (Maven, Gradle, JAR, WAR)
What is pom.xml and what is it used for?BeginnerHow do you build and run a Java project with Maven?BeginnerWhat is the difference between a JAR and a WAR file?BeginnerWhat is Gradle and how is it different from Maven?BeginnerHow do you configure a Spring Boot application for different environments?BeginnerA Maven build fails in the pipeline with "Could not resolve dependencies". How do you fix it?Intermediate