How do I analyze Groovy code with Sonarlint for IntelliJ? - grails

The Sonarlint plugin for IntelliJ does analysis on our Java code, but it won't analyze Groovy code in the same project.
We have a Grails project which works with sonar-runner on the command line. When I install the IntelliJ IDEA Sonarlint plugin, the plugin doesn't seem to pick up the settings from our command line config, and it doesn't show the warning highlights for issues like it does for the Java files in the editor.
Our project uses the "code-coverage" Grails plugin to generate an XML coverage report, then the sonar runner (command line tool) pulls in the metrics. In sonar-project.properties, we set:
# Project key within sonar
sonar.projectKey=com.mycompany:myapp
sonar.projectName=My Grails Project
sonar.projectVersion=1.0
# Source directories to analyze
sonar.sources=src/groovy, grails-app/domain, grails-app/services, grails-app/controllers, grails-app/utils, grails-app/taglib, grails-app/views
# Set language for the project to be Groovy
sonar.language=grvy
# Path to the Cobertura XML report
sonar.grvy.coveragePlugin=cobertura
sonar.groovy.cobertura.reportPath=target/test-reports/cobertura/coverage.xml
sonar.jdbc.username=...
sonar.jdbc.password=...
sonar.host.url=http://sonar.mycompany.com
sonar.jdbc.url=...
sonar.jdbc.driverClassName=...
The properties file has JDBC settings, the coverage report path, etc.

SonarLint is a different product than SonarQube (even if there are some links). For example SonarLint doesn't know what is sonar-project.properties. The purpose is to integrate with IntelliJ, not with some external configuration.
Regarding Groovy support, this is not planned since we don't have any Groovy analyzer (the SonarQube plugin for Groovy is simply reusing codenarc reports AFAIK). If you want to run Codenarc in IntelliJ, there is already a dedicated plugin for that.

Related

Testing Jenkins "Code Coverage API Plugin"

I am new to Jenkins, I have installed "Code Coverage API Plugin" in Jenkins, now want to test it.
I am trying to do steps from this page https://plugins.jenkins.io/code-coverage-api/.
But I did not understand where to put XML files from points 1 and 2.
Can anybody help me, please?
The tags present in the images are the plugins that will be added in the pom.xml of the application. By running mvn clean package cobertura will generate a report based on the formats provided, xml or html.
If the code is checked out to the job and after running "mvn clean package" cobertura report will be generated under project workspace target/site/cobertura
The xml path is provided to code coverage api to provide the graphical representation.
Jacaco is preferred for java versions >=8 whereas cobertura doesn't cover for java8 based functionalities.

IntelliJ code coverage runner - headless

According to:
https://intellij-support.jetbrains.com/hc/en-us/community/posts/206842605-Running-IntelliJ-code-coverage-headless-on-Jenkins-
It is possible to use intellij code-coverage runner in a headless environment - i.e. Jenkins.
This is nice since IntelliJ reports much more accurate coverage results on new groovy code than e.g. Cobertura. And I can also collect information from Grails tests - unit and integration. However, I have no idea on how to set it up - gradle, maven, custom build-script or whatever - on Jenkins.
When IntelliJ runs Grails tests it fires the following command:
C:\Program Files\Java\jdk1.7.0_79\bin\java
-XX:+TieredCompilation
-XX:TieredStopAtLevel=1
-XX:CICompilerCount=3
-Dgrails.full.stacktrace=true
-Djline.WindowsTerminal.directConsole=false
-Dgrails.build.listeners=org.jetbrains.groovy.grails.rt.GrailsIdeaTestListener
-Didea.launcher.port=7533
"-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.3\bin" -classpath C:\Users\xxx\AppData\Local\Temp\classpath.jar
-Dfile.encoding=UTF-8 com.intellij.rt.execution.application.AppMain org.grails.cli.GrailsCli test-app -echoOut
Which indicates that IntelliJ should be installed to run it headless. Is it possible to execute the above command in a headless env?
I have tried unpacking intellij and by executing the following (slightly modified for linux) command:
java
-XX:+TieredCompilation
-XX:TieredStopAtLevel=1
-XX:CICompilerCount=3
-Dgrails.full.stacktrace=true
-Djline.WindowsTerminal.directConsole=false -Dgrails.build.listeners=org.jetbrains.groovy.grails.rt.GrailsIdeaTestListen r
-Didea.launcher.port=7533
-Didea.launcher.bin.path=/home/jenkins/idea/bin -classpath /tmp/classpath.jar
-Dfile.encoding=UTF-8 org.grails.cli.GrailsCli test-app -echoOut
it fails with:
Error: Could not find or load main class com.intellij.rt.execution.application.AppMain even with idea_rt.jar added to the command with "cp" flag.
Is this a goose chase, or could it be done?
Did you check classpath.jar on Windows? This file is automatically generated and links lots of jars via Manifest to avoid OS limitation for the maximum command line length. Your /tmp/classpath.jar either doesn't exist or doesn't link the dependencies correctly.
Final classpath would include several IDE jars from the lib directory, project classes from the output directory and dependencies, plugin jars, etc. In other words, everything required to run the app would be linked from the generated classpath.jar file.

Is it possible to run Sonar plugin on Jenkins without any build process?

I would like to run Sonar plugin on Jenkins without any build process (my intent was to integrate Sonar analysis within Jenkins and take advantage of the subversion plugins and configurations we already had on there).
I do not want to run the build process since that would take up unnecessary time; I would only like to have a Jenkins job dedicated for Sonar analysis.
You can do that. You have to triggering the analysis with the SonarQube Runner.
Go to the Build section, click on Add build step and choose Invoke Standalone Sonar Analysis
Configure the SonarQube analysis. You can either point to an existing sonar-project.properties file or set the analysis properties directly in the Project properties field
When you analyse with SonarQube Runner , then you should give the following mandatory properties:
sonar.projectKey=my:project
sonar.projectName=My project
sonar.projectVersion=1.0
# Path to the parent source code directory.
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# Since SonarQube 4.2, this property is optional. If not set, SonarQube starts looking for source code
# from the directory containing the sonar-project.properties file.
sonar.sources=src
In this case you may miss some rule violations (like FindBugs), because .class files are not provided. You have to build the project manually and set the sonar.binaries property to your class files. If you never want to build the project, then you can use the SourceMeter plugin for SonarQube too. It only needs the source files, but can produce more metrics and issues if you needed.

Reusing several report files in jenkins sonar plugin

I'm generating several XML report files via ant task (checkstyle, findbugs, macker, jacoco, ncss, classycle, etc.).
I want the to reuse the generated files in jenkins sonar plugin. Is there any way to do so?
I'm planning in setting the properties in the project specific sonar-project.properties file. But I can't find any of these options at codehaus documentation site.
There's no point to reuse:
checkstyle or findbugs reports as SonarQube also executes those tools
ncss or classycle reports as it is SonarQube very job to compute those metrics
For JaCoCo, you can reuse reports if you don't want SonarQube to execute the unit tests again. See http://docs.codehaus.org/display/SONAR/Code+Coverage+by+Unit+Tests+for+Java+Project.

PMD with grails project

Does PMD works with grails project, i.e. with .groovy files??
i'm using STS editor,
if it works, what setup i have to do?
Please let me know, if anyone have any idea
Thanks in advance
I'm not aware of any PMD plugin for Groovy/Grails. However, there is a CodeNarc Grails plugin, which does similar kinds of static analysis on Groovy/Grails code.
codeNarc is one of the best choices for grails projects, thou it is ignoring java classes that potentially are part of your project.
I have not seen any pmd or findBugs plugins for grails that would take care of the java portion. You can use the STS/Eclipse PMD plugin thou to analyze explicitly the src/java/ folder.
Unfortunately the findBugs eclipse-plugin is not able to limit to a certain parts of the project so it no big use (findBugs works purely on class files and works through the complete project).
I guess it should be possible to write a grails pmd plugin that would analyze the java parts of a grails project.
Starting with Grails 3, the build system uses Gradle. There is a PMD gradle plugin which you could use to perform static analysis on your java source files. There is also a Codenarc gradle plugin which you can use to perform analysis on the groovy files in your project.
https://docs.gradle.org/current/userguide/pmd_plugin.html

Resources