Interview Prep/Build Tools (Java, Node.js, Python, .NET)/Java builds (Maven, Gradle, JAR, WAR)

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

  1. Pinned toolchain — ./mvnw, a fixed JDK in setup-java and the Docker base image tag (or digest).
  2. Pinned plugin and dependency versions — no LATEST, no version ranges; dependencyManagement / BOMs for consistency.
  3. Internal repository — all downloads through Nexus/Artifactory (mirrorOf *), which also protects against public repo outages and dependency-confusion attacks.
  4. No SNAPSHOTs in releases — enforce with the maven-enforcer-plugin (requireReleaseDeps).
  5. Tests and quality gates — unit tests, JaCoCo coverage, SonarQube quality gate.
  6. Dependency scanning — OWASP Dependency-Check, Trivy fs, Snyk or Dependabot.
  7. SBOM — CycloneDX Maven plugin or Syft, stored with the release.
  8. Immutable artifacts — release versions can't be redeployed; Docker image tagged with version + Git SHA.
  9. Build once, promote — the same JAR/image moves dev → staging → prod.
  10. Secrets — Nexus credentials from the CI secret store, never in pom.xml or settings.xml in 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)