简体   繁体   中英

Gradle 'Provided' dependency for Java plugin

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.

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.

You have added a providedCompile configuration, but you aren't doing anything with it. Hence it won't make it on any class path. To put the configuration on the main compile class path, you can do:

sourceSets.main.compileClasspath += configurations.providedCompile

Likewise, to put it on the test compile class path:

sourceSets.test.compileClasspath += configurations.providedCompile

You can use compile scope inside 'jar' modules and providedCompile inside 'war' module.

War's providedCompile scope will override jar's compile scope.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM