Gradle:找不到providedCompile()方法

33

这个构建文件是如何使用providedCompile的,这个那个文件都可以。

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 providedCompile() 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: 12.721 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'
    providedCompile 'javax:javaee-api:7.0'
}

关于:

如何使用Gradle解决javaee-api的依赖以建立EAR文件?

2个回答

78

providedCompile 是随 war 插件一起提供的。因此,您需要添加:

plugins {
  // ...
  id 'war'
}

在我的情况下,我已经应用了war文件,但仍然出现相同的错误。 - Saransh Sharma
这段代码应该添加在哪里? - Pablo Pazos
1
@PabloPazos,在你的构建脚本的最开始。 - Opal

1
如果您安装了较高版本的Gradle,就可能会出现此错误。例如,您安装了:
gradle --version

给予
Gradle 5.0

但是该项目要求使用Gradle 2.10或其他低版本的Gradle。 为解决此问题,首先请尝试使用Gradle包装器./gradlew(如果包含在项目中),而不是本地安装的gradle。或者安装正确的版本的gradle并使用路径变量使其可用。

你所说的“项目”,是指我将要工作的具体项目吗? - Thufir
是的。假设该项目是由其他人创建的。 - Master Drools

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接