3

我知道为什么我的依赖不起作用。这是我的配置:

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.internal.artifacts.dsl.dependencies.DefaultDependencyHandler_Decorated@785c1069.

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

谢谢你的帮助

4

2 回答 2

9

testCompile配置由插件java声明。因此,在您可以将依赖项添加到 之前testCompile,您必须先添加apply plugin: "java"subprojects.

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

于 2014-03-28T03:41:50.140 回答
1

试试这个

ext {
   junitVersion = "4.11"

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

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

dependenciesDSL依赖于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 符号字符串作为第二个 arg &因此它将doAdd像往常一样进入方法(字符串符号由相关人员解析NotationParser(在这种情况下org.gradle.api.internal.notations.DependencyStringNotationParser)。

相反,您当前的使用使它认为您正在寻求调用名为的方法DependencyHandler#testCompile

于 2014-03-27T22:30:20.490 回答