Is it possible to override sonar.modules in the sonarqube gradle plugin? - gradle

I'm trying to set up sonar analysis across a non-standard multi-project directory structure with a root project to aggregate all the sonar data.
Using sonarqube runner, I've tried to set the property sonar.modules with all the sub-projects (which are not in child directories), but the plugin seems to override the property with erroneous default values that cause my build to fail.
The overwrite seems to happen in this java class : https://github.com/SonarSource/sonar-gradle/blob/master/src/main/java/org/sonarqube/gradle/SonarQubePlugin.java on line 190.
Is there a way around this problem?

I have not tried, but looking at how other properties(sonar.projectKey, sonar.projectName, sonar.projectVersion) within the same addGradleDefaults method are begin set, Did you try the same pattern followed for configuring the other properties per SonarQube plugin documentation ?
FYR, Here is the link from SonarQube on How to use the plugin and or override the default values within the gradle build script. http://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Gradle
Here is the code snippet
apply plugin: 'org.sonarqube'
sonarqube {
properties {
property "sonar.projectName", "My Project Name"
property "sonar.projectKey", "org.sonarqube:java-gradle-simple"
}
}

Related

sonar.jacoco.reportPaths is not working with sonar gradle plugin

I have a multi-module gradle project. Tests for one of my module are in separate modules. For ex: ProjectA, ProjectATest1, ProjectATest2, ProjectATest3. Jacoco execution reports are getting created in the test projects. I want to do sonar analysis of my ProjectA and sonar fails to find the jacoco files.
in ProjectA, sonarqube properties, I gave
sonarqube{
properties {
property "sonar.jacoco.reportPaths","ProjectATest1/gradleBuild/jacoco/Tests.exec", "ProjectATest2/gradleBuild/jacoco/Tests.exec", "ProjectATest3/gradleBuild/jacoco/Tests.exec"
}
}
But I get this exception
Could not find method property() for arguments
[sonar.jacoco.reportPaths,"ProjectATest1/gradleBuild/jacoco/Tests.exec",
"ProjectATest2/gradleBuild/jacoco/Tests.exec",
"ProjectATest3/gradleBuild/jacoco/Tests.exec"]
SonarQube: Coverage incomplete on multimodule gradle project with JaCoCo the answer says it should work. Is there a bug in sonar gradle plugin?
Could not find method property() for arguments [sonar.jacoco.reportPaths,"ProjectATest1/gradleBuild/jacoco/Tests.exec", "ProjectATest2/gradleBuild/jacoco/Tests.exec", "ProjectATest3/gradleBuild/jacoco/Tests.exec"]
It means that the method property disallow passing 4 arguments. You have to define paths in a one string (comma separated list):
sonarqube{
properties {
property "sonar.jacoco.reportPaths", "ProjectATest1/gradleBuild/jacoco/Tests.exec,ProjectATest2/gradleBuild/jacoco/Tests.exec,ProjectATest3/gradleBuild/jacoco/Tests.exec"
}
}

How to get Intellij to recognize properties in application.yml

I am trying to get Intellij to recognize my properties using gradle. I have followed the steps here. So this means I have a #ConfigurationProperties annotated class with some properties.
I added the spring dependency to process them:
dependencies {
optional "org.springframework.boot:spring-boot-configuration-processor"
}
compileJava.dependsOn(processResources)
I added the plugin (I've tried not using the plugin and just making it a compile dependency, no change)
buildscript {
repositories {
maven { url 'http://repo.spring.io/plugins-release' }
}
dependencies { classpath 'io.spring.gradle:propdeps-plugin:0.0.9.RELEASE' }
}
apply plugin: 'propdeps'
apply plugin: 'propdeps-maven'
apply plugin: 'propdeps-idea'
When I run the build, I see a build/classes/java/main/META-INF/spring-configuration-metadata.json file is created based off of my properties.
When I try to use the property in either application.yml or application.properties, Intellij says it cannot resolve it.
The docs does say it should be called additional-spring-configuration-metadata.json and may expect it to be called that to process it, but I do not see a way to make the build name it that way nor configure Intellij to expect otherwise.
Has anyone got this working with gradle? Or is this a bug.
Edit I created a repo with a pair of projects to demonstrate this. One for gradle and one for maven. I created the projects from start.spring.io and basically just added the properties configuration. I also used a straight compile dependency in both cases instead of optional / compileOnly.
I had not confirmed this before, but the code assist does work for maven, but not gradle. Both create a spring-configuration-metadata.json in the META-INF in their respective build folders. I am not exactly sure who is not picking it up.
Misc relevant versions
Intellij: 2017.3.4
Springboot: 1.5.9
Gradle: 4.4.1
Java: 8.161
Turn the annotation processing on
Do not delegate IDE build/run actions to Gradle
Rebuild your project in IDE: Build -> Rebuild Project
As far as I can tell, IntelliJ (at the time of this writing, 2018.1.2) wants the spring-configuration-metadata.json file to either be in a main source root (src/main/resources/META-INF/ or src/main/java/META-INF/) or in its default output directory for it to pick it up for autocompletion of properties in your source tree. To expand on phospodka's comment, you can add something like this to your build.gradle to satisfy IntelliJ.
task copyConfigurationMetadata(type: Copy) {
from(compileJava) {
include 'META-INF/spring-configuration-metadata.json'
}
into "out/production/classes"
}
compileJava {
dependsOn processResources
finalizedBy copyConfigurationMetadata
}
This answer is a combination of the (at this time) other two answers with a minor twist. In my case this is what "fixed" the issue: (in 2019.03.01-Ultimate)
Turn on the default annotation processing (File>Settings>Build, Execution, Deployment>Annotation Processors>Enable Annotation processing
Select Obtain processors from project classpath
Select Store generated sources relative to module output directory
keep other defaults
click OK
Add the code in #thyme's answer to your build.gradle
EXCEPT instead of into "out/production/classes"
use: into "build/generated/sources/annotationProcessor"
Now you should be able to run gradle clean/build and Intellij should be able to find your "additional metadata" definitions.
Notice that even though the build.gradle code doesn't explicitly mention 'additional-spring-configuration-metadata.json', it is exactly that "additional metadata" that ends up in the annotationProcessor folder as "spring-configuration-metatdata.json" where Intellij finds it.
EDIT: Also note, you need to clean / rebuild after adding any new "additional metadata" items before Intellij will see the new entries (in the freshly regenerated file).

SonarRunner with gradle: Fail to download libraries from server

I have updated Sonar to the 4.5.1 LTS version, and now in my gradle task i have got the following error and can't fix it:
Fail to download libraries from server
build.gradle with sonar runner
sonarRunner {
sonarProperties {
property "sonar.host.url", "http://sonar:9000"
property "sonar.login", ""
property "sonar.password", ""
property "sonar.jdbc.url", "jdbc:mysql://sonar"
property "sonar.jdbc.driverClassName", "com.mysql.jdbc.Driver"
property "sonar.jdbc.username", "sonar"
property "sonar.jdbc.password", "sonar"
property "sonar.profile", "sonar"
property "sonar.projectName", "sonar"
property "sonar.language", "java"
property "sonar.sources", "src/main/java"
property "sonar.binaries", "build";
}
}
i've tried to write this property, but nothing happend:
toolVersion = '2.3' // default
Recent Sonar versions require Gradle 2.2. (2.1 might also work but I'm not sure.)
As a work around, i've installed sonar-runner, and analyzing my gradle project now via sonar-runner 2.4.
As said before, recent SonarQube versions need Gradle 2.2 on client side.
If you cannot or do not want to update your used Gradle version, you can easily copy the Gradle Sonar Runner plugin to your buildSrc project with some slight modifications to not depend on Gradle Injection which does not work for a custom plugin.
I just did this for our project that is still using Gradle 1.12 and it works great.

Can sonar.sources be configured through SonarQube UI so that Eclipse users pickup the same configuration?

By default SonarQube will not analyse test source sets, so I have configured build.gradle's sonarRunner task as below.
I then discovered that I can setup the exclusions list in SonarQube > Configuration > Settings > Exclusions so that this setting can be shared by the eclipse plugin.
Can sonar.sources and similar properties be configured through Sonars UI for a project so that Eclipse users pickup the same configuration? If not is there another way?
build.gradle
apply plugin: 'sonar-runner'
sonarRunner {
sonarProperties {
...
property "sonar.sources", "src/main/java, src/test/java"
property "sonar.tests", ""
property "sonar.binaries", "build/classes/main, build/classes/test"
property "sonar.exclusions", "<list here>"
}
}
No, this is not possible.
If you want to analyse your Gradle-based project in Eclipse, I can understand that you feel like this is a problem because there's no built-in support for Gradle projects in the SQ Eclipse plugin. Still, what you can do is to duplicate those properties in the SonarQube properties of the Eclipse project (righ-click on the project => "Properties > SonarQube > Preview Analysis Properties").

Gradle: Load property file dependent on profile

I want to write a gradle build script, which works in different environments (development, live).
In each environment I have to load different property sets (target directories, databases, etc.).
Is there a gradle way to read a property file dependent on the environment or a profile?
You may want to check out the Gradle Properties Plugin.
Include plugin:
plugins {
id 'net.saliman.properties' version '1.4.2'
}
Create property files:
gradle-dev.properties
or
gradle-prod.properties
Call gradle:
gradle myTask -PenvironmentName=dev
gradle myTask -PenvironmentName=prod
Also have a look at gradle's equivalent of maven's profiles.
I had the same problem, but I use spring and configured it to read properties from classpath:application.properties
In this case you can add this to gradle.build with java plugin
if (project.hasProperty('env')) {
println "Target environment: $env"
sourceSets.main.resources.srcDir "src/main/environment/$env"
}
this for different folders based on environment
So for adding resources based on environment 'dev' you should have 'src/main/environment/dev' folder (with properties file), and invoke gradle: gradle myTask -Penv=dev

Resources