Wa02 Gradle
Wa02 Gradle
Gradle is an open-source tool for making easy to build and automate the operation necessary when deploy
a large program. We can use it to build app for many ecosystems like program JVM base, Android, Native,
etc.
Gradle is based on a set of principle. It is a convention-based tool that makes assumption about where
source files are stored inside your project folder, where test file and resources are (simplify the work of the
programmer). It does support multi-project build, so project subdivided in several independent modules
where each module is responsible for a specific aspect (like one for frontend and one for backend) and they
can have also different languages.
It can be customizable via a script and via a plugin; classes written in Java or JVM complaint languages
which are responsible of performing some given operation. We can do that due to external modules
(plugins) that operate transformations on input file. It supports dependences management that means
often program we create are based of huge set of libraries and we need to download them and locate
them.
Also, the project must have some dependences, need some library. When you create a new project, you
have only the standard library of the Kotlin languages. Where do this library come from? It is reported
inside the repository block; now says he look on the maven Central website.
Build Gradle and setting.gradle have the .kts if you choose the Kotlin language. Build.grade the file
responsible for building the project. The setting.gradle just say at the beginning the project name; you add
subproject and list them together. The gradlew and gradlew.bat are shell script that allows us to run Gradle
from the command line guarantee to run the correct wrapper (contained in the Gradle folder that
guarantee that the project is always compile with the same version).
The folder Gradle is here because when the program will be prepared all the command for running it will be
handling to the Gradle wrapper. It will go be part of our project so that we can guarantee a unique
compilation; with the Gradle setting and version.
Src have two folders main and test. Inside main/kotlin we put our source files (divided in the language we
wrote them). All what is in test is considered test file and only use for testing.
We can find also in this structure src/main/resources which contains instead source files, some non-
executable files like images. File inside resource directory will be packet as part of the final jar and available
at runtime by the class loader but they don’t contain any source code (like, configuration file, icon, images).
When you run the project, a new folder called Build did appear.
We usually create process with one single artifact or more. In a web application is common to have 2
software artifacts (frontend and backend). The frontend consists in a set of HTML and Js classes while
backend project that consist in a bunch of classes. Gradle support single artifact and multiple artifacts.
Single module (project) contains a set of tasks to be performed. Each task is aimed at a specific operation;
like clean, build, assemble, etc.
A project in gradle is a set of tasks applied to a set of artifacts and transformed in the final product we
want. We have different plugins and each plugin do a task. Each operation needed one or more parameters
and the plugins interpreted them. We can use also custom plugins. The build.gradle.kts describe all the
tasks, it lists how things are related each other, the order in which they are executed is decided by gradle
looking the connection graph of our specification, so it understand when there is something that can be
cached and re-used.
By adding plugin to the project, we can add custom behavior providing extra steps necessary when deploy
our project.
Inside the setting Gradle we have information for all project in our folder.
Jcenter is the place where our repositories can be downloaded. We don’t describe how a task has to be
done. Most of the time we need really small amounts of information.
Typically, all the system is triggered by command line. Useful when compile things on a remote machine.
If you use the Kotlin language you can add more task references, and this let you customize the behavior of
a task providing a set of operation. We use the delegation pattern.
By using plugins, we can extend the capability of our project. The plugin block let us list what to do. Some
are maintaining by Gradle itself and other by the community.
The ‘application’ plugin makes available the run task that allow to launch the artifact.
Some dependences depend on others. You can see for example searching the library on Maven site.
We are creating an application with dependences inside the folder ./lib or in the company repository or
inside maven Central (if not find in previous location).
For saying in which phase of the build project need the library we can define the keyword before the GAV
notation (group artifact version).
In some situation we need to create multiproject, split into different module (front end and backend for
example). Each microservices is an independent module part of the general service we are creating. We
want to keep the source code separately to improve maintainability and readability.
Setting Gradle only exists at the top level, and we have a parent build grade. For each module we have a
build Gradle. At parent level set the information for all the modules.
The main setting Gradle file need do include the module part of our project.