[go: up one dir, main page]

0% found this document useful (0 votes)
6 views9 pages

Wa02 Gradle

Corso di Web Application II del prof G.Malnati - Politecnico di Torino, 2021

Uploaded by

giagio.vit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

Wa02 Gradle

Corso di Web Application II del prof G.Malnati - Politecnico di Torino, 2021

Uploaded by

giagio.vit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Maven is based on XML, so it is more verbose, Gradle is simpler.

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.

02: Gradle Pag. 1 a 9


The build process is described in one or more script files. New project  Gradle  Kotlin/JVM. The
build.gradle file is the main description of my project. It lists most things regarding my project. Says that is
going to require some plugin to carry out the proper compilation of this. Also describe the group name of
the project and the Gradle version. We need it empty at the beginning and we do the command gradle init.

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.

A typical gradle project is composed like the image.

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.

The build file build.gradle contains a description of the


build process saying which plugins do we need in order
to support the task for our compilation, list the
repository that is the set of websites from where
library can be downloaded, list the dependences that is

02: Gradle Pag. 2 a 9


the list of libraries and their version which are needed in order to create our project. And then can be any
further information about the project configuration.

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.

02: Gradle Pag. 3 a 9


./gradlew clean or in Windows gradlew clean

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.

We have to pass the default value inside these 3 methods.

02: Gradle Pag. 4 a 9


Typically, task is dependent for each other. If we ask Gradle to do a task can realize that some other tasks
need to be executed before (in the example before running Kotlin, we invoke the function hello that print
something). The right slide is a graph of all the dependences.

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.

02: Gradle Pag. 5 a 9


By development in Kotlin we will use Kotlin plugin. Kotlin(JVM) means we want to compile the code for the
java virtual machine. We have also the kotlin kapt plugin which is the annotation processor for Kotlin. Allow
us to augment the code that we write in Kotlin with extra annotation and can generate extra metadata and
extra block of code.

The ‘application’ plugin makes available the run task that allow to launch the artifact.

02: Gradle Pag. 6 a 9


Each artifact (maven one) is identified by the group name (reverse DNS), the artefact name (project) and
the version (group artefact version).

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).

02: Gradle Pag. 7 a 9


Implementation: library needed both compile time and runtime. CompileOnly only included in the compile
class path and not runtime class part (runtimeOnly vice versa). When we say runtime means that we add
the jar file as part of the execution.

For testing we have a much complex graph.

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.

02: Gradle Pag. 8 a 9


For all subproject we install dependences for maven Central. The first line buildScript is the library only to
build the project. Project2 is dependent of project1; so that maybe a module1 is a library became available
for the second project. In the module we do not put version because we already specified it before.

The main setting Gradle file need do include the module part of our project.

02: Gradle Pag. 9 a 9

You might also like