我正在做一个多项目的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
关闭中的所有子项目声明了存储库。也可以单独或使用闭包为根项目定义存储库allprojects
您的构建应该可以正常工作,至少在这方面是这样。