Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle 'Provided' dependency for Java plugin

Tags:

java

gradle

I am attempting to compile several WAR files, all that depend on a common JAR module. In my Gradle build however, I cannot seem to get a 'Provided' like dependency to work with the Java plugin.

My compile looks like this:

apply plugin: 'java'
configurations{
    providedCompile
}

dependencies {
    compile module("org.springframework.amqp:spring-amqp:${springAmqpVersion}")
    compile module("org.slf4j:slf4j-api:${slf4jVersion}")
    compile module("org.slf4j:slf4j-ext:${slf4jVersion}")

    providedCompile "javax.servlet:servlet-api:${servletApiVersion}"

    runtime module("org.slf4j:jcl-over-slf4j:${slf4jVersion}")
    runtime module("org.slf4j:jul-to-slf4j:${slf4jVersion}")
    runtime module("org.slf4j:log4j-over-slf4j:${slf4jVersion}")

    sourceArchives module("org.springframework.amqp:spring-amqp:${springAmqpVersion}:sources")
    sourceArchives module("javax.servlet:servlet-api:${servletApiVersion}:sources")
}


sourceSets {
    main { compileClasspath += configurations.providedCompile }
}

However, that last bit is where it gets an exception. I have tried adding the servlet-api (Provided by Tomcat) to the configuration after the runtime dependencies would extend it, or simply putting it in as a compile module, then removing it from runtime dependencies later.

I've attempted several different ways of modifying the dependencies, with my closest results being:

newRuntime = configurations.runtime.minus(configurations.providedCompile)
configurations.runtime = newRuntime

This last bit however, will generate the variable newRuntime with the proper dependencies, however when I tried to reassign the variable back to the runtime configuration, it throws a "Cannot find property exception"

I found a lot of discussion of this exact problem on Gradle's bug tracking:Gradle-784

However the main lead from that is from Spring who uses Maven with their gradle builds, which I am unfamiliar with.

The most promising link I found here on SO, but unfortunately I could not get the examples to work as well: SO Provided Question Of note for the Stack Overflow question the line that showed most promise:

//Include provided for compilation
sourceSets.main.compileClasspath += configurations.provided

This line does not give an error like other attempts, however it appears that the providedCompile (My version of provided) dependency is not actually put on the compile classpath, as there is a classpath error when compilation is attempted.

like image 555
Chris Lefevre Avatar asked Sep 06 '12 13:09

Chris Lefevre


People also ask

What is provided dependency in Gradle?

Generally, all the dependencies needed in the production environment are known as runtime dependency. In Gradle official documentation, it says that runtime dependency are "the dependencies required by the production classes at runtime. By default, also includes the compile time dependencies.".

What is plugin and dependency in Gradle?

A plugin is how Gradle knows what tasks to use. There are many plugins. For more info, see Gradle - Plugin Documentation. A dependency is a library that is compiled with your code. The following line makes your module depend on the Android AppCompat V7 library.

What does the Gradle Java plugin do?

The Java plugin adds Java compilation along with testing and bundling capabilities to a project. It serves as the basis for many of the other JVM language Gradle plugins.

How do I add a dependency in Gradle?

To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your module's build.gradle file.


1 Answers

I'm not 100% following your message but providedCompile is only allowed for 'war' plugin.

apply plugin: 'war'

dependencies {
  // others go here
  providedCompile "javax.servlet:javax.servlet-api:${servletVersion}"
}

During 'war' step the servlet jar is not included.

like image 136
Natan Cox Avatar answered Nov 04 '22 19:11

Natan Cox