在项目的gradle 中
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'
classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:3.1" //sonar插件
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
plugins {
id "org.sonarqube" version "3.1"
}
apply from: "sonar.gradle"
// 引入公共配置
apply from: "config.gradle"
config 文件
ext {
sdkVersion = [
compileSdk: 28,
buildTools: "28.0.0",
minSdk : 25,
targetSdk : 28
]
supportLib = [
"appcompat" : "androidx.appcompat:appcompat:1.2.0",
"constraint" : "androidx.constraintlayout:constraintlayout:2.0.4",
"asynclayout" : "androidx.asynclayoutinflater:asynclayoutinflater:1.0.0"
]
wtLib = [
"wt-widget" : "com.autopai.support:widget:R30-1.0.0.zn", // 公共控件
"wt-statistic" : "com.openos.sdk:statistic:1.0.0.+", // 埋点
"wt-virtual-car": "com.openos.sdk:virtualcar-sdk:0.0.2-SNAPSHOT" ,// 虚拟车,末尾加-SNAPSHOT,每次都拉取最新版本
"wt-theme" : "com.autopai.support:skin:2.0.1.+" // 主题切换
]
testLib = [
"junit": "junit:junit:4.13.1"
]
androidTestLib = [
"test-core" : 'androidx.test:core:1.3.0',
"test-runner" : 'androidx.test.ext:junit:1.1.2',
"test-rules" : 'androidx.test:rules:1.3.0',
"espresso-core": 'androidx.test.espresso:espresso-core:3.3.0'
]
}
在app的 gradle 中 可以引入config文件的数组
dependencies {
implementation 'com.openos.sdk:virtualcar-sdk:0.0.2-SNAPSHOT'
testImplementation testLib["junit"]
androidTestImplementation androidTestLib["test-runner"]
androidTestImplementation androidTestLib["espresso-core"]
}
sonar.gradle
import groovy.json.JsonSlurper
/**
* 更新日期 2021-03-16-18-20-26
* 请注意todo注释的地方
*/
ext {
sonar = [
//sonarqube 服务器地址
host : "http://10.1.120.7:9001",
// todo 这里写上sonar后台自己生成的token
username : "e2f2ed6dec45d7a985072f4502857a1bb8da7a61",
// 密码请保持为空即可,不可删除
passwd : "",
projectkey : APK_PACKAGE_NAME + "_${getBranch()}",
projectName: APK_PACKAGE_NAME + "_${getBranch()}(${getGitName()})"
]
}
//启动远程服务器一个sonar任务
sonarqube {
properties {
property "sonar.host.url", sonar.host
property "sonar.verbose", "true"
property "sonar.login", sonar.username
// property "sonar.password", sonar.passwd
property "sonar.sourceEncoding", "UTF-8"
property "sonar.projectKey", sonar.projectkey
property "sonar.projectName", sonar.projectName
property "sonar.exclusions", "**/*Test.java"
property "sonar.core.codeCoveragePlugin","jacoco"
//覆盖报告绝对路径 需要保证路径下有生成的报告report.html
property "sonar.coverage.jacoco.xmlReportPaths","${project.projectDir}/core/build/reports/coverage/debug/report.xml"
}
}
subprojects {
sonarqube {
//todo 如果项目有渠道,请手动指定打开下面注释,设置对应的渠道(注意:如果渠道是s311ica,则下面需要写上s311icaDebug),如果没有渠道则不用做任何操作
// androidVariant 'dev_Debug'
println("flavor=" + name)
properties {
//这里sources,binaries默认会去找对应渠道,不需去指定,否则会出现多渠道分析干扰的情况
}
}
}
//查询sonar分析工程是否通过
task queryStatus() {
try {
//先创建出了一个URL对象,urlPath:是我们访问接口地址
String urlPath = sonar.host + "/api/qualitygates/project_status?projectKey=" + sonar.projectkey
URL url = new URL(urlPath);
//URL链接对象,通过URL对象打开一个connection链接对像
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//设置urlConnection对象链接超时
urlConnection.setConnectTimeout(5000);
//设置urlConnection对象获取数据超时
urlConnection.setReadTimeout(5000);
//设置本次urlConnection请求方式
urlConnection.setRequestMethod("GET");
// 认证登陆
String base64Credentials = new String(Base64.encoder.encode((sonar.username + ":" + sonar.passwd).getBytes("UTF-8")));
urlConnection.setRequestProperty("Authorization", "Basic " + base64Credentials);
//调用urlConnection的链接方法,线程等待,等待的是服务器所给我们返回的结果集
urlConnection.connect();
//获取本次网络请求的状态码
int code = urlConnection.getResponseCode();
//如果本次返回的状态吗是200(成功)
println("http code:" + code)
if (code == 200) {
//调用urlConnection.getInputStream得到本次请求所返回的结果流
InputStream inputStream = urlConnection.getInputStream();
//创建一个BufferedReader,去读取结果流
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String readLine;
StringBuffer buffer = new StringBuffer();
while ((readLine = reader.readLine()) != null) {
buffer.append(readLine);
}
//读取完结果流之后所得到的结果
String result = buffer.toString();
inputStream.close();
def parsedJson = new JsonSlurper().parseText(result)
println(sonar.projectkey + " ---> status:" + parsedJson.projectStatus.status)
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//需要有git环境
def static getBranch() {
String cmd = "git rev-parse --abbrev-ref HEAD"
return cmd.execute().text.trim().toString()
}
//需要有git环境
def static getGitName() {
String cmd = "git config user.email"
return cmd.execute().text.trim().toString()
}
网友评论