gradlebuild.gradle

Gradle cannot find method compile()?


For reasons beyond my control, we're using a very old version of Gradle.

$ ./gradlew --version

------------------------------------------------------------
Gradle 4.10.3
------------------------------------------------------------

Build time:   2018-12-05 00:50:54 UTC
Revision:     e76905e3a1034e6f724566aeb985621347ff43bc

Kotlin DSL:   1.0-rc-6
Kotlin:       1.2.61
Groovy:       2.4.15
Ant:          Apache Ant(TM) version 1.9.11 compiled on March 23 2018
JVM:          1.8.0_181 (Oracle Corporation 25.181-b13)
OS:           Mac OS X 10.16 x86_64

I want to use the AWS IAM plugin in my build.gradle file to get artifacts from our S3 Maven repo, so I have these

subprojects {
    apply plugin: 'maven'
    apply plugin: 'java'
    apply plugin: 'jacoco'
    apply plugin: 'findbugs'
    apply plugin: 'pmd'
    apply plugin: 'checkstyle'
    apply plugin: 'license-report'
    apply plugin: 'maven-publish'
    apply plugin: 'docker'

    repositories {
        mavenLocal()
        maven {
            url "s3://" + s3_bucket
            authentication {
                awsIm(AwsImAuthentication)
            }
        }
    }
}

// Needed by awsIm(AwsImAuthentication)
dependencies {  
    compile 'com.amazonaws:aws-java-sdk-iam:1.11.78'
    compile 'com.amazonaws:aws-java-sdk-ec2:1.11.78'
}       

But when I execute a Gradle command I get..

$ ./gradlew clean

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/user/git-repos/path_to/build.gradle' line: 280

* What went wrong:
A problem occurred evaluating root project 'rootProject'.
> Could not find method compile() for arguments [com.amazonaws:aws-java-sdk-iam:1.11.78] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

I've tried compileOnly, implementation, etc., all giving the same error. What am I missing?


Solution

  • As written, your dependencies block configures your root project (for it is at the top level in the script), whereas you apply the plugins to all subprojects and not the root project (as you do so inside the subprojects scope).

    That is crucial, since one of those plugins, java, adds the compile method to the dependencies block in each project to which it is applied. It is not applied to your root project so there is no such method there.

    I suggest therefore that you move the dependencies block inside subprojects. Then it will also configure your subprojects and will add the two new dependencies to all subprojects.