cobertura + gradle - 为什么我不能 运行 cobertura 任务?

cobertura + gradle - why can't I run the cobertura task?

我正在处理多个项目 gradle 项目,主要的 build.gradle 是这样的:

buildscript {
repositories {
    mavenLocal()
    mavenCentral()
}
    dependencies {
        classpath 'net.saliman:gradle-cobertura-plugin:2.2.4'
    }
}

plugins {
    id "org.sonarqube" version "2.2.1"
}

apply plugin: 'groovy'
apply plugin: 'cobertura'

def getVersionName = { ->
    def stdout = new ByteArrayOutputStream()
    exec {
        commandLine 'git', 'describe', '--tags', '--always'
        standardOutput = stdout
    }
    return stdout.toString().trim()
}

ext {
    springFrameworkVersion = '4.3.4.RELEASE'
}

subprojects {
    apply plugin: 'eclipse'
    apply plugin: 'idea'
    apply plugin: 'java'
    apply from: "${parent.projectDir.canonicalPath}/cobertura.gradle"

    sourceCompatibility = 1.8
    targetCompatibility = 1.8

    repositories {
        mavenCentral()
    }

    dependencies {
        compile("org.springframework:spring-context:${springFrameworkVersion}")

        testCompile("junit:junit:4.+")
        testCompile("org.springframework:spring-test:${springFrameworkVersion}")
    }
    jar {
        version = getVersionName()
    }
}

project(':projectA') {

}

project(':projectB') {
   dependencies {
            compile project(':projectA')
        }
    }

project(':projectC') {
    dependencies {
        compile project(':projectB')
    }
}

cobertura {
    coverageFormats = ['html', 'xml']
    coverageIgnoreTrivial = true
    coverageIgnores = ['org.slf4j.Logger.*']
    coverageReportDir = new File("$buildDir/reports/cobertura")
}

但是当我尝试 运行 带有 cobertura 任务的 gradle 包装器时,它失败了并且控制台输出是:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':instrument'.
> Could not resolve all dependencies for configuration ':cobertura'.
   > Cannot resolve external dependency net.sourceforge.cobertura:cobertura:2.0.3 because no repositories are defined.
     Required by:
         :main-project:unspecified

这是怎么回事?

就像错误信息告诉你的那样。

你已经在你的根项目中声明了对 net.sourceforge.cobertura:cobertura:2.0.3 的依赖(因为它没有明确添加,我猜 Gradle cobertura 插件会隐式添加它,它的文档可能会这样说) 但您没有为根项目定义任何存储库。您只为构建脚本(解析 Gradle 插件和东西)和 subprojects 闭包中的所有 sub-projects 声明了存储库。也为根项目定义存储库,可以单独定义,也可以使用 allprojects 闭包,您的构建应该可以正常工作,至少在这方面是这样。