Blog

Oracle ADF and Gradle integration – part#3: Dependency Management

Library dependency management is one of the most missing tools in Oracle JDeveloper 11g. It is true that Maven support has been added in JDeveloper 12c. Nevertheless, in the older projects you may want to use Gralde integration described in this article. This approach also works with JDeveloper 12c.

I. Overview

This tutorial is intended for both JDeveloper 11g and 12c. There is only one difference between them, which I will discuss when we get to it.

From the tutorial you will learn how to:

write custom Groovy/Java helper class and use it in Gradle script;

design custom dependency model using Gradle dependency management;

integrate Gradle dependency management with Oracle ADF application.

But before we start

If you have not read the following articles yet, I will recommend that you do so because it will be much easier to understand you the tutorial.

The Idea

Before the release of JDeveloper 12c, the standard way to add library to Oracle ADF application was by creating and attaching
*.library file into the project. The file contains entries pointed to jar files used in the project. Here is an example:

Let’s think what would happened if we could generate custom library file and attach it into project. It sounds good, doesn’t it? I will tell you more – it works as it should. Attaching a library file to the project is relatively easy so, most of the tutorial is about how to generate the right
*.library file in the right place.

In this tutorial, we will create two library files for both the Model and ViewController projects. Files will be located in
adf-gradle/build/libraries/ADFGradle_model and
adf-gradle/build/libraries/ADFGradle_vc directories. They will be generated based on previously downloaded jar files. Jars will be located in the same folders as library files and will be downloaded by newly created Gradle task. The task will use an utility class written in groovy. This is what the idea from the end looks like.

Sample Application

For the purposes of the tutorial I prepared an exemplary application which you can download from here. The application is compatible with JDeveloper 11.1.1.7 but it is also possible to open it in JDeveloper 12c. It has already been integrated with Gradle as I explained it in this tutorial.

ADFGradle-dependency-management-base.zip file content

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

.

├── adf-gradle

│ └── build.gradle

├── GradleDependencyManagement.jws

├── Model

├── src

└── ViewController

├── public_html

├── src

│ └── com

│ └── bettercoding

│ └── gradle

│ └── view

│ └── bean

│ └── SampleBean.java

└── ViewController.jpr

As you probably noticed, the ViewController project contains
SampleBean.java class. The class imports external packages like
com.fasterxml.jackson.core.type.TypeReference and it depends on
jackson-databind library (which also depends on two other libraries:
jackson-core and
jackson-annotations). The project does not compile because of missing dependencies. Your goal is to make is to make it work.

II. Gradle Task: downloadDependencies

In this part of the tutorial you will see how to write a Gradle script which allows you to:

configure dependencies for both Model and ViewController projects;

download all previously configured libraries (with theirs dependencies) into the right directories;

create ADF Library files for both Model and ViewController projects;

Utility class: AdfGradleUtils.groovy

Although there is a possibility to write whole code directly in
build.gradle file I decided to put it into separated groovy class. We can advantage the fact that every class placed in the
adf-gradle/buildSrc/src/main/groovy directory is automatically compiled and ready to use in
build.gradle script. By the way, it is also possible to write Java class using the following location
adf-gradle/buildSrc/src/main/java.

At this moment we will prepare a mocked utility class
adf-gradle/buildSrc/src/main/groovy/AdfGradleUtils.groovy with the following content:

The class contains two methods. The first one is responsible for downloading all dependencies defined in
configuration parameter. The second method is responsible for creating ADF library file based on previously downloaded libraries. The methods are not finished yet. We will finish it later.

Integration: build.gradle

Heaving a separate utility class makes the integration process much more clearer. At first we have to define repositories and both configurations for our application(for Model and ViewController projects). In this case we will use Central Maven Repository as default source of artifacts.

build.gradle

1

2

3

4

5

6

7

8

9

10

11

12

13

group'com.bettercoding'

version'1.0-SNAPSHOT'

repositories{

mavenCentral()

}

configurations{

ADFGradle_model

ADFGradle_vc

}

...

Then we have to define project properties which helps us to stick to canons:

Let’s think for a moment what
processDependencies method actually do. It prepares the right directory structure and copies all jars (with dependencies) into the directory. At The end the method calls
generateAdfLibraryFile.

The
generateAdfLibraryFile method is responsible for generating ADF Library file with entries pointed to previously downloaded jars. The idea of the method is very simple – just list all jar files from the directory and add it into library. The body of the method may looks like below.

III. Configure ADF project

The last thing we have to do is to attach generated ADF library files into ADF Application. Run JDeveloper and open
Project Properties menu. Then go to
Libraries and Classpath , click
Add Library , click
Load and choose the directory with generated
ADFGradle_vc library. Then select the library.

Repeat above for Model project and try to rebuild the project. You should got a compilation error, because we have not defined any library yet.

To add the required libraries put the following lines to
build.gradle file: