配置带有子模块的Android项目与sonarqube gradle插件一起使用的正确方法是什么?谷歌并不是我的朋友,但我可能错过了一些基本的东西. (我搜索与android构建目录和子模块相关的sonarqube问题.没有有用的结果.)
在很高的层次上,我正在使用具有以下结构的Android项目.
git_repository
|----- android_project
|--- app
|--- SDK
|- api
git_repository包含README.md和其他*文件,包括android_project. android_project包含应用程序,以及SDK中的git子模块.这个git子模块包含app需要运行的api代码.
问题是当我尝试运行sonarqube时,它似乎在寻找不存在的文件/目录.对于一个更简单的最小项目,我没有这个问题.我计划在星期一设置一个使用子模块的最小项目,但我想在离开周末前把这个问题拿出来.
$./gradlew clean sonarqube
* snip *
:sonarqube
Invalid value for sonar.java.test.libraries
:sonarqube FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':sonarqube'.
> No files nor directories matching '/Users/my_username/git_repository/android_project/app/build/intermediates/dependency-cache/debug'
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or -- debug option to get more log output.
BUILD FAILED
Total time: 9.897 secs
$
此Gradle任务在MacOS / Android Studio命令行设置上失败,但最终目标是使配置与Jenkins一起使用.
我的settings.gradle和build.gradle文件如下.显然我做错了什么.
git_repository / android_project / settings.gradle完整列表
include ':app', ':api'
project(':api').projectDir = new File('SDK/api')
git_repository / android_project / build.gradle完整列表
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
classpath 'org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
apply plugin: 'org.sonarqube'
allprojects {
repositories {
jcenter()
}
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
}
//subprojects {
// sonarqube {
// properties {
// // property "sonar.sources", "src"
// }
// }
//}
//sonarqube {
// properties {
//// property "sonar.exclusions", "file:**/SDK/**"
// }
//}
subprojects {
sonarqube {
properties {
property "sonar.sourceEncoding","UTF-8"
property "sonar.sources","src/main/java"
property "sonar.java.binaries", "./build/"
property "sonar.tests","src/androidTest"
// property "sonar.exclusions","build,build/**,**/*.png"
property "sonar.import_unknown_files", true
property "sonar.android.lint.report", "./build/outputs/lint-results.xml"
}
}
}
project(":api") {
sonarqube {
skipProject = true
}
}
解决方法:
是的,对于具有多个模块的项目来说,它有点棘手,它使用适当的通配符实现.
跟着这些步骤:
>在包含所有子模块的主模块中放置
sonarqube.gradle文件
>在主模块的build.gradle文件中添加maven插件和
类依赖
这是上面提到的两个文件的示例:
sonarqube.gradle
apply plugin: "org.sonarqube"
sonarqube {
//noinspection GroovyAssignabilityCheck
properties {
//noinspection GroovyAssignabilityCheck
property "sonar.projectName", "appar"
//noinspection GroovyAssignabilityCheck
property "sonar.projectVersion", "1.0"
//noinspection GroovyAssignabilityCheck
property "sonar.analysis.mode", "publish"
//noinspection GroovyAssignabilityCheck
property "sonar.language", "java"
//noinspection GroovyAssignabilityCheck
property 'sonar.sourceEncoding', "UTF-8"
//noinspection GroovyAssignabilityCheck
property "sonar.sources", "./src/main"
// noinspection GroovyAssignabilityCheck
property "sonar.exclusions", "src/main/java/com/appar/model/**, **/*Entity.java"
//noinspection GroovyAssignabilityCheck
property "sonar.host.url", "http://192.168.21.33:9000"
//noinspection GroovyAssignabilityCheck
property "sonar.login", "admin"
//noinspection GroovyAssignabilityCheck
property "sonar.profile", "fulllint"
//noinspection GroovyAssignabilityCheck
property 'sonar.import_unknown_files', true
//noinspection GroovyAssignabilityCheck
property "sonar.android.lint.report", "./build/outputs/lint-results-debug.xml"
//noinspection GroovyAssignabilityCheck
property "sonar.password", "admin"
//noinspection GroovyAssignabilityCheck
property "sonar.java.binaries", "build/"
}
}
的build.gradle
buildscript {
repositories {
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.0.1"
classpath 'com.dicedmelon.gradle:jacoco-android:0.1.1'
}
}
allprojects {
repositories {
jcenter()
}
}
然后从sonarqube.gradle应用于单独模块的build.gradle
这是一个子模块的build.gradle示例:
apply plugin: 'com.android.library'
apply from: '../sonarqube.gradle'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
minSdkVersion 21
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
testCoverageEnabled = true
}
}
}
dependencies {
compile project(':java-library')
testCompile 'junit:junit:4.12'
testCompile "org.robolectric:robolectric:3.1.4"
}
只需将此行与所有其他应用行一起放置,如上面的文件所示
apply from: '../sonarqube.gradle'
将sonarqube.gradle应用于子模块中的所有build.gradle文件后.
只需运行命令
./gradlew sonarqube
相信我,项目将成功构建并被推入sonarqube服务器,并将显示错误结果
如果你正在使用findbugs在推送之前制作项目,否则构建将失败,因为findbugs需要字节码来分析.
不要使用该属性
//noinspection GroovyAssignabilityCheck
property "sonar.projectKey", "appar_app"
这个sonar.projectKey属性. SonarQube使用它来识别声纳数据库中的每个项目(或模块).
因此,如果所有模块都具有相同的projectKey值,SonarQube将更新其数据库中的单个项目.
不用担心,此属性会自动设置为每个模块的文件夹名称.