Guides / Languages and packages

Scan Java dependencies in Maven and Gradle projects

intermediateUpdated 26 Nov 20261070 words

Java builds pull in many libraries that nobody on the team chose directly. Log4Shell (CVE-2021-44228) was in log4j-core, which many projects received through another library. Maven and Gradle do not ship an audit command, so the work has two parts: see what the build resolves, then check that list with a scanner.

See what the build resolves

Maven prints the resolved tree with the dependency plugin. -Dincludes filters it to a group or an artifact:

mvn dependency:tree
mvn dependency:tree -Dincludes=org.apache.logging.log4j

For a project with log4j-core 2.14.1 and commons-text 1.9 (Maven 3.10.2):

com.example:audit-demo:jar:1.0.0
+- org.apache.logging.log4j:log4j-core:jar:2.14.1:compile
|  \- org.apache.logging.log4j:log4j-api:jar:2.14.1:compile
\- org.apache.commons:commons-text:jar:1.9:compile
   \- org.apache.commons:commons-lang3:jar:3.11:compile

Gradle prints the tree per configuration; runtimeClasspath is what ships with the application. dependencyInsight explains why a version was chosen:

./gradlew dependencies --configuration runtimeClasspath
./gradlew dependencyInsight --dependency log4j-core --configuration runtimeClasspath

Lock Gradle versions

A Maven build resolves the same versions as long as pom.xml lists fixed versions. Gradle builds often use ranges or dynamic versions (2.+), and then a scan of the build file cannot tell what you ship. Dependency locking writes the resolved versions to gradle.lockfile:

// build.gradle.kts
dependencyLocking {
    lockAllConfigurations()
}
./gradlew dependencies --write-locks
org.apache.commons:commons-lang3:3.11=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.apache.commons:commons-text:1.9=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.apache.logging.log4j:log4j-api:2.14.1=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.apache.logging.log4j:log4j-core:2.14.1=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
empty=annotationProcessor,testAnnotationProcessor

Commit gradle.lockfile. From then on, Gradle uses these versions and fails when the build asks for others, until you update the lock for a module:

./gradlew dependencies --update-locks org.apache.logging.log4j:log4j-core

Scan with Trivy

Trivy reads pom.xml, gradle.lockfile and JAR files. In the project directory:

trivy fs --scanners vuln --severity HIGH,CRITICAL .

For the example project (Trivy 0.75.0, columns trimmed):

gradle/gradle.lockfile (gradle)
Total: 4 (HIGH: 1, CRITICAL: 3)

Library                               Vulnerability   Severity  Installed  Fixed Version
org.apache.commons:commons-text       CVE-2022-42889  CRITICAL  1.9        1.10.0
org.apache.logging.log4j:log4j-core   CVE-2021-44228  CRITICAL  2.14.1     2.15.0, 2.3.1, 2.12.2
                                      CVE-2021-45046  CRITICAL             2.16.0, 2.12.2, 2.3.1
                                      CVE-2021-45105  HIGH                 2.12.3, 2.17.0, 2.3.1

pom.xml (pom)
Total: 4 (HIGH: 1, CRITICAL: 3)
...

--exit-code 1 makes Trivy exit with 1 when it reports something, for use as a CI gate.

For pom.xml, Trivy resolves the transitive dependencies itself. It looks in the local Maven repository (~/.m2/repository) first and downloads missing POM files from the repositories of the project and from Maven Central. --offline-scan stops those downloads. In a test without a local repository, an offline scan of a pom.xml found the direct dependency and none of the three below it, so use it only where the build has filled ~/.m2 before. A gradle.lockfile already lists every resolved module, and its scan does not need the network.

Trivy scans the Maven scopes compile, runtime and import and leaves out test and provided. For Gradle it leaves out development dependencies unless you pass --include-dev-deps.

A built artifact is the other place to look. A fat JAR or a container image holds the libraries that ship, whatever the build file says; see /guides/scan-images-in-ci for image scans.

OWASP Dependency-Check

OWASP Dependency-Check is a Maven and Gradle plugin that checks the JAR files of the build against the National Vulnerability Database (NVD). It fits teams that want the check inside the build itself:

<plugin>
  <groupId>org.owasp</groupId>
  <artifactId>dependency-check-maven</artifactId>
  <version>13.0.0</version>
  <configuration>
    <failBuildOnCVSS>8</failBuildOnCVSS>
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>check</goal>
      </goals>
    </execution>
  </executions>
</plugin>

failBuildOnCVSS fails the build for findings with a CVSS score of 8 or higher. The first run downloads and processes the NVD data, which the documentation says may take 20 minutes or more; keep its data directory in the CI cache.

Fix findings

For a direct dependency, raise the version in pom.xml or build.gradle.kts.

For a transitive dependency, set the version without making it a direct dependency. In Maven, dependencyManagement sets the version wherever the artifact appears in the tree. Libraries that consist of several artifacts, such as Log4j, publish a BOM (bill of materials); import it so that all parts move together:

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-bom</artifactId>
      <version>2.26.1</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

In a test where log4j-slf4j-impl 2.14.1 pulled in log4j-core, pinning only log4j-core to 2.26.1 left log4j-api at 2.14.1; with the BOM, both moved to 2.26.1. A version you declare in your own dependencyManagement wins over the same artifact in an imported BOM, such as the Spring Boot dependencies. Spring Boot also reads the property log4j2.version for this purpose.

In Gradle, a dependency constraint sets the version and records why:

dependencies {
    constraints {
        implementation("org.apache.logging.log4j:log4j-core:2.26.1") {
            because("CVE-2021-44228 and later log4j-core advisories")
        }
    }
}

With dependency locking on, the build then fails (FAILED in the dependency tree) because the lockfile still says 2.14.1. Update the lock for that module:

./gradlew dependencies --update-locks org.apache.logging.log4j:log4j-core

dependencyInsight then lists the reason:

   Selection reasons:
      - By ancestor
      - By constraint: CVE-2021-44228 and later log4j-core advisories
      - By constraint: Dependency version enforced by Dependency Locking

Remove a pin when the library or framework that pulls the dependency in ships the fixed version itself. After each change, print the tree again and scan again.

Run it in CI and on a schedule

Advisories for Java libraries keep appearing while many Java services are rebuilt rarely. Scan each pull request and add a scheduled scan of the main branch. A GitHub Actions job with the Trivy image:

name: dependency scan
on:
  pull_request:
  schedule:
    - cron: "53 5 * * *"
jobs:
  trivy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - run: >
          docker run --rm -v "$PWD:/src"
          aquasec/trivy:0.75.0
          fs --scanners vuln --severity HIGH,CRITICAL --exit-code 1 /src

Each run downloads the vulnerability database (138 MB in November 2026); mount and cache ~/.cache/trivy if that matters for your build times. Dependabot and Renovate read pom.xml and Gradle builds and open pull requests for new versions, which keeps the time between a fixed release and your deploy short.

The Patchbrief agent can read pom.xml and gradle.lockfile in the app paths you configure and checks them against new advisories every day.

Sources

Check your servers every day

The agent of Patchbrief reports the packages and settings of a server once a day; the digest names what to fix first.

Start free