简体   繁体   English

使用Gradle为多模块项目设置SonarQube 5.4:无法通过ClassLoader访问类XYZ

[英]Set up SonarQube 5.4 for multi-module project with Gradle: Class XYZ is not accessible through the ClassLoader

I'm struggling to set up correctly SonarQube 5.4 for a multi-module project. 我正在努力为多模块项目正确设置SonarQube 5.4。 I've read here , here and here , all of the provided solutions doesn't solve my errors: I got during a Sonar analyze for every class I created (not for libraries I included) a warning: 我在这里阅读, 这里这里 ,所有提供的解决方案都没有解决我的错误:我在Sonar分析期间为我创建的每个类(不包括我包含的库)提出警告:

"Class XYZ is not accessible through the ClassLoader." “无法通过ClassLoader访问类XYZ。”

and I get no results in the Sonar dashboard (neither on localhost nor on Jenkins), not even the time of the analyze got updated. 我在Sonar仪表板上没有得到任何结果(在本地主机和Jenkins上都没有),甚至在分析更新的时候也没有。 I'm using Gradle 2.10, here is my root Gradle file: 我正在使用Gradle 2.10,这是我的根Gradle文件:

buildscript {
repositories {
    jcenter()
    mavenCentral()
    maven {
        url "https://plugins.gradle.org/m2/"
    }
}
dependencies {
    classpath 'com.android.tools.build:gradle:2.0.0'
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.0-rc1"
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
    }
}
apply from: 'buildsystem/dependencies.gradle'
apply plugin: "org.sonarqube"

subprojects {
sonarqube {
    properties {
        property "sonar.host.url", "http://localhost:9000/"
        property "sonar.sources", "./src/main/java"
        property "sonar.java.binaries", "./build/"
        property "sonar.projectKey", "net.project"
        property "sonar.projectName", "Project Android"
        property "sonar.projectVersion", "0.5"
        property "sonar.sourceEncoding", "UTF-8"
        }
    }
}
allprojects {
ext {
    androidApplicationId = 'net.project'
    androidVersionCode = 1
    androidVersionName = "0.5"
    testInstrumentationRunner = "android.support.test.runner.AndroidJUnitRunner"
    testApplicationId = 'com.beatclip.android.presentation.test'
    }

repositories {
    jcenter()
    }
}

task wrapper(type: Wrapper) {
description 'Creates the gradle wrapper.'
gradleVersion '2.10'
}

Could be Dagger2 the problem? 可能是Dagger2的问题? I have no SonarQube configurations in the Gradle files for the modules set, since this example from SonarQube doesn't provide any Sonar configs in module Gradle files as well. 我在模块集的Gradle文件中没有SonarQube配置,因为SonarQube的这个示例也没有在模块Gradle文件中提供任何Sonar配置。

Any help or example is appreciated! 任何帮助或示例表示赞赏! :) :)

While I have no clue what's going on with gradle and how to set it up correctly, I somehow managed to get partially better results in the scan (still some classes fail to load, but mostly external ones, so I hope the scan works at least on internal ones). 虽然我不知道gradle会发生什么以及如何正确设置它,但我在某种程度上设法在扫描中得到了更好的结果(仍然有些类无法加载,但大多数是外部的,所以我希望扫描至少可以工作内部的)。

I'm using the sonarqube plugin 2.0.1, sonarqube 4.5.7, gradle 2.14.1. 我正在使用sonarqube插件2.0.1,sonarqube 4.5.7,gradle 2.14.1。

I'm setting this for final application module, so this will not help you directly with "multi-module", but maybe something similar can be put into subprojects part? 我正在为最终的应用程序模块设置这个,所以这不会直接帮助你使用“多模块”,但也许类似的东西可以放入subprojects部分? As I don't understand groovy (1), it's very difficult and painful for me to change anything, took me several hours before I managed at least to get here. 因为我不理解groovy(1),所以改变任何东西都是非常困难和痛苦的,在我至少到达这里之前花了几个小时。

(1) anything I try to write works in the other way than I expected, for example I would expect adding the classFilesTree (file tree instance) directly to property "sonar.java.binaries" would be enough to traverse the files, but I ended up with collect + {}.each to expand it before setting the property (I had several issues at that point, so I'm not sure this was correct step, and now it works, so I'm not going to experiment). (1)我尝试编写的任何东西都以我预期的方式工作,例如我希望将classFilesTree (文件树实例)直接添加到property "sonar.java.binaries"就足以遍历文件了,但是我最后使用collect + {} .each来扩展它,然后再设置属性(此时我遇到了几个问题,所以我不确定这是正确的步骤,现在它有效,所以我不打算进行实验) 。 I simply don't understand these high level languages, I can never imagine how it lands on the CPU and what executes where with what in memory, so I'm struggling a lot, my mind is trained to work over bytes, not abstractions. 我根本不理解这些高级语言,我无法想象它是如何落在CPU上的,以及什么在内存中执行的内容,所以我挣扎了很多,我的思维训练工作超过字节,而不是抽象。

plugins {  //instead of the old way with dependecies + apply plugin
    id "org.sonarqube" version "2.0.1"
}

// path to Android SDK jar file, requires sdk.dir=/path/file in local.properties
def androidSdkJarPath;
afterEvaluate {
    def rootDir = project.rootDir
    def localProperties = new File(rootDir, "local.properties")
    if (localProperties.exists()) {
        Properties properties = new Properties()
        localProperties.withInputStream { instr ->
            properties.load(instr)
        }
        def sdkDir = properties.getProperty('sdk.dir')
        androidSdkJarPath = sdkDir + "/platforms/" + android.compileSdkVersion + "/android.jar"
        logger.info('androidSdkJarPath: ' + androidSdkJarPath)
    }
}

sonarqube {
    properties {
        // ... other module configuration for sonar...

        FileTree classFilesTree = fileTree(dir: 'build/intermediates/classes', include: '**/release/**')
        FileTree jarFilesFromAarsTree = fileTree(dir: 'build/intermediates/exploded-aar', include: '**/jars/*.jar', exclude: ['**/debug/jars/**', '**/snapshot/jars/**'])

        def classFiles = []
        def jarFilesFromAars = []
        classFilesTree.collect { relativePath(it) }.each { classFiles += it }
        jarFilesFromAarsTree.collect { relativePath(it) }.each { jarFilesFromAars += it }
//        logger.warn('classFiles: ' + classFiles )
//        logger.warn('jarFilesFromAars: ' + jarFilesFromAars )
        // Still MIA (on my project): android.support.v4.**, ...

        property "sonar.java.binaries", classFiles
        property "sonar.libraries", [jarFilesFromAars, androidSdkJarPath]
    }
}

subprojects {
    sonarqube {
        properties["sonar.sources"] += "src/main/java"
        properties["sonar.test"] += ["src/androidTest/java", "src/test/java"]
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM