This project demonstrates different scenarios for using Null Safety in a Maven-based Java project. The project contains modules illustrating different aspects of Null Safety:
- module-nullability - Demonstrates the usage of
@javax.annotation.ParametersAreNonnullByDefaultand SpotBugs detection of violations. - module-openapi-nullability - Demonstrates how Swagger-generated classes handle nullability and SpotBugs detection of violations.
The SpotBugs tool integrated as Maven Plugin allows to fail the build on bugs and vulnerabilities, and to generate a report with vulnerability violations.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.9.0</version>
</plugin>
The tool integrates with Nullability annotations from Javax/Jakarta and includes in the generated report Nullability Violations.
This Demo exploits such a feature to encourage the use of Nullability annotations.
The SpotBugs plugin can be configured to run during the Maven build process and fail based on the severity of the issues found:
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<executions>
<!-- Always generate report -->
<execution>
<id>spotbugs-report</id>
<phase>verify</phase>
<goals>
<goal>spotbugs</goal>
</goals>
</execution>
<!-- Fail when verifying -->
<execution>
<id>spotbugs-check</id>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
<configuration>
<failOnError>true</failOnError>
<threshold>high</threshold>
<effort>Max</effort>
</configuration>
</execution>
</executions>
</plugin>mvn -fae clean verifyThe SpotBugs plugin can be extended to detect security vulnerabilities with:
<configuration>
<plugins>
<plugin>
<groupId>com.h3xstream.findsecbugs</groupId>
<artifactId>findsecbugs-plugin</artifactId>
<version>1.13.0</version>
</plugin>
</plugins>
</configuration>The spotbugs report can be analysed in a CI/CD step to prevent merging potential bugs, or displayed in a dashboard for awareness.
Many CI/CD tools support integration with SpotBugs, allowing you to upload reports and annotate pull requests with findings.
- GitHub Actions: Use the
actions/upload-artifactto store SpotBugs XML/HTML reports, and third-party actions to annotate pull requests with findings. - GitLab CI: Upload SpotBugs reports as job artifacts and use the JUnit or Code Quality report features for integration.
- SonarQube: Import SpotBugs results for advanced dashboards, history, and code quality gates.
This Maven-based project serves as a demonstration of various null safety techniques and how static analysis tools, like SpotBugs, can help detect potential issues in Java code.
See README.md for details on how to use the SpotBugs plugin to detect nullability violations in Java code.
See README.md for details on how to use the SpotBugs plugin to detect nullability violations in Swagger-generated classes.