Sonarqube how to configure gradle sub-projects cor

2019-06-03 15:03发布

问题:

My gradle project structure looks something like this:

geode-core
geode-lucene
extensions/geode-modules
extensions/geode-modules-session

For extensions sub-projects, gradle tasks would thus be referenced with extensions/geode-modules:build for example.

When I try and use SonarQube in Gradle I'm getting the following error (this is the 1.2 SonarQube Gradle plugin):

Validation of project reactor failed:
  o "io.pivotal.gemfire:extensions/geode-modules" is not a valid project or module key. Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.
  o "io.pivotal.gemfire:extensions/geode-modules-session" is not a valid project or module key. Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.

So the / in the module name is causing a problem. To fix it I tried following the solution on this thread: http://sonarqube-archive.15.x6.nabble.com/Is-not-a-valid-project-or-module-key-when-Upgrade-sonar-3-0-to-4-0-td5021412.html

My gradle config now looks like this:

sonarqube {
  properties {
    property "sonar.modules", "extensions.geode-modules"
    ...
    property "extensions.geode-modules.sonar.projectName", "geode-modules"
    property "extensions.geode-modules.sonar.sources", "src/main/java"
  }
}

Same error. Also, this didn't work either:

sonarqube {
  properties {
    property "sonar.modules", "extensions/geode-modules"
    ...
    property "extensions/geode-modules.sonar.projectName", "geode-modules"
    property "extensions/geode-modules.sonar.sources", "src/main/java"
  }
}

Any thoughts on how to get this to work properly?

回答1:

A generic approach is to replace all slashes in a module name. This way, you don't need to configure the various properties. Add this part in your root build.gradle file:

subprojects {

    sonarqube {
        String regex = "(.*)/(.*)"
        String projectKey = project.name.replaceAll(regex, "\$1:\$2")
        String sonarModuleKey = rootProject.group + ':' + rootProject.name + ':' + projectKey

        properties {
            property "sonar.moduleKey", sonarModuleKey
        }
    }
}


回答2:

you need to add to your module a submodule and use the Property file example:

projectMy-
       -moduleOne
             --oneSub1
             --oneSub2
       -moduleTwo
             --twoSub1
             --twoSub2
-sonar-project.properties

properties:

sonar.projectName=\:projectMy
sonar.projectKey=projectMy
sonar.projectVersion=0.0.0.1
sonar.host.url=<sonar.host:port>
sonar.jdbc.driverClassName=<jdbc.driver.name>
sonar.jdbc.url=<your.jdbc>
sonar.jdbc.username=<your.username>
sonar.jdbc.password=<your.password>
sonar.forceAnalysis=true
sonar.sourceEncoding=UTF-8
sonar.sources=src/main/java
sonar.modules=moduleOne,moduleTwo
moduleOne.sonar.modules=oneSub1,oneSub2
moduleTwo.sonar.modules=twoSub1,twoSub2

and build.gradle task

apply plugin: 'sonar-runner'
version = '<version>'
repositories {
   mavenCentral()
}
sonarRunner {

   sonarProperties {
      property "sonar.projectVersion", project.version
   }
}

if you want only gradle

sonarqube {
  properties {
    property "sonar.modules", "extensions"
    ...
    property "extensions.sonar.modules", "geode-modules"
    property "geode-modules.sonar.sources", "src/main/java"
  }
}