-->

Gradle: Could not find method provided()

2020-06-18 04:17发布

问题:

Which is preferred, provided or provided group and where is this documented?

thufir@doge:~/NetBeansProjects/gradleEAR$ 
thufir@doge:~/NetBeansProjects/gradleEAR$ gradle clean

FAILURE: Build failed with an exception.

* Where:
Build file '/home/thufir/NetBeansProjects/gradleEAR/build.gradle' line: 40

* What went wrong:
A problem occurred evaluating root project 'gradleEAR'.
> Could not find method provided() for arguments [javax:javaee-api:7.0] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

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

BUILD FAILED

Total time: 2.991 secs
thufir@doge:~/NetBeansProjects/gradleEAR$ 



plugins {
    id 'com.gradle.build-scan' version '1.8' 
    id 'java'
    id 'application'
    id 'ear'
}

mainClassName = 'net.bounceme.doge.json.Main'

buildScan {
    licenseAgreementUrl = 'https://gradle.com/terms-of-service'
    licenseAgree = 'yes'
}

repositories {
    jcenter()
}

jar {
    manifest {
        attributes 'Main-Class': 'net.bounceme.doge.json.Main'
    }
}

task fatJar(type: Jar) {
    baseName = project.name + '-all'
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
    manifest {
        attributes 'Implementation-Title': 'Gradle Quickstart', 'Implementation-Version': '3.4.0'
        attributes 'Main-Class': 'net.bounceme.doge.json.Main'
    }
}

dependencies {
    compile group: 'javax.json', name: 'javax.json-api', version: '1.1'
    compile group: 'org.glassfish', name: 'javax.json', version: '1.1'
    provided 'javax:javaee-api:7.0'
}

In reference to:

How does Gradle resolve the javaee-api dependency to build an EAR?

回答1:

The provided configuration is added by the war plugin (which you are not applying) so it's not available in your scenario

I suggest you use the compileOnly configuration which is available from gradle 2.12+



回答2:

It's not clear to me, what is your actual question: reasons of the build failure or what is preferred way to declare dependencies?

Build failure: as far as I know, there are no provided scope for dependencies by default, but you can declare it as compileOnly which is the same as provided and supported by Gradle since 2.12 version. Or you have to use some additional plugins, like a spring boot plugin.

Or you have to create a custom configuration to support provided dependencies.

Preferred way to declare dependencies: provided and provided group are just the same and which to use is mostly opinion based. IMO, that is the reason, you can't find some recommendations about it. In both cases you declare the same dependency properties, such as group, name and version, the only difference, that in one case you do it more explicitly.