Provided VS CompileOnly

When I use provided like this:
provided group: ‘javax.servlet’, name: ‘javax.servlet-api’, version: ‘3.1.0’
the result was:
BUILD FAILED in 0s
Could not find method provided() for arguments [{group=javax.servlet, name=javax.servlet-api, version=3.1.0}] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
But use compileOnly is ok, why?

Hi,

provided is not a valid configuration name when using java or war plugin. See the plugin documentation page to have an overview of the configurations that are available to your project and their effect: configurations from java plugin and configurations from war plugin

To obtain the equivalent of the Maven provided scope, you can then either:

  1. define your dependency in configurations compileOnly and testImplementation (requires Gradle 3.5+ and java plugin)
  2. define your dependency in providedCompile (requires war plugin)
  3. apply the nebula.provided-base plugin only if using Gradle 3.4 or lower, and use the provided configuration.