Skip to main content

Maven-Based Java Projects

The spdx-maven-plugin and cyclonedx-maven-plugin Maven plugins build, respectively, SPDX and CycloneDX-compliant SBOMs. Both do a good job of capturing information about project content as well as the transitive closure of all dependencies.

The quality of the results of using these plugins depends on the quality of the metadata available to the plugin. For Maven-based builds, metadata is provided primarily via the pom.xml file. This includes dependencies, description, identity of the developers, location of source repositories, licenses, and more. The overall completeness and quality of metadata varies greatly: dependencies are generally specified correctly (they may include extra dependencies that are no longer used), but most of the other fields are either not provided, contain inaccurate data, or are specified in inconsistent formats.

License information, for example, tends to be specified by name. When multiple licenses are involved, they tend to be specified as individual entries with no explicit indication of how the licenses are combined.

Maven Metadata Best Practices

The quality of the SBOMs depends on the quality of the metadata provided.

We recommend adopting the following practices.

  • Ensure that metadata is accurate
    SBOM generation leverages the metadata provided in the pom.xml file, so specifying that information accurately improves the quality of the SBOM.
  • Specify license data at the module level
    If it is truly the case that all modules are distributed under the same license terms, then the license can (and indeed should) be specified at the parent level. When individual modules have different license terms, then those should be specified at the module level.
  • Specify the license as an SPDX identifier or expression
    When the content is distributed under a single license, specify that license as an SPDX identifier. For example, to express that the content is distributed under the Eclipse Public License 2.0:
    <licenses>
            <license>
                    <name>EPL-2.0</name>
                    <url>https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt</url>
            </license>
    </licenses>
    

    When the content is distributed under multiple licenses, specify the license as an SPDX expression. For example, to express that content may be consumed under either the Eclipse Public License 2.0 or the GNU Public License 2.0 with the Classpath Exception (EPL-2.0 or GPL-2.0-only with Classpath-exception-2.0):
    <licenses>
            <license>
                    <name>EPL-2.0 or GPL-2.0-only with Classpath-Exception-2.0</name>
            </license>
    </licenses>
    

    Note that url can be considered optional. Specifying a url does not make sense when specifying a license expression.

    Tip: SPDX expressions should be written from the consumer’s perspective. Use or to express that a consumer can choose between licenses. Use and to express that a consumer must accept the terms of multiple licenses.

  • Express only the license of the project content
    Do not include license information for dependencies.
  • Specify all of the metadata
    All of it goes into the SBOM. Specify the name, description, organisation, scm, issueManagement, …​
  • Correctly use Maven scope
    For example, JUnit should be in the test scope where it will be ignored by the SBOM generator plugin.

CycloneDX SBOMs for Maven

The CycloneDX Maven plugin can be used to generate a CycloneDX SBOM directly from a Maven build.

Add the following to the build/plugins section of your pom.xml file.

<build>
        ...
        <plugins>
                ...
                <plugin>
                        <groupId>org.cyclonedx</groupId>
                        <artifactId>cyclonedx-maven-plugin</artifactId>
                        <version>2.7.9</version>
                        <configuration>
                                <schemaVersion>1.4</schemaVersion>
                                <projectType>library</projectType>
                        </configuration>
                        <executions>
                                <execution>
                                        <phase>package</phase>
                                        <goals>
                                                <goal>makeAggregateBom</goal>
                                        </goals>
                                </execution>
                        </executions>
                </plugin>
        </plugins>
</build>

Tip: Add this to your parent pom.xml to generate the SBOM for all modules.

The default configuration will automatically exclude dependencies that are declared in the test scope, but will include dependencies that are included in the compile, provided, runtime, and system scopes. You can tune the configuration to exclude other scopes (or include the test scope).

<configuration>
        ...
        <includeCompileScope>true</includeCompileScope>
        <includeProvidedScope>true</includeProvidedScope>
        <includeRuntimeScope>true</includeRuntimeScope>
        <includeSystemScope>true</includeSystemScope>
        <includeTestScope>false</includeTestScope>
        ...
</configuration>

The projectType value expresses in the nature of the content that’s produced by the build. The value library (the default) expresses that the content is a library (e.g., a Java JAR file). Other values (e.g., application) may also be used depending on the nature of the content . There’s more help in the CycloneDX documentation.