我已将sonar属性添加到build.gradle文件中,以便在使用gradle构建项目时将项目上传到服务器。下面是我的build.gradle文件。
import org.tmatesoft.svn.core.wc
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'eclipse'
apply plugin: 'findbugs'
apply plugin: 'sonar-runner'
apply plugin: "jacoco"
group = 'com.example.service'
sourceCompatibility = 1.5
version = '1.0'
jar {
manifest {
attributes 'Implementation-Title': 'Gradle Quickstart', 'Implementation-Version': version
}
}
findbugs {
// make findbugs non build breaking
ignoreFailures = true
}
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
compile 'org.apache.commons:commons-lang3:3.1'
compile "org.codehaus.sonar.runner:sonar-runner-api:2.4"
testCompile group: 'junit', name: 'junit', version: '4.+'
}
test {
systemProperties 'property': 'value'
}
sonarRunner {
sonarProperties {
property "sonar.host.url", "http://localhost:9000"
property "sonar.sourceEncoding", "UTF-8"
property "sonar.projectName", "$project.name"
property "sonar.projectKey", "$project.group:$project.name"
property "sonar.projectBaseDir","$project.projectDir"
property "sonar.working.directory","$project.buildDir/sonar"
property "sonar.jacoco.reportPath", "${project.buildDir}/jacoco/test.exec"
}
}
test {
jacoco {
destinationFile = file("$buildDir/jacoco/test.exec")
}
}
uploadArchives {
repositories {
mavenLocal()
}
}
但是当我从eclipse构建项目时,没有任何事情发生。控制台中没有错误消息,项目也没有上传到声纳服务器。
有没有人知道在使用Gradle构建时如何将项目上传到Sonar服务器?
答案 0 :(得分:0)
查看我的一些帖子,你会找到你想要的东西。主要是,您必须使用:
allprojects {
apply plugin: 'java'
apply plugin: 'codenarc'
apply plugin: 'pmd'
apply plugin: 'findbugs'
apply plugin: 'checkstyle'
apply plugin: 'jacoco'
apply plugin: 'sonar-runner'
//JavaCompile(Works with recent Gradle versions) or Compile (works with Gradle 1.6)
//tasks.withType(Compile) {
tasks.withType(JavaCompile) {
options.debug = true
options.compilerArgs = ["-g"]
}
//You don't need sourceSets if your project is following Maven/Gradle standard structure. In my case, we don't follow src/main/java but have src/java thus, in the global file, I have dont_change_me as the value for sourceSets main java and I'm setting it to src/java in the Project's build.gradle. dont_change_me is required here as I'm/was using sourceSet.main.xx.xxx values in few of the tasks here.
sourceSets {
main {
java {
srcDir 'dont_change_me'
}
resources {
srcDir 'dont_change_me'
}
}
test {
java {
srcDir 'dont_change_me'
}
resources {
srcDir 'dont_change_me'
}
}
integrationTest {
java {
srcDir 'dont_change_me'
}
resources {
srcDir 'dont_change_me'
}
}
acceptanceTest {
java {
srcDir 'dont_change_me'
}
resources {
srcDir 'dont_change_me'
}
}
}
def sonarServerUrl = "kobalori.company.com"
sonarRunner {
sonarProperties {
property "sonar.host.url", "http://$sonarServerUrl:9000"
property "sonar.jdbc.url", "jdbc:h2:tcp://$sonarServerUrl:9092/sonar"
property "sonar.jdbc.driverClassName", "org.h2.Driver"
property "sonar.jdbc.username", "sonar"
property "sonar.jdbc.password", "sonar"
property "sonar.junit.reportsPath", "build/test-results/UT"
property "sonar.surefire.reportsPath", "build/test-results/UT"
//property "sonar.dynamicAnalysis", "reuseReports"
property "sonar.binaries", "build/classes/main"
property "sonar.java.binaries", "build/classes/main"
// property "sonar.libraries", "build/libs/*.jar"
property "sonar.sources", "src/java"
//property "sonar.sources", "src/main/groovy"
//property "sonar.sources", "src/java, src/main/groovy"
//property "sonar.tests", "test/java, src/test/groovy, src/groovy-test, src/java-test"
//property "sonar.tests", "src/test/groovy"
property "sonar.tests", "test/java, src/java-test"
//properties ["sonar.sources"] = sourceSets.main.allSource.srcDirs
//properties ["sonar.tests"] += sourceSets.test.allJava.srcDirs
//properties ["sonar.tests"] += sourceSets.integrationTest.allJava.srcDirs
}
}
//sonarRunner {
// toolVersion = '2.4'
//}
checkstyle {
configFile = new File(rootDir, "config/checkstyle.xml")
ignoreFailures = true
//sourceSets = [sourceSets.main, sourceSets.test, sourceSets.integrationTest]
sourceSets = [sourceSets.main]
}
findbugs {
ignoreFailures = true
sourceSets = [sourceSets.main]
//toolVersion = "2.0.3"
}
// pmd {
// ruleSets = ["basic", "braces", "design"]
// ignoreFailures = true
// }
//Required with Gradle 2.0+ -- 2.0+ -- 2.3
pmd {
ruleSets = ["java-basic", "java-braces", "java-design" ]
ignoreFailures = true
}
//Try this or later version too.
//pmd {
// toolVersion = '4.3'
//}
jacoco {
//toolVersion = "0.6.2.201302030002"
//toolVersion = "0.7.0.201403182114"
//toolVersion = "0.7.1.201404171759"
toolVersion = "0.7.2.201409121644"
reportsDir = file("$buildDir/customJacocoReportDir")
}
task testReport(type: TestReport) {
destinationDir = file("$buildDir/reports/tests/allTests")
//testResultDirs = file("$buildDir/test-results/allTests")
}
test {
maxParallelForks = 5
forkEvery = 50
ignoreFailures = true
//testReportDir = file("$buildDir/reports/tests/UT")
//destinationDir = file("$buildDir/reports/tests/UT")
//testResultsDir = file("$buildDir/test-results/UT")
//testLogging.showStandardStreams = true
//onOutput { descriptor, event ->
// logger.lifecycle("Test: " + descriptor + " produced standard out/err: " + event.message )
//}
//Following Jacoco test section is required only in Jenkins instance extra common file
jacoco {
//The following vars works ONLY with 1.6 of Gradle
// destPath = file("$buildDir/jacoco/UT/jacocoUT.exec")
// classDumpPath = file("$buildDir/jacoco/UT/classpathdumps")
//Following vars works only with versions >= 1.7 version of Gradle
destinationFile = file("$buildDir/jacoco/UT/jacocoUT.exec")
// classDumpFile = file("$buildDir/jacoco/UT/classpathdumps")
}
}
task integrationTest( type: Test) {
//Always run tests
outputs.upToDateWhen { false }
ignoreFailures = true
testClassesDir = sourceSets.integrationTest.output.classesDir
classpath = sourceSets.integrationTest.runtimeClasspath
//testReportDir = file("$buildDir/reports/tests/IT")
//testResultsDir = file("$buildDir/test-results/IT")
//Following Jacoco test section is required only in Jenkins instance extra common file
jacoco {
//This works with 1.6
//destPath = file("$buildDir/jacoco/IT/jacocoIT.exec")
// classDumpPath = file("$buildDir/jacoco/IT/classpathdumps")
//Following works only with versions >= 1.7 version of Gradle
destinationFile = file("$buildDir/jacoco/IT/jacocoIT.exec")
// classDumpFile = file("$buildDir/jacoco/IT/classpathdumps")
}
}
task acceptanceTest ( type: Test) {
//Always run tests
outputs.upToDateWhen { false }
ignoreFailures = true
testClassesDir = sourceSets.integrationTest.output.classesDir
classpath = sourceSets.integrationTest.runtimeClasspath
//testReportDir = file("$buildDir/reports/tests/AT")
//testResultsDir = file("$buildDir/test-results/AT")
//Following Jacoco test section is required only in Jenkins instance extra common file
jacoco {
//This works with 1.6
//destPath = file("$buildDir/jacoco/AT/jacocoAT.exec")
// classDumpPath = file("$buildDir/jacoco/AT/classpathdumps")
//Following works only with versions >= 1.7 version of Gradle
destinationFile = file("$buildDir/jacoco/AT/jacocoAT.exec")
// classDumpFile = file("$buildDir/jacoco/AT/classpathdumps")
}
}
jacocoTestReport {
group = "Reporting"
description = "Generate Jacoco coverage reports after running tests."
//ignoreFailures = true
//executionData = files('build/jacoco/UT/jacocoUT.exec')
//executionData = files('build/jacoco/IT/jacocoIT.exec')
//executionData = files('build/jacoco/UT/jacocoUT.exec', 'build/jacoco/IT/jacocoIT.exec')
//executionData = files(['build/jacoco/UT/jacocoUT.exec', 'build/jacoco/IT/jacocoIT.exec'])
executionData = fileTree(dir: 'build/jacoco', include: '**/*.exec')
reports {
xml{
enabled true
//Following value is a file
destination "${buildDir}/reports/jacoco/xml/jacoco.xml"
}
csv.enabled false
html {
enabled true
//Following value is a folder
destination "${buildDir}/reports/jacoco/html"
}
}
//sourceDirectories = files(sourceSets.main.allJava.srcDirs)
sourceDirectories = files(['src/java','src/main/java', 'src/main/groovy'])
classDirectories = files('build/classes/main')
//------------------------------------------
//additionalSourceDirs = files(['test/java','test/groovy','src/java-test', 'src/groovy-test'])
//additionalSourceDirs = files(['src/test/groovy'])
//additionalSourceDirs += files('src/java-test')
//additionalClassDirs = files('build/classes/test')
//additionalClassDirs += files('build/classes/integrationTest')
//additionalClassDirs += files('build/classes/acceptanceTest')
//------------------------------------------
//IGNORE THE FOLLOWING
//------------------------------------------
//sourceDirectories = files(sourceSets.main.allJava.srcDirs)
//classDirectories = files(sourceSets.main.output.classesDir)
//classDirectories = files("build/classes/main")
//additionalSourceDirs = files("test/java")
//additionalSourceDirs += files("src/java-test")
//additionalClassDirs = files("build/classes/test")
//additionalClassDirs += files("build/classes/integrationTest")
//additionalSourceDirs = fileTree(dir: "test", include: "**/*.*")
//additionalSourceDirs += fileTree(dir: "src/java-test", include: "**/*.*")
//additionalClassDirs = fileTree(dir: "build/*est", include: "**/*.class")
//additionalClassDirs += fileTree(dir: "build/jacoco/*T/classpathdumps/com/thc", include: "**/*.class")
//additionalClassDirectories = fileTree(dir: "build/*est", include: "**/*.class")
//additionalClassDirectories += fileTree(dir: "build/jacoco/*T/classpathdumps/com/thc", include: "**/*.class")
//additionalSourceDirs = files(['test/java', 'src/java-test'])
//additionalSourceDirs = files(sourceSets.test.allJava.srcDirs)
//additionalSourceDirs += files(sourceSets.integrationTest.allJava.srcDirs)
//additionalSourceDirs += files(sourceSets.acceptanceTest.allJava.srcDirs)
// additionalClassDirs = files(['build/jacoco/UT/classpathdumps/com/thc', 'build/jacoco/IT/classpathdumps/com/thc'])
//additionalClassDirs = files(['build/jacoco/UT/classpathdumps/com/thc'])
//additionalSourceDirs = files('test/java', 'src/java-test')
// additionalClassDirs = files('build/jacoco/UT/classpathdumps/com/thc', 'build/jacoco/IT/classpathdumps/com/thc')
//sourceDirectories = files('src/main/java')
// classDirectories = files('build/classes/main')
//sourceDirectories = fileTree('src/main/java')
// classDirectories = fileTree('build/classes/main')
//additionalSourceDirectories = fileTree('test/java', 'test/resources', 'src/java-test')
// additionalClassDirectories = fileTree('build/classes/test', 'build/classes/integrationTest')
//additionalSourceDirs = files(sourceSets.test.allJava.srcDirs,sourceSets.integrationTest.allJava.srcDirs)
//additionalSourceDirs = files(sourceSets.*.allJava.srcDirs)
//additionalSourceDirs += files(sourceSets.test.allJava.srcDirs)
//additionalSourceDirs += files(sourceSets.integrationTest.allJava.srcDirs)
//additionalSourceDirs = files('src/java', 'test/java', 'test/resources', 'src/java-test', 'conf')
//additionalClasseDirs = files('build/classes')
//executionData = files('build/jacoco/test.exec', 'build/jacoco/integrationTest.exec', 'build/jacoco/acceptanceTest.exec')
//additionalSourceDirs = files(sourceSets.integrationTest.allSource.srcDirs)
//additionalSourceDirs = files('src/java', 'test/java', 'test/resources', 'src/java-test', 'conf')
//additionalSourceDirs = files(sourceSets.test.allJava.srcDirs)
//additionalSourceDirs = files(sourceSets.integrationTest.allJava.srcDirs)
//additionalSourceDirs = files([sourceSets.main.allJava.srcDirs)
}
}
你可以忽略//注释掉的行,就像我刚刚玩的那样。 Gradle构建完成后,您需要运行jacocoTestReport任务。
我跑了这个:
gradle clean build jacocoTestReport
(这将通过单元测试获得源代码主要代码的构建,代码覆盖)。
为了获取,IT(集成测试代码覆盖或任何非单元测试覆盖),我首先运行外部JVM(Tomcat等)。然后,我将jacoco代理信息传递给外部JVM(在我的情况下就是Tomcat)就像这样(我将jacocoagent.jar放在某个文件夹中(在我的情况下是tomcat),并告诉外部JVM(Tomcat实例)开始填充Jacoco代码覆盖率,这将通过IT测试而不是单元测试创建jacocoIT.exec文件,用于代码覆盖主要源代码。注意:我在Tomcat启动脚本中嵌入了这个PROJ_EXTRA_JVM_OPTS变量,我将它传递给Tomcat启动命令,以便Tomcat实例知道在哪里创建jacocoIT.exec文件:
export PROJ_EXTRA_JVM_OPTS=-javaagent:tomcat/jacocoagent.jar=destfile=build/jacoco/IT/jacocoIT.exec,append=false
然后我运行Gradle任务来运行IT测试,
gradle integrationTest
然后,一旦它们成功运行,你会注意到jacocoIT.exec有一些文件大小(但它仍然没有完成)。要让Tomcat将代码覆盖率信息刷新到jacocoIT.exec文件中,您现在必须停止tomcat(目标JVM)实例。
我运行停止tomcat脚本,Tomcat现在停止了。此时,我现在发现我的jacocoIT.exec文件现在已经完全填充了更多文件大小(比它停止tomcat之前的文件大小)。
此时,我通过运行运行Gradle jacocoTestReport和SonarRunner任务(通过Unit + IT测试生成COMBINED Unit + IT代码覆盖主要源代码+也通过使用/设置-Dsonar将信息发送到Sonar .xx.yy = value(您可以在命令行或Jenkins中轻松传递(Invoke Gradle Plugin> Switches / options框):
gradle jacocoTestReport -x test -x testClasses sonarRunner
注意:确保正确传递-Dsonar.xx变量。
例如:我在调用sonarRunner任务时传递以下选项/开关(假设在SonarQube实例中,您有项目仪表板,其中包含所需的小部件/已启用):
此处变量 $ {testType} 中包含值= IT 。所有其他变量都在Jenkins Global配置中定义(JENKINS_URL等,或者它们可用作Jenkins默认环境变量)。我正在使用SVN(作为源代码控制工具),因此使用了SVN_URL(如果你的Jenkins作业使用SVN作为Source控件来检查源代码,那么这是Jenkins的默认env变量。)
--stacktrace -x test -x testClasses -Dsonar.jacoco.itReportPath=build/jacoco/${testType}/jacoco${testType}.exec -Dsonar.sources=src/java -Dsonar.java.binaries=build/classes/main -Dsonar.binaries="build/classes/main" -Dsonar.tests=test/java,src/java-test -Dsonar.jacoco.reportPath=build/jacoco/UT/jacocoUT.exec -Dsonar.junit.reportsPath=build/test-results -Dsonar.surefire.reportsPath=build/test-results -Dsonar.dynamicAnalysis=reuseReports -Dsonar.build-stability.url=jenkins:${JENKINS_URL}/job/${JOB_NAME} -Dsonar.scm.url=scm:svn:${SVN_URL}