18
votes

I'm new to gradle and I'm getting a build error that I don't really understand. My project is just an empty shell with the directory structure and no java source code. Here is my root build.gradle file

allprojects {
    //Put instructions for all projects
    task hello << { task -> println "I'm $task.project.name" }
}

subprojects {
    //Put instructions for each sub project
    apply plugin: "java"
    repositories {
        mavenCentral()
    }
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.+'
}

when I execute the gradle build command the build fails because it doesn't know the testCompile method with this message:

Could not find method testCompile() for arguments [{group=junit, name=junit, version=4.+}] on root project

I use Gradle 2.5.

I've understood that this method is a part of the java plugin which I've loaded. I don't see what went wrong, can you help?

3
that dependencies block probably belongs inside the subprojects block, yeah?dnault
It's complaining that testCompile doesn't exist because you haven't applied the 'java' plugin to this build file.dnault
As I already said, i'm new to gradle, so how can i apply the java plugin to this build file ?Jib'z
Unless the root project has java source code, it doesn't make sense to apply the java plugin... and it doesn't make sense to declare dependencies for the root project. If your intent is to say that each of the subprojects depends on JUnit, then that dependencies block belongs inside the subprojects block.dnault

3 Answers

20
votes

In case anyone comes here based on the Could not find method testCompile() error, by now the more probable cause is that you need to replace the deprecated testCompile by testImplementation. See What's the difference between implementation and compile in Gradle?

10
votes

The java plugin is only applied to subprojects, so the testCompile configuration, added by the java plugin, can only be used in subprojects. The below works:

allprojects {
    //Put instructions for all projects
    task hello << { task -> println "I'm $task.project.name" }
}

subprojects {
    //Put instructions for each sub project
    apply plugin: "java"
    repositories {
        mavenCentral()
    }



    dependencies {
        testCompile group: 'junit', name: 'junit', version: '4.+'
    }
}
-1
votes

It's saying that it can't find the method testCompile for the arguments check that you spelt it correctly making sure the name and group which you have as "junit" are all correct and also the version is correct another fix for this issue is adding the testCompile line in sub projects block.