gradle dependencies

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

We have seen how we can create a java project in gradle, but there were no dependencies in it. Real world projects are not that simple. They have dependencies. Gradle dependencies help us define dependencies for java projects. They can be compile time and runtime dependencies. This article will deal with both type of gradle dependencies.

Gradle Dependencies:

We will directly go for an example to understand gradle dependencies. Consider we need to create a class called DependencyTest, which uses a class called RequiredClassAtCompileTime. As name suggests, it is required for successful compilation. Now There is also case that libraries which are needed for successful compilation also need some libraries which are needed at runtime, We have a class called RequiredClassAtRuntime which is required by RequiredClassAtCompileTime.

At first we will need to create jars for RequiredClassAtRuntime and RequiredClassAtCompileTime both of which are required for DependencyTest

RequiredClassAtRunTime:

Write a class as below:

build.gradle

This will have a very simple build.gradle file.

This will get built successfully and generate RequiredAtRuntime.jar

RequiredAtCompileTime:

We have a class called RequiredAtCompileTime as below.

Note that this uses RequiredAtRuntime, though the source code uses it, gradle needs the information about it while building RequiredAtCompileTime. We can specify gradle dependencies in gradle file as below.

There are a few things to notice here

  • repositories specifies the repository on local file system. It means we have put all required jar files under libs directory under root directory of RequiredAtCompileTime project. For this, you have to put generated RequiredAtRuntime.jar file under RequieredAtCompileTime/libs
  • compile option means that we are providing gradle dependencies which are required at compile time.
  • fileTree options specifies the directory and the files to include as compile time gradle dependencies

Run gradle build command for project RequiredAtCompileTime. It should build successfully. Now we have the dependencies ready. Proceed to building the DependencyTest project.

DependencyTest:

Write class as below

Note that it needs RequiredClassAtCompileTime for compilation which in turn needs RequiredClassAtRuntime from RequiredAtCompileTime.jar and RequiredAtRuntime.jar.

build.gradle

This is quite similar to RequiredAtCompileTime project. You need to have dependencies in libs directory

Run command gradle build for DependencyTest. This should build successfully.

The difference between compile and runtime gradle dependencies is that compile time are needed for source code compilation whereas runtime dependencies are needed by compile time dependencies. It is called as transitive dependency.

Consider example of hibernate jars which are needed at compile time if we write a java application that uses hibernate. But hibernate also needs logging jars at runtime. So they need to be defined as runtime

I hope this article helped understand compile time gradle dependencies.

 

 

 

.

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Leave a Reply

Your email address will not be published. Required fields are marked *