How to use provided scope for jar file in Gradle build?

In the 2.12 release of Gradle, compileOnly was added to give similar functionality to provided scope. There is a difference in what happens in the test classpath. Here is relevant quote and snippet from the release notes:

You can now declare dependencies to be used only at compile time in conjunction with the Java plugin. Compile only dependencies are used only during source compilation and are not included in the runtime classpath or exposed to dependent projects. This behavior is similar to that of the ‘provided’ scope available in Maven based builds. However, unlike Maven provided dependencies, compile only dependencies in Gradle are not included on the test classpath.

Compile only dependencies should be assigned to the relevant source set’s ‘compileOnly’ configuration.

dependencies {
    compileOnly 'javax.servlet:servlet-api:2.5'
}

Leave a Comment