簡體   English   中英

多模塊聲納 gradle 項目

[英]Sonar with multi-module gradle project

我有基於 java 和 kotlin 的多模塊 gradle 項目。 我正在嘗試為此設置聲納分析。 我在根項目中配置了聲納並使用 CircleCI 運行分析。 sonarcloud 中的結果僅適用於其中一個子項目。

我的項目結構如下:

  • 項目A/build.gradle
  • 項目B/build.gradle
  • 項目C/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")
    }
}

需要在子項目塊之外包含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")
    }
}

我的 kotlin 多模塊項目的解決方案與junit 5 & spring boot ,使用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 }}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM