64

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).

Gradle seems to have gotten this the wrong way around since 'runtime' inherits from 'compile'. I can't imagine a situation where I would want classes at runtime that I wouldn't want at compile time. However, there are many circumstances where I need classes to generate code at compile time that I do not wish to deploy at runtime!

I've ploughed through the bloated gradle documentation but cannot find any clear instructions or examples. I suspect this might be achieved by defining a 'configuration' and setting it as the classpath of the CompileJava plugin - but the documentation falls short on explaining how to achieve this.

1
  • 2
    i think the current up-to-date correct answer is that of xsveda using compileOnly configuration.
    – Robert
    Commented Sep 18, 2016 at 13:40

11 Answers 11

57

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.

8
  • 3
    Has anyone gotten this to work with Android Studio and gradle 1.9? Commented Dec 29, 2013 at 16:18
  • you can find the provided scope definition on several sites. but the ide plugin part helped me a lot!
    – vvursT
    Commented Jun 18, 2014 at 6:47
  • I'm pretty new to Gradle and I'm using it in my Android Studio project. When I try to add the above (everything about the idea {...} clause) I get an error saying "Could not find property main on SourceSet container". Is there anything else I need to do?
    – b.lyte
    Commented Jul 2, 2014 at 20:23
  • 1
    For Gradle 2.0 you have to write: idea { module{ scopes.PROVIDED.plus += [configurations.provided] } } See fo rmore details forums.gradle.org/gradle/topics/…
    – FeelGood
    Commented Nov 8, 2014 at 14:41
  • 2
    Gradle3: sourceSets.main.compileClasspath += configurations.provided Commented Aug 23, 2016 at 1:48
12

If you use the war plugin, providedCompile should do the trick. However if you need to exclude dependencies from being included in a jar, you'll have to extend the jar task. Below is an example for building a "fat jar" or "uber jar" (a single jar that includes all classes of its dependencies) excluding depndencies marked provided:

configurations {
    provided
    compile.extendsFrom provided
}

dependencies {
    provided "group.artifact:version"
    compile "group.artifact:version"
}

jar {
    dependsOn configurations.runtime
    from {
        (configurations.runtime - configurations.provided).collect {
            it.isDirectory() ? it : zipTree(it)
        }
    } 
}

Credit: http://kennethjorgensen.com/blog/2014/fat-jars-with-excluded-dependencies-in-gradle/

Update:

As of Gradle 2.12 the issue of defining compile only dependencies is finally solved in a simple and natural manner by the new "copmpileOnly" configuration:

dependencies {
    compileOnly 'javax.servlet:servlet-api:2.5'
}
1
  • 4
    This is by far the best general / non-android answer. Very helpful for hadoop & storm libraries where dependencies are needed at compile time but need to be excluded from the jar.
    – Nathan
    Commented Mar 19, 2015 at 18:52
9

I figured it out for my project setup. I use Android Studio running with the gradle plugin 0.9.+ with gradle 1.11 The main project uses amazon ads and amazon inapp purchases. It depends on a library project using amazon device messaging(ADM).

My main issue was with the the ADM where I got the "RuntimeException: Stub!" error.

1.) Library Project: The "provided configuration" proposed by Lukas does not work, as stated by him, so I used Richards approach, which however did not work as well out of the box. I had to change it a little since I could not find the lib in the ext_libs folder of the aar file. Gradle seems to pack all libraries in the libs folder in the in the final aar-file.

android.libraryVariants.all { variant ->
variant.packageLibrary.exclude( 'libs/amazon-device-messaging-1.0.1.jar' )
}

2.) Application Project : Here, the approach with the "provided configuration" worked.

configurations{
    provided
} 
dependencies {
    compile 'fr.avianey:facebook-android-api:+@aar'
    compile files('ext_libs/amazon-ads-5.3.22.jar')
    compile files('ext_libs/in-app-purchasing-1.0.3.jar' )
    provided files('ext_libs/amazon-device-messaging-1.0.1.jar')
}

android.applicationVariants.all {
    variant -> variant.javaCompile.classpath += configurations.provided
}
1
  • I can confirm that the solution to exlude the library works for apply plugin: 'com.android.library', the trick is to exlude it from 'libs' even if you have the lib in whatever other folder, as written above gradle put it all into libs folder. Thanks a lot Mike. Commented Jul 14, 2014 at 8:54
6

It's quite common to have runtime dependencies that aren't compile time dependencies. The other way around is a fairly special case and as such requires a few lines of configuration in Gradle. I suggest to search the Gradle forum for provided.

It sounds like what you are really after is declaring dependencies for your build, not for the compile class path. How this is done depends on how the desired functionality gets invoked (Ant task, Gradle task/plugin, ad-hoc use from build script). If you provide more detailed information on what you are trying to do, I can provide a more specific answer.

Here are some links to relevant information in the Gradle user guide:

6

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"
}
6

In Gradle 2.12 a compileOnly configuration has been introduced. A blog post introducing this features can be found here:

Gradle latest feature: Compile only dependencies

Please be aware of one important side effect:

As a result of the addition of the “compileOnly” configuration, the “compile” configuration no longer represents a complete picture of all compile time dependencies. When needing reference a compile classpath in build scripts or custom plugins, the appropriate source set’s compileClasspath property should be used instead.

3

It turns out that they have added a "provided" configuration in the gradle android plugin 0.8.0 but it doesn't quite work. It does add the provided libraries to the compile path automatically, but it also includes them in the final aar/apk.

What worked for me was the solution provided by @lukas-hanaceck but by changing the name from "provided" to any other custom name. In my case this is a library project which is a dependency for my final android application project. Heres a gist of what worked for me.

configurations {
   providedlibs
}

dependencies {
   providedlibs files('provided/library.jar')
}

libraryVariants.all {
    variant -> variant.javaCompile.classpath += configurations.providedlibs
}

It compiles perfectly and the provided/library.jar is not included in the final apk. The only issue I am having is notifying Android studio of the existence of library.jar. The idea plugin doesn't seem to work for Android studio. Im guessing that they have another custom plugin for syncing gradle with studio.

1
  • I confirm the stock provided Android Gradle 1.0.1 configuration does properly appear in the compile classpath but then forces itself into the APK, which defeats the purpose. Many of these answers are quite similar. I used this one as a template and it worked ok for me. It works well for jar (remote and local) dependencies but does not work for AARs. I ended up exploding the classes.jar out of the AAR as a workaround. Commented Jan 19, 2015 at 23:31
2

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

2

In Android Studio 1.0 do this:

android.libraryVariants.all { variant ->
    variant.outputs.each { output ->
        output.packageLibrary.exclude('libs/someLib.jar')
    }
}
0
0

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!

0
0

The OP apparently didn't look for an Android answer, but some answers are specific to Android. So I suggest you look at this page : http://tools.android.com/tech-docs/new-build-system

Version 0.9.0 introduced a provided scope. So, just use

dependencies {
    provided "groupId:artifcatId:version"
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.