> For the complete documentation index, see [llms.txt](https://docs.snyk.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.snyk.io/supported-languages/supported-languages-list/java-and-kotlin/snyk-cli-for-java-and-kotlin.md).

# CLI support for Java and Kotlin

How to test Maven and Gradle Projects for Java and Kotlin with the Snyk CLI, including supported manifest files

To test Maven and Gradle Projects, use the `snyk test` command as follows:

* Snyk CLI with Gradle: to build the dependency graph, Snyk integrates with Gradle and inspects the dependencies reported by the tool. The following manifest files are supported: `build.gradle` (Groovy DSL) and `build.gradle.kts` (Kotlin DSL).
* Snyk CLI with Maven: to build the dependency tree, Snyk integrates with Maven and inspects the dependencies reported by the tool. The following manifest files are supported: `pom.xml`.

| Package manager-environment | Test help                                                                                                                                                                                                                                                                                                                                                                                                                                                | Monitor help                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| --------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Maven                       | <p><code>--maven-aggregate-project</code><br><a href="https://docs.snyk.io/developer-tools/snyk-cli/snyk-cli/commands/test#options-for-maven-projects">Options for Maven Projects</a>.<br><br>Example for aggregate projects:<br><code>snyk test --maven-aggregate-project</code></p><p>Ensure you run the options in the same directory as the root <code>pom.xml</code> file.</p>                                                                      | <p><code>--maven-aggregate-project</code><br><a href="https://docs.snyk.io/developer-tools/snyk-cli/snyk-cli/commands/monitor#options-for-maven-projects">Options for Maven Projects</a>.<br><br>Example for aggregate projects:<br><code>snyk monitor --maven-aggregate-project</code></p><p>Ensure you run the options in the same directory as the root <code>pom.xml</code> file.</p>                                                                      |
| Gradle                      | <p><code>--init-script=\<FILE></code> - Used for projects with a Gradle initialization script.</p><p><a href="https://docs.snyk.io/developer-tools/snyk-cli/snyk-cli/commands/test#options-for-gradle-projects">Options for Gradle Projects</a>.</p>                                                                                                                                                                                                     | <p><code>--init-script=\<FILE></code> - Used for projects with a Gradle initialization script.<br><a href="https://docs.snyk.io/developer-tools/snyk-cli/snyk-cli/commands/monitor#options-for-gradle-projects">Options for Gradle Projects</a>.</p>                                                                                                                                                                                                           |
| Build tools                 | <p><code>snyk test -- \[\<context-specific\_options>]</code></p><p>Visit <a href="https://docs.snyk.io/developer-tools/snyk-cli/snyk-cli/commands/test#options-for-build-tools">Options for build tools</a>.</p>                                                                                                                                                                                                                                         |                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| Unmanaged JAR files         | <p><code>--scan-unmanaged</code> - Tests unmanaged files<br></p><p><code>--scan-unmanaged --file=\<JAR\_FILE\_NAME></code> - Tests individual JAR, WAR, and AAR files<br></p><p><code>--scan-all-unmanaged</code> - Auto-detects Maven, JAR, WAR, and AAR files recursively from the current folder.<br>Visit <a href="https://docs.snyk.io/developer-tools/snyk-cli/snyk-cli/commands/test#scan-all-unmanaged">Options for unmanaged JAR files</a>.</p> | <p><code>--scan-unmanaged</code> - Tests unmanaged files<br></p><p><code>--scan-unmanaged --file=\<JAR\_FILE\_NAME></code> - Tests individual JAR, WAR, and AAR files<br></p><p><code>--scan-all-unmanaged</code> - Auto-detects Maven, JAR, WAR, and AAR files recursively from the current folder.</p><p>Visit <a href="https://docs.snyk.io/developer-tools/snyk-cli/snyk-cli/commands/monitor#scan-all-unmanaged">Options for unmanaged JAR files</a>.</p> |

## CLI help for Maven Projects

A Maven aggregate Project is one that uses modules and inheritance. When scanning these types of Projects, Snyk performs a compile to ensure all modules are fixable by the Maven reactor.

To scan aggregate Projects, use the `--maven-aggregate-project` option:

```
snyk test --maven-aggregate-project
```

To scan non-aggregate Projects, use the `--all-projects` option:

```
snyk test --all-projects
```

You can use the same options with `snyk monitor`.

The following example outlines how Maven-specific options are used with the Snyk CLI.

1. Test a specific Maven profile called “prod”.

```
snyk test -- -P prod
```

2. Add a system property from your pom.xml file, for example, the package version that appears in your pom.xml:

```
${pkg_version}
```

3. Define the system property:

```
snyk test -- -Dpkg_version=1.4
```

## CLI help for Gradle Projects

A Gradle build can consist of several sub-projects. Each sub-project has its own `build.gradle`, and only the root Project also includes a `settings.gradle` file. Sub-projects depend on the root Project but can be configured otherwise.

By default, the Snyk CLI scans only the current Project, the Project in the root of the current folder, or the Project that is specified by `--file=path/to/build.gradle`.

To scan all Projects at once (recommended), use the `--all-sub-projects` option:

```
snyk test --all-sub-projects
```

Each of the individual sub-projects appears as a separate Snyk Project in the Web UI.

To scan a specific Project (for example, "myapp"), use the following command:

```
snyk test --sub-project=myapp
```

### Gradle configurations

Gradle dependencies are declared for a particular scope. Gradle represents each scope with [configurations](https://docs.gradle.org/current/userguide/declaring_dependencies.html#sec:what-are-dependency-configurations). For example:

* `implementation`: configuration for dependencies required at compile time and runtime, but not exposed to consumers.
* `api`: configuration for dependencies required at compile time and runtime, and exposed to consumers.
* `compileOnly`: configuration for dependencies required only at compile time.
* `runtimeOnly`: configuration for dependencies required only at runtime.
* `compileClasspath`: configuration for dependencies required at compile time.

In most cases, Snyk includes all the dependencies in the `compileClasspath` configuration, but this can vary in some circumstances.

To test a specific configuration:

* Use the `--configuration-matching` option with a Java regular expression (case-insensitive) as its parameter. The CLI identifies all resolvable configurations that match the regex and aggregates their dependencies into a single scan result. If you require separate reports for different configurations (for example, compile versus runtime), run the command separately for each.
* If the different sub-projects include different configurations, scan each sub-project separately.

Examples of how you can use the `--configuration-matching` option:

* `--configuration-matching=compile` to match `compile`, `testCompile`, `compileOnly`, and so on.
* `--configuration-matching=^compile$` to match only `compile`.
* `--configuration-matching='^(debug|release)compile$'` to match `debugCompile` and `releaseCompile`.
* `--configuration-matching='^(?!test).*$'` to match all configurations except those starting with "test" (for example, excludes `testCompileClasspath`, `testRuntimeClasspath`). Snyk recommends this for Projects with large dependency graphs that hit maximum path limits, because excluding test configurations reduces the graph size while still covering production dependencies.

### Android build variants

Android Gradle supports creating different versions of your app by configuring [build variants](https://developer.android.com/studio/build/build-variants).

Because the Snyk default behavior is to merge all available configurations, the iterated variants cause a clash of configurations that cannot be merged.

In these situations, the Snyk scan fails with an error from Gradle that can contain one of the following messages:

* Cannot choose between the following configurations of `project :mymodulewithvariants`
* Cannot choose between the following variants of `project :mymodulewithvariants`
* Could not select value from candidates

To avoid such conflicts:

* Use a specific configuration: if you know of a build configuration that has all the required attributes and the configuration is identical across all sub-projects included in the test, specify that configuration.\
  For example:

  ```
  --configuration-matching=prodReleaseRuntimeClasspath
  ```
* Explicitly specify the dependency configuration: modify intra-project dependencies in your `build.gradle` files to use a specific configuration. For example:

  ```
    dependencies {
        implementation project(path: ':mymodulewithvariants', configuration: 'default')
    }
  ```
* Suggest configuration attributes: if you receive an error when running the command, the error can indicate which attribute values are available, while the error details from Gradle also indicate which dependency variants match which attributes. Using these details, add the attribute filter option.\
  For example:

  ```
  snyk test --configuration-attributes=buildtype:release,usage:java-runtime,mode:demo
  ```

  matches the variants using `com.android.build.api.attributes.BuildTypeAttr=release` and `org.gradle.usage=java-runtime`

### Daemon

By default, Snyk passes `gradle build --no-daemon` in the background when running `snyk test` and `snyk monitor` on Windows.

If you see `snyk test` or `snyk monitor` fail on other operating systems because of daemon-related issues, try adding the `--no-daemon` flag to the Snyk command or set `GRADLE_OPTS: '-Dorg.gradle.daemon=false'`.

For tips on disabling the daemon, visit the [Gradle documentation](https://docs.gradle.org/current/userguide/gradle_daemon.html#sec:disabling_the_daemon).

### Lockfiles

If your Gradle Project uses a single `gradle.lockfile` or multiple `*.lockfile` per configuration, the following error can appear:

{% code overflow="wrap" %}

```
Gradle Error (short): > Could not resolve all dependencies for configuration ':compileOnly'. > Locking strict mode: Configuration ':compileOnly' is locked but does not have lock state.
```

{% endcode %}

The `compileOnly` configuration has been deprecated, and even if your Project successfully generates a lockfile, the `compileOnly` state is not included because this configuration cannot be resolved.

Only resolvable configurations compute a dependency graph. To solve this issue, Snyk recommends that you update the `build.gradle` file that contains your `dependencyLocking` logic with the following instruction:

```
compileOnly {resolutionStrategy.deactivateDependencyLocking() }
```

This ignores the `compileOnly` and saves only the necessary information to analyze your Project.

If you have trouble testing your Gradle Projects with Snyk, contact Snyk support and provide the following details:

* `build.gradle`
* `settings.gradle` (especially if Snyk did not pick up a version of a package)
* The output from the following commands:
  * `$ snyk test -d`
  * `$ gradle dependencies -q`

## Workaround for Ant and Ivy

[Apache Ant](https://ant.apache.org/) is a Java build system focused solely on executing build tasks defined in XML. [Apache Ivy](https://ant.apache.org/ivy/) extends Ant by adding dependency management, handling library retrieval and transitive dependencies, which Ant alone does not manage.

Ivy dependencies are configured in an XML file, for example `ivy.xml`:

```xml
<ivy-module version="2.0">
    <info organisation="com.example" module="my-project" revision="1.0"/>

    <dependencies>
        <dependency org="junit" name="junit" rev="4.12" conf="default"/>
    </dependencies>
</ivy-module>
```

An `ant` task defined in `build.xml` typically evaluates this dependency file:

```xml
<target name="resolve-dependencies" depends="init">
    <ivy:retrieve pattern="${lib.dir}/[artifact]-[revision].[ext]"/>
</target>
```

When you run `ant resolve-dependencies`, Ivy downloads the dependencies from Maven Central, like regular Maven dependencies.

To let Snyk know about the dependency tree, you must first convert to the Maven POM format. Start by configuring a new `makepom` task in `build.xml`:

```xml
<target name="makepom" depends="resolve-dependencies">
    <ivy:makepom ivyfile="${basedir}/ivy.xml" pomfile="${basedir}/pom.xml" conf="default,runtime">
        <mapping conf="default" scope="compile"/>
        <mapping conf="runtime" scope="runtime"/>
    </ivy:makepom>
</target>
```

You can now run the following commands:

```
ant makepom
snyk test --file=pom.xml
```

You do not need to check in the `pom.xml` file, and you can delete it after the `snyk` test is done. To monitor the dependency tree, run:

```
snyk monitor --file=pom.xml
```

## Snyk CLI tips and tricks

Visit the [CLI commands and options summary](https://docs.snyk.io/developer-tools/snyk-cli/snyk-cli/cli-commands-and-options-summary) and the [CLI cheat sheet](https://snyk.io/blog/snyk-cli-cheat-sheet/). Use the `--help` option in the CLI for details of Snyk CLI commands.

### Source code

* Use the `snyk code test` command from the root of the project to perform source code analysis.
* Use `--scan-all-unmanaged --all-projects` to recursively find all JAR files under the present working directory.

### Open Source libraries

#### Maven

The `snyk test` command tests the first manifest it can find, and scans that singular entry point. To scan all manifests, follow these instructions:

* To scan aggregate projects, use the `--maven-aggregate-project` option\
  (for example, `snyk test --maven-aggregate-project`)
* To scan all projects, use the `--all-projects` option\
  (for example, `snyk test --all-projects`)

Snyk scans profiles that are active by default.

* You can pass additional Maven arguments. A common one is a non-standard `settings.xml` location. For example, `snyk test -- -s path/to/settings.xml`
* To scan a specific configuration, test a specific Maven profile using `-P [name]`. For example, use `snyk test -- -P prod` to scan the `prod` configuration.

#### Gradle

By default, the Snyk CLI scans only the current project (the project in the root of the current folder), or the project that is specified by `--file=path/to/build.gradle`.

You can use `--all-projects` across all package managers. It also includes the behaviors of `--all-sub-projects`, described in the following list.

* To scan all projects at once (recommended), use the `--all-sub-projects` option:\
  (for example, `snyk test --all-sub-projects`). Each of the individual sub-projects appears as a separate Snyk Project in the Web UI.
* To scan a specific project (for example, myapp), use `--sub-project=` (for example, `snyk test --sub-project=myapp`).

For specific configurations, visit [Snyk for Java and Kotlin](/supported-languages/supported-languages-list/java-and-kotlin.md).

#### Unmanaged JAR files

For more information about unmanaged JAR files, visit [Scan all unmanaged JAR files](https://docs.snyk.io/developer-tools/snyk-cli/snyk-cli/scan-and-maintain-projects-using-the-cli/scan-all-unmanaged-jar-files).

### Containers

As part of a container scan, Snyk automatically looks for application vulnerabilities (such as open-source, Maven, and npm vulnerabilities). Snyk recommends integrating through the CLI or a container registry earlier in the pipeline and using this as an additional signal or insight into what is in production. For more information, visit [Snyk CLI for container security](https://docs.snyk.io/developer-tools/snyk-cli/scan-and-maintain-projects-using-the-cli/snyk-cli-for-snyk-container).

To test infrastructure as code, visit [Infrastructure as Code security](https://snyk.io/product/infrastructure-as-code-security/).

To fix vulnerabilities, visit [Fixing vulnerabilities on Maven projects](https://snyk.io/blog/fixing-vulnerabilities-in-maven-projects/).

### Options and plugins

To help generate reports locally or at build time, visit [snyk-to-html plugin](https://docs.snyk.io/developer-tools/snyk-cli/snyk-cli/scan-and-maintain-projects-using-the-cli/cli-tools/snyk-to-html).

Use the `--json` and `--sarif` options to generate output that can be programmatically accessed.

For advanced filtering options, visit [snyk-filter](https://docs.snyk.io/developer-tools/snyk-cli/snyk-cli/scan-and-maintain-projects-using-the-cli/cli-tools/snyk-filter).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.snyk.io/supported-languages/supported-languages-list/java-and-kotlin/snyk-cli-for-java-and-kotlin.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
