【问题标题】:Sonar with multi-module gradle project带有多模块 gradle 项目的声纳
【发布时间】:2020-09-12 21:12:41
【问题描述】:

我有基于 java 和 kotlin 的多模块 gradle 项目。我正在尝试为此设置声纳分析。我在根项目中配置了声纳并使用 CircleCI 运行分析。 sonarcloud 中的结果仅适用于其中一个子项目。

我的项目结构如下:

  • projectA/build.gradle
  • ProjectB/build.gradle
  • ProjectC/build.gradle
  • build.gradle

这是我的根 build.gradle。

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.61'
        classpath 'io.spring.gradle:dependency-management-plugin:1.0.9.RELEASE'
        classpath 'org.springframework.boot:spring-boot-gradle-plugin:2.2.4.RELEASE'
        classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.7"
    }
}

ext {
    springCloudVersion = 'Hoxton.SR1'
    springBootVersion = '2.2.4.RELEASE'
}

configurations.all {
    resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}

allprojects {
    group = 'com.organiz'
    version = '1.0.0-SNAPSHOT'

    repositories {
        mavenCentral()
        maven {
            //        url ="s3url"  //   only for releases
            url ="s3url"  //  only for snapshots
            credentials(AwsCredentials) {
                accessKey project.accessKey
                secretKey project.secretKey
            }
        }
    }
}

subprojects {

    apply plugin: 'java'
    apply plugin: 'kotlin'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'idea'
    apply plugin: 'org.sonarqube'
    sourceCompatibility = '1.8'

    dependencies {
        implementation 'org.springframework.boot:spring-boot-starter'
        testImplementation('org.springframework.boot:spring-boot-starter-test') {
            exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
        }
        implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
        implementation('someappcommon:1.0.0-SNAPSHOT') { changing = true }
        implementation("com.h2database:h2:1.4.200")
    }

    repositories {
        mavenCentral()
        jcenter()
        maven {
            url "someurlhere"
        }
        someappMavenRepoUrl.split(',').each { repoUrl -> maven { url repoUrl } }
    }

    test {
        useJUnitPlatform()
    }

    compileKotlin {
        kotlinOptions {
            jvmTarget = "1.8"
        }
    }
    compileTestKotlin {
        kotlinOptions {
            jvmTarget = "1.8"
        }
    }
    sonarqube {
         properties {
    property "sonar.projectKey", "projectKey"
    property "sonar.organization", "org"
    property "sonar.host.url", "https://sonarcloud.io"
    property "sonar.verbose", "true"
  }
}
}

project(':project1') {
    dependencies {
        implementation project(":common")
    }
}

project(':project2') {
    dependencies {
        implementation project(":common")
    }
}

【问题讨论】:

    标签: java gradle sonarqube multi-module


    【解决方案1】:

    需要在子项目块之外包含sonarqube,然后将分析所有子模块并导出报告。

    buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.61'
            classpath 'io.spring.gradle:dependency-management-plugin:1.0.9.RELEASE'
            classpath 'org.springframework.boot:spring-boot-gradle-plugin:2.2.4.RELEASE'
            classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.7"
        }
    }
    
    ext {
        springCloudVersion = 'Hoxton.SR1'
        springBootVersion = '2.2.4.RELEASE'
    }
    
    configurations.all {
        resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
    }
    
    allprojects {
        group = 'com.organiz'
        version = '1.0.0-SNAPSHOT'
    
        repositories {
            mavenCentral()
            maven {
                //        url ="s3url"  //   only for releases
                url ="s3url"  //  only for snapshots
                credentials(AwsCredentials) {
                    accessKey project.accessKey
                    secretKey project.secretKey
                }
            }
        }
    }
    
        apply plugin: 'org.sonarqube'
        sonarqube {
             properties {
        property "sonar.projectKey", "projectKey"
        property "sonar.organization", "org"
        property "sonar.host.url", "https://sonarcloud.io"
        property "sonar.verbose", "true"
      }
    }
    
    subprojects {
    
        apply plugin: 'java'
        apply plugin: 'kotlin'
        apply plugin: 'org.springframework.boot'
        apply plugin: 'idea'
        sourceCompatibility = '1.8'
    
        dependencies {
            implementation 'org.springframework.boot:spring-boot-starter'
            testImplementation('org.springframework.boot:spring-boot-starter-test') {
                exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
            }
            implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
            implementation('someappcommon:1.0.0-SNAPSHOT') { changing = true }
            implementation("com.h2database:h2:1.4.200")
        }
    
        repositories {
            mavenCentral()
            jcenter()
            maven {
                url "someurlhere"
            }
            someappMavenRepoUrl.split(',').each { repoUrl -> maven { url repoUrl } }
        }
    
        test {
            useJUnitPlatform()
        }
    
        compileKotlin {
            kotlinOptions {
                jvmTarget = "1.8"
            }
        }
        compileTestKotlin {
            kotlinOptions {
                jvmTarget = "1.8"
            }
        }
    }
    
    project(':project1') {
        dependencies {
            implementation project(":common")
        }
    }
    
    project(':project2') {
        dependencies {
            implementation project(":common")
        }
    }
    

    【讨论】:

      【解决方案2】:

      我对带有junit 5 和spring boot 的kotlin 多模块项目的解决方案,使用gradle-jacoco-log 插件:

      1.☝? 在你的根build.gradle.kts:

      plugins {
          kotlin("jvm") version "1.5.0"
          id("org.sonarqube") version "3.2.0"
          id ("org.barfuin.gradle.jacocolog") version "1.2.4"
      }
      
      
      sonarqube {
          val sonarOrganization: String by project
          val sonarProjectKey: String by project
          val sonarLogin: String by project
      
          properties {
              properties(
                  hashMapOf<String, String>(
                      "sonar.login" to sonarLogin,
                      "sonar.projectKey" to sonarProjectKey,
                      "sonar.organization" to sonarOrganization,
                      "sonar.host.url" to "https://sonarcloud.io",
                      "sonar.coverage.jacoco.xmlReportPaths" to "${buildDir}/reports/jacoco/jacocoAggregatedReport/jacocoAggregatedReport.xml"
                  )
              )
          }
      }
      

      2.?? 在有测试的后端子项目中:

      plugins{
          jacoco
      }
      
      jacoco { toolVersion = "0.8.7" }
      
      tasks{
          test { useJUnitPlatform()
          jacocoTestReport { reports { xml.required.set( true ) } }
          finalizedBy(jacocoTestReport) 
          }
      }
      
      

      可选步骤(示例如何使用)

      3.?? 在您的 CI 文件中(在我的情况下为.github/workflows/master.yml):

      name: Master CI
      on:
        push:
          branches:
            - master
      
      
      jobs:
        DEPLOY:
          runs-on: ubuntu-latest
          steps:
            - uses: actions/checkout@v2
      
            - name: ? Set up JDK 16
              uses: actions/setup-java@v1
              with:
                java-version: 16
      
            - name: ? Test
              run: |
                chmod +x ./gradlew
                ./gradlew test
      
            # Create `build/jacoco/jacocoMergeSubprojects.exec` 
            - name: ⛈ 1) Jacoco merge subprojects reports
              run: ./gradlew jacocoMergeSubprojects -x test
      
            # Create `build/reports/jacoco/jacocoAggregatedReport/jacocoAggregatedReport.xml`
            - name: ⛈ 2) Jacoco Jacoco aggregated report
              run: ./gradlew jacocoAggregatedReport -x test
      
            - name: ⛈ 3) Sonarqube report
              run: ./gradlew sonarqube -x test
              env:
                GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      

      【讨论】:

        猜你喜欢
        • 2013-03-21
        • 2017-05-09
        • 2018-09-09
        • 2018-07-04
        • 2021-02-23
        • 1970-01-01
        • 2016-06-11
        • 2015-09-28
        • 2020-12-11
        相关资源
        最近更新 更多