Gradle: 找不到提供的方法()

Gradle: Could not find method provided()

哪个是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'
}

参考:

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

我不清楚,您的实际问题是什么:构建失败的原因或声明依赖项的首选方式是什么?

构建失败:据我所知,默认情况下没有 provided 依赖范围,但您可以将其声明为 compileOnly ,这与 provided 相同,并且自 2.12 版本起受 Gradle 支持。或者你必须使用一些额外的插件,比如 spring 引导插件。

或者您必须创建自定义配置以支持 provided 依赖项。

声明依赖关系的首选方式:providedprovided group 完全相同,使用哪个主要基于意见。 IMO,这就是原因,您找不到有关它的一些建议。在这两种情况下,您都声明了相同的依赖属性,例如 groupnameversion,唯一的区别是,在一种情况下您更明确地声明了它。

provided 配置由 war 插件(您未应用)添加,因此在您的场景中不可用

我建议您使用 compileOnly 配置,可从 gradle 2.12+