I have blocker issue "Close this "ConfigurableApplicationContext"" in main method
public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); }
I've tried code from SonarQube example
public static void main(String[] args) { ConfigurableApplicationContext context = null; try { context = SpringApplication.run(MyApplication.class, args); } finally { if (context != null) { context.close(); } } }
but it closes the context right after starting.
How to fix this issue?
By default, SonarQube way came preinstalled with the server. The default configuration for SonarQube way flags the code as failed if: the coverage on new code is less than 80%. percentage of duplicated lines on new code is greater than 3. maintainability, reliability or security rating is worse than A.
The default configuration for SonarQube way flags the code as failed if: the coverage on new code is less than 80% percentage of duplicated lines on new code is greater than 3 maintainability, reliability or security rating is worse than A
sonar.coverage.exclusions: This provides us to exclude any code file unrelated to the coverage report. For example Spring Boot Main class. You can find more configuration keys in the Sonarqube server Administration -> Configuration -> Analysis Scope.
1. Overview In this article, we're going to be looking at static source code analysis with SonarQube – which is an open-source platform for ensuring code quality. Let's start with a core question – why analyze source code in the first place?
The issue that SonarQube is reporting is a false positive and should be ignored. SonarQube's FAQ lists some options for removing false positives:
False-Positive and Won't Fix
You can mark individual issues as False Positive or Won't Fix through the issues interface. However, this solution doesn't work across branches - you'll have to re-mark the issue False Positive for each branch under analysis. So an in-code approach may be preferable if multiple branches of a project are under analysis:
//NOSONAR
You can use the mechanism embedded in rules engine (//NOPMD...) or the generic mechanism implemented in SonarQube: put //NOSONAR at the end of the line of the issue. This will suppress the issue.
Switch Off Issues
You can review an issue to flag it as false positive directly from the user interface.
If you have a web application, the application context will be destroyed (I think by ContextLoaderListener
, not sure), no explicit code is needed.
In the case of a command line application, the context must be destroyed manually, otherwise beans will not be destroyed properly - @PreDestroy methods will not be called. E.g:
@Bean public ApplicationRunner applicationRunner() { return new ApplicationRunner() { public void run(ApplicationArguments args) throws Exception { try { doStuff(); } finally { context.close(); } }
I noticed this when a Cassandra session stayed open after my spring boot command line application has finished.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With