Gradle - Need to execute Cobertura twice w/ diff settings back-to-back for Sonar reporting

I need to execute the Gradle cobertura plugin twice with different settings for one execution of the sonarqube plugin. The reason for needing to execute twice is that the coverage analysis for Groovy and Java need to be in separate report files. Java and Groovy have separate properties for Sonar to say where the report file is for the respective language (sonar.cobertura.reportPath and sonar.groovy.cobertura.reportPath). The reason I cannot leave the analysis in the same file is because the upload to Sonar fails complaining about duplicate metrics. Which I imagine is because of the same file getting read once by the Java plugin and once by the Groovy plugin.

Therefore, from what I can see, I need a coverageGroovy.xml and a coverageJava.xml. So far, I have not found a way to do this in Gradle.

Any ideas are appreciated.

(One idea I had was to use JaCoCo for only the Java tests, but I did not see a way to limit JaCoCo to only the Java files.)

Does Sonarqube not catch Java code if you set the language to Groovy?

I’d solve your use case by parsing a system or project property, something like -DsonarqubeLanguage=java. In your build code that configures the Sonarqube parse the property and have condition logic that sets the language-specific properties. With that code in place you can invoke Gradle twice but with different properties.

What I ended up doing what simply switching to JaCoCo, which as it turns out, seems to work with Groovy. I thought it would not. With JaCoCo, having the analysis for Java and Groovy in the same file does cause any duplicate metric errors in the Sonar upload. So this leads me to suspect that the Cobertura Sonar plugin should be able to handle having one report file for both languages as well, but may not be filtering the results correctly when it is reading the report file.