Can someone please give me a simple build.gradle example of how I can specify compile-time-only classes that are not included in the runtime deployment (war).
Gradl
There has been a lot of discussion regarding this topic, mainly here, but not clear conclusion.
You are on the right track: currently the best solution is to declare your own provided
configuration, that will included compile-only dependencies and add to to your compile classpath:
configurations{
provided
}
dependencies{
//Add libraries like lombok, findbugs etc
provided '...'
}
//Include provided for compilation
sourceSets.main.compileClasspath += [configurations.provided]
// optional: if using 'idea' plugin
idea {
module{
scopes.PROVIDED.plus += [configurations.provided]
}
}
// optional: if using 'eclipse' plugin
eclipse {
classpath {
plusConfigurations += [configurations.provided]
}
}
Typically this works well.
I didnt find a solution for Android Studio, but this what I tried:
In android studio I had to update to version 0.5.+
in gradle/gradle-wrapper.properties replace
distributionUrl=http\://services.gradle.org/distributions/gradle-1.9-rc-3-bin.zip
by
distributionUrl=http\://services.gradle.org/distributions/gradle-1.11-all.zip
in all my build.gradle replace
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
by
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
}
and in the library I wanted to use provided
configurations {
provided
}
//put applicationVariants in case it is apply plugin: 'android' and not apply plugin: 'android-library'
android.libraryVariants.all {
variant -> variant.javaCompile.classpath += configurations.provided
}
dependencies {
provided files('ext_libs/amazon-device-messaging-1.0.1.jar')
}
and at the end it doesnt work, it seems that it works for jar but not for aar or apk as stated here https://groups.google.com/forum/#!topic/adt-dev/WIjtHjgoGwA
In Android Studio 1.0 do this:
android.libraryVariants.all { variant ->
variant.outputs.each { output ->
output.packageLibrary.exclude('libs/someLib.jar')
}
}
We don't need "provided", try to add this:
android.libraryVariants.all { variant ->
variant.packageLibrary.exclude( 'ext_libs/amazon-device-messaging-1.0.1.jar' )
}
Enjoy!
If you use the WAR plugin, you can use providedCompile
as in this example
dependencies {
compile module(":compile:1.0") {
dependency ":compile-transitive-1.0@jar"
dependency ":providedCompile-transitive:1.0@jar"
}
providedCompile "javax.servlet:servlet-api:2.5"
providedCompile module(":providedCompile:1.0") {
dependency ":providedCompile-transitive:1.0@jar"
}
runtime ":runtime:1.0"
providedRuntime ":providedRuntime:1.0@jar"
testCompile "junit:junit:4.11"
moreLibs ":otherLib:1.0"
}