ProvidedCompile and Compile Dependencies with Gradle - No Fluff Just Stuff

ProvidedCompile and Compile Dependencies with Gradle

Posted by: Tobias Gesellchen on January 2, 2014

When using the Gradle war plugin you're enabled to declare providedCompile dependencies to tell the compiler to include those dependencies in the compile classpath, but to not make Gradle include them in the packaged .war artifact. Your build.gradle script might look like this, e.g. for a webapp using GWT:

apply plugin: 'war'

dependencies {  
  ...
  providedCompile 'com.google.gwt:gwt-user:2.5.1'
  ...
}

The dependency shown in the example has some own dependencies, which can be shown by using the :dependencies task. You should get a dependency tree like the snippet below:

...
providedCompile - Additional compile classpath for libraries that should not be part of the WAR archive.  
\--- com.google.gwt:gwt-user:2.5.1
     +--- javax.validation:validation-api:1.0.0.GA
     \--- org.json:json:20090211
...

You might now want to have some Validation in your application, so you'd like to use the validation-api as already provided through the transitive dependency of gwt-user. Since Gradle won't package provided dependencies, you might declare the validation-api with a compile scope, so that your build script looks like follows:

apply plugin: 'war'

dependencies {  
  ...
  compile 'javax.validation:validation-api:1.0.0.GA'
  providedCompile 'com.google.gwt:gwt-user:2.5.1'
  ...
}

Well, when now calling the Gradle build task, and deploying the created .war artifact you'll notice that the validation-api hasn't been packaged. An explanation can be found at a Gradle forum thread telling you to disable transitive dependencies for cases like shown above. The solution for the example above would look like this:

apply plugin: 'war'

dependencies {  
  ...
  compile 'javax.validation:validation-api:1.0.0.GA'
  providedCompile ('com.google.gwt:gwt-user:2.5.1') {
    transitive = false
  }
  ...
}

Please read the linked forum thread completely for details!

Tobias Gesellchen

About Tobias Gesellchen

Tobias Gesellchen is a software engineer with 10 years of application development and focus on Java and JavaScript. Tobias worked at Hypoport on a new product for a German finance integration platform, including several tools like AngularJS as frontend technology, a backend implemented in Groovy and a deployment pipeline using Docker. He now works at Small Improvements on a tool for great companies to improve their reviews, feedback, and culture. In his free time Tobias hacks on a number of open source projects.

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »