2014-03-27 126 views
3

我有任何想法为什么我的依赖不工作。 这是我的配置:在gradle中添加依赖关系

ext { 
    junitVersion = "4.11" 

    libs = [ 
      junit : dependencies.create("junit:junit:4.11") 
    ] 
} 

configure(subprojects) { subproject -> 
    dependencies { 
     testCompile(libs.junit) 
    } 
} 

我有错误:

* What went wrong: 
A problem occurred evaluating root project 'unit590'. 
> Could not find method testCompile() for arguments [DefaultExternalModuleDependency{group='junit', name='junit', version='4.11', configuration='default'}] on org.gradle.api.interna[email protected]785c1069. 

* Try: 
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. 

感谢所有帮助

回答

9

testCompile配置由java插件声明。所以在你添加依赖到testCompile之前,你必须要apply plugin: "java"subprojects

PS:libs的声明可以简化,如马特的答案所示。 configure(subprojects) { ... }可简化为subprojects { ... }

+0

非常感谢,我是新手gradle用户:) –

1

试试这个,而不是

ext { 
    junitVersion = "4.11" 

    libs = [ 
      junit : "junit:junit:${junitVersion}" 
    ] 
} 

configure(subprojects) { subproject -> 
    dependencies { 
     testCompile libs.junit 
    } 
} 

dependencies DSL依赖于Groovy的methodMissing impl和这个,在版本中gradle这个我有手,看起来像这样

public Object methodMissing(String name, Object args) { 
    Configuration configuration = configurationContainer.findByName(name) 
    if (configuration == null) { 
     if (!getMetaClass().respondsTo(this, name, args.size())) { 
      throw new MissingMethodException(name, this.getClass(), args); 
     } 
    } 

    Object[] normalizedArgs = GUtil.collectionize(args) 
    if (normalizedArgs.length == 2 && normalizedArgs[1] instanceof Closure) { 
     return doAdd(configuration, normalizedArgs[0], (Closure) normalizedArgs[1]) 
    } else if (normalizedArgs.length == 1) { 
     return doAdd(configuration, normalizedArgs[0], (Closure) null) 
    } 
    normalizedArgs.each {notation -> 
     doAdd(configuration, notation, null) 
    } 
    return null; 
} 

这将要求每个语句中dependencies{}和拨打电话加入提供了一个很好的,简单的DSL /创建等

我的版本会将testcompile作为第一个字符串arg和GAV表示法字符串作为第二个参数&,因此它将像往常一样进入doAdd方法(字符串表示法由相关的NotationParser(本例中为org.gradle.api.internal.notations.DependencyStringNotationParser)解析。

您当前使用的,而不是使其觉得你正在寻求调用一个方法叫做DependencyHandler#testCompile