Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Provided' dependency in Gradle

I have build.gradle in front of me and there are some dependencies declared as provided but in documentation I do not see this dependency scope.

dependencies {     compile("org.springframework.boot:spring-boot-starter-web:1.2.4.RELEASE")     ....      provided 'backport-util-concurrent:backport-util-concurrent:3.1'     provided 'org.javolution:javolution:5.5.1@jar     .... } 

Is this provided by a plugin? If so how do I found out which plugin this belongs to?

What is the difference between provided and runtime dependency scope in Gradle?

like image 453
ps-aux Avatar asked Jun 09 '15 11:06

ps-aux


People also ask

What is provided dependency in Gradle?

In the Maven world, the "provided" scope means a library that needs to be available in order to compile the project but which should not be exported as a transitive dependency or included in the final packaged archive (e.g. WAR file) because it will be "provided" in some other way by the target environment.

Where are the dependencies in Gradle?

Gradle declares dependencies on JAR files inside your project's module_name /libs/ directory (because Gradle reads paths relative to the build.gradle file).

What is compile time dependency in Gradle?

compileOnly dependencies are available while compiling but not when running them. This is equivalent to the provided scope in maven. It means that everyone who wants to execute it needs to supply a library with all classes of the CompileOnly library.

What is difference between compile and compileOnly Gradle?

The compileOnly configuration is used to itemize a dependency that you need to compile your code, same as compile above. The difference is that packages your java code use from a compileOnly dependency will not be listed as Import-Package manifest entries.


1 Answers

What is provided scope?

Suppose that a jar is needed to compile your code, but the jar is present in the production environment library collection. Then you don't need to package the jar with your project archives. To support this requirement, Maven has a scope named provided. If you declare any jar dependency as provided, then this jar will be present in your classpath during compilation but will not be packaged with your project archive.

provided scope is very useful, particularly in web applications. For example, servlet-api.jar is needed to be present in your classpath to compile your project, but you don't need this to package servlet-api.jar file with your war. With provided scope one can achieve this requirement.

There is no Scope defined in Gradle java plugin named provided. Also not in war or android plugins. If you want to use provided scope in your project, then you have to define it in your build.gradle file. Following is the code snippet to declare provided scope in gradle:

configurations {     provided }  sourceSets {     main { compileClasspath += configurations.provided } } 

Now, your second question:

What is the difference between provided and runtime dependency scope in Gradle?

To answer this question first I will define compile dependency. compile dependencies are dependencies, those are necessary to compile your code. Now imagine that if your code uses a library named X then you must declare X as your compile-time dependency. Also imagine that X uses another library Y internally, and you declared Y as your runtime dependency.

During compilation, Gradle will add X into your classpath but will not add Y. Since, Y is not required for compilation. But it will package both X and Y with your project archive since both X and Y are necessary to run your project archive in the production environment. Generally, all the dependencies needed in the production environment are known as runtime dependency.

In Gradle official documentation, it says that runtime dependency are "the dependencies required by the production classes at runtime. By default, also includes the compile time dependencies.".

Now, if you've read this far, then you already know that provided is a compile dependency that we don't want to be present in the runtime dependency (basically, we don't want it to package with the project archive).

Following is an illustration of provided and runtime scope. Here, compile refers to the dependencies that are required to compile the project and non-compile refers to the dependencies that are not required for project compilation.

like image 172
Sazzadur Rahaman Avatar answered Sep 24 '22 03:09

Sazzadur Rahaman