How to determine compilation-time path * only * class in Gradle?

Can someone please give me a simple build.gradle example of how I can specify compile-time classes that are not included in runtime deployment (war).

Gradle seems to be wrong, since "runtime" inherits from "compilation". I cannot imagine a situation where I would like classes at run time not to want at compile time. However, there are many circumstances when I need classes to generate code at compile time that I don't want to deploy at runtime!

I have plowed up bloated gradle documentation but can't find any clear instructions or examples. I suspect that this can be achieved by defining the “configuration” and setting it as the path to the classes of the CompileJava plugin, but the documentation is not suitable for explaining how to achieve this.

+62
classpath compilation gradle
May 01 '12 at 23:56
source share
11 answers

There was a lot of discussion on this topic, mainly here , but not a clear conclusion.

You are on the right track: currently, the best solution is to declare your own provided configuration, which will include only compilation dependencies and add to your compilation path:

 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] } } 

This usually works well.

+55
May 2 '12 at
source share

If you are using a military plugin, providedCompile should do the trick. However, if you need to eliminate dependencies from the jar number, you will have to extend the jar task. The following is an example of building a “thick jar” or “uber jar” (the only jar that includes all its dependency classes), except for the provided marks:

 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 problem of defining compilation of only dependencies is finally solved in a simple and natural way with the new configuration "copmpileOnly":

 dependencies { compileOnly 'javax.servlet:servlet-api:2.5' } 
+11
Sep 12 '14 at 13:54 on
source share

I figured this out for my project setup. I am using Android Studio with gradle 0.9 plugin. + With gradle 1.11. The main project uses amazon ads and amazon inapp purchases. It depends on the library design using Amazon Device Messaging (ADM).

My main problem was in ADM, where I got "RuntimeException: Stub!". mistake.

1.) Library project: “The provided configuration” proposed by Lucas does not work as he told him, so I used Richards' approach, which, however, did not work out of the box either. I had to modify it a bit, since I could not find the lib in the ext_libs folder of the aar file. gradle seems to pack all the libraries in the libs folder in the final aar file.

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

2.) Application design: The "provided configuration" approach works here.

 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 } 
+9
Jun 11 '14 at 8:07
source share

It is generally accepted to have run-time dependencies that are independent of compilation time. Another way is a rather special case, and this requires several configuration lines in Gradle. I suggest finding a Gradle forum for provided .

It seems like you really after that declare the dependencies for your assembly, and not for the compilation class path. How this is done depends on how the required functionality is called (Ant task, Gradle task / plugin, ad-hoc use from build script). If you provide more details about 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
May 2 '12 at 16:53
source share

If you use the WAR plugin, you can use the 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
Sep 09 '13 at 13:36 on
source share

In Gradle 2.12, the compileOnly configuration was introduced. A blog post that features these features can be found here:

Gradle last function: compile dependencies only

Pay attention to one important side effect:

As a result of adding the “compileOnly” configuration, the “compilation” configuration no longer provides a complete picture of all compile-time dependencies. If necessary, refer to the compilation path of the classpath in build scripts or custom plug-ins, use the compileClasspath property corresponding to the source code instead.

+5
Mar 30 '16 at 8:28
source share

Turns out they added the “provided” configuration to the gradle android 0.8.0 plugin, but this doesn’t quite work. It automatically adds the provided libraries to the compilation path, but 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 user name. In my case, this is a library project that is dependent on my final Android application project. This is the essence of what worked for me.

 configurations { providedlibs } dependencies { providedlibs files('provided/library.jar') } libraryVariants.all { variant -> variant.javaCompile.classpath += configurations.providedlibs } 

It compiles fine, and the provided /library.jar is not included in the final apk. The only problem I came across was to point out Android studio about the existence of library.jar. The idea plugin does not seem to work for Android studio. I assume they have another custom plugin to synchronize gradle with the studio.

+3
Jul 10 '14 at
source share

I did not find a solution for Android Studio, but this is what I tried:

In android studio I had to upgrade 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 that 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 in the end it does not work, it seems that it works for jar, but not for aar or apk, as indicated here https://groups.google.com/forum/#!topic/adt-dev/WIjtHjgoGwA

+2
Mar 12 '14 at 9:08
source share

In Android Studio 1.0, do the following:

 android.libraryVariants.all { variant -> variant.outputs.each { output -> output.packageLibrary.exclude('libs/someLib.jar') } } 
+2
Dec 09 '14 at 7:22
source share

We do not need to be "provided", try adding this:

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

Enjoy it!

0
Apr 24 '14 at 22:31
source share

The OP was clearly not looking for an answer on Android, but some answers are specific to Android. Therefore, I suggest you look at this page: http://tools.android.com/tech-docs/new-build-system

Version 0.9.0 introduced the provided area. So just use

 dependencies { provided "groupId:artifcatId:version" } 
0
Jul 03 '14 at 11:33
source share



All Articles