3

I have a jhipster project with a sonarqube task defined in the project. We have built a react UI under the directory /src/main/ui/src/... and tests under /src/main/ui/src/test/... . Our sonarqube properties are as follows:

sonarqube {
    properties {
        property "sonar.host.url", "https://our_sonar_server"
        property "sonar.projectKey", "MyProjectGroup:MyProject"
        property "sonar.projectName", "MyProject"
        property "sonar.login", sonarLogin
        property "sonar.password", sonarPassword

        property "sonar.links.scm", "https://mygithub/myProject.git"
        property "sonar.links.scm_dev", "https://mygithub/MyProject.git"
        property "sonar.links.ci", ciLink

        if (pullRequest != null) {
            property "sonar.analysis.mode", "preview"
            property "sonar.github.pullRequest", pullRequest
            property "sonar.github.repository", "MyProjectGroup/MyProject"
            property "sonar.github.oauth", "my_oauth_hash"
            property "sonar.github.endpoint", "https://mygithubapi/v3"
        }
    }
}

I currently, sonar only picks up the java which is under the standard /src/main/java. I have tried adding property "sonar.sources", "src/main/ui/src/*.js" property "sonar.test", "src/main/ui/src/test/**" As well as a few variations. However, the jenkin build which invokes this via a shell step calling "./gradlew sonar -Pprod -x test" fails with an error like "invalid value" or similar. I have not been able to find an example for the correct syntax.

Edit: I was able to get the sonar build to pass in the PR using property "sonar.sources", "src/main/ui/src" property "sonar.test", "src/main/ui/src/test"

However, when checking the project on the sonar server, I'm still not seeing any of the javascript files being referenced.

2 Answers 2

2

After merging my last edit, the js files show up in sonarqube. The js tests are showing up as well, though they do not show any coverage percentage. Just the number of unit tests and how many pass. There's probably some more config necessary for that, but that wasn't really part of the original question.

After speaking with my manager, we decided to narrow the source sets to two subdirectories, "src/main/ui/src/app" and "src/main/ui/src/layouts" so that tests will not show up under source sets. The documentation suggests that you can add a comma separated list, so I created an external property and referenced those in the actual property:

ext.jsAppSrc = "src/main/ui/src/app, src/main/ui/src/layout"
ext.jsLayoutSrc = "src/main/ui/src/layout"
ext.jsTest = "src/main/ui/test"
...
property "sonar.sources", jsAppSrc, jsLayoutSrc
property "sonar.test", jsTest

The props evaluate correctly, but the sonar task fails with the error Could not find method property() for arguments [sonar.sources, src/main/ui/src/app, src/main/ui/src/layout on org.sonarqube.gradle.SonarQubeProperties.

I then refactored to put the comma separated list in a single property, which then completed successfully.

ext.jsAppSrc = "src/main/ui/src/app, src/main/ui/src/layout"
ext.jsTest = "src/main/ui/test"
...
property "sonar.sources", jsAppSrc
property "sonar.test", jsTest
1

You can try defining a custom source set: https://docs.sonarqube.org/latest/analyzing-source-code/scanners/sonarscanner-for-gradle/#custom-source-sets

// build.gradle
sonar {
    properties {
        properties["sonar.sources"] += sourceSets.custom.allSource.srcDirs
        properties["sonar.tests"] += sourceSets.integTest.allSource.srcDirs
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.