Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use provided scope for jar file in Gradle build?

Tags:

scope

gradle

I need to use Amazon Maps and Amazon Messaging in my apps.

With gradle, I did not succeed in adding the Amazon dependencies with a "provided" scope as they need to be :

The JAR file contains stub implementations of the Amazon Maps API. It does not contain actual implementations of the Maps API classes, so you should not compile the JAR into your app.

None of the solutions provided by Amazon support worked for me.

If someone succeeded to use amazon maps or amazon messaging with Gradle, please share your build.gradle file here.

like image 572
Benjamin Avatar asked Sep 11 '13 10:09

Benjamin


2 Answers

In the 2.12 release of Gradle, compileOnly was added to give similar functionality to provided scope. There is a difference in what happens in the test classpath. Here is relevant quote and snippet from the release notes:

You can now declare dependencies to be used only at compile time in conjunction with the Java plugin. Compile only dependencies are used only during source compilation and are not included in the runtime classpath or exposed to dependent projects. This behavior is similar to that of the 'provided' scope available in Maven based builds. However, unlike Maven provided dependencies, compile only dependencies in Gradle are not included on the test classpath.

Compile only dependencies should be assigned to the relevant source set's 'compileOnly' configuration.

dependencies {     compileOnly 'javax.servlet:servlet-api:2.5' } 
like image 106
mkobit Avatar answered Sep 18 '22 15:09

mkobit


The solution that I've been using is pretty simple. You must add the following code to your build.gradle file:

apply plugin: 'eclipse'  // Eclipse users only  configurations {     provided }  sourceSets {     main.compileClasspath += configurations.provided     test.compileClasspath += configurations.provided     test.runtimeClasspath += configurations.provided }  eclipse.classpath.plusConfigurations += configurations.provided  // Eclipse users only 

If you are not an Eclipse user (I'm not), you don't actually need the first and last lines, as you might have guessed.

Once the above configuration additions are in, you can then simply add a provided dependency in your dependencies section alongside of any regular compile dependencies:

dependencies {     compile group: 'org.springframework', name: 'spring-core', version: '3.2.6.RELEASE'      provided group: 'javax.servlet', name: 'servlet-api', version:'2.5'     provided group: 'javax.servlet.jsp', name: 'jsp-api', version:'2.1' } 

Hope that helps. It's been working quite well for me for some time.

like image 41
Michael Oryl Avatar answered Sep 18 '22 15:09

Michael Oryl