Up and Running with Gradle
Gradle is a build automation tool, it takes all the code and packages in your project and packages it into a single deployable unit. Build tools have become an important part of every DevOps project because you want to have a package that is immutable and consistent in every stage of your project lifecycle. This article is focused on helping you get all the basics about Gradle. The beauty of Gradle is that whether your project is big or small, It will still be able to up for the build challenge and makes the running of your applications easy. The Gradle build is written in a scripting language called Groovy that is built on a Java Virtual machine. What is even cooler about Gradle is that you do not even need to download Gradle to use, just run the ./gradlew build as the Gradle wrapper comes bundled in every Gradle project.
Key Points to note about Gradle
- build. gradle is the Gradle build script file
- It is written in Groovy Domain-Specific Language
- build. gradle is comparable to Maven’s pom.xml
- you will locate build. gradle in the top level of your build projects
The structure of build.gradle file
- Plugins: These add more functionality to our code
plugins {
id='java'
{
2. Metadata: Metadata can include the project name and version and is generally used to give information about our project
group 'org.azurenaija'
version '1.5 group1'
3. Repository: This is where our build dependencies are downloaded from
Repositories{
Azurenaija_repo()
}
4. Dependencies: These are artifacts required in order to be able to build our project
dependencies {
testCompile group: "junit' name: "junit', version:"1.5'
}
More Key Concepts
Task: This defines a unit of work that you want to execute in your build, this could be compiling your code to deploy it to remote repos. It can be invoked from the command line with ./gradlew build. The list of available tasks in your project can be seen by running ./gradlew.tasks. Also, you can even create your own customized task if what you need in your project does not exist. A task can have dependencies on another task, the task that a task is dependent on gets run first. All the dependencies between a task and a project create what is called a task graph
Wrapper: The Gradle wrapper is a script that you can use to invoke Gradle and run the build task. The wrapper is always committed to your version control system & this means that you can just check your project and build it without installing gradle on your machine. The Gradle wrapper also contains a specific version of Gradle for your project & this helps you to avoid incompatibility problems. Always use a Gradle wrapper script unless you are initializing a new Gradle project.
How to Install Gradle
Requirements
- Java must be installed on your machine
- For this exercise, I am using a Linux machine but gradle can be installed on windows and mac as well.
Step1: Validate Java in my machine
You can do so by typing the command below:
java --version
So as not to have compatibility issues, you can download Jre with the command below:
sudo apt install default-jre
Do not worry if you do not have the same version of Java, Gradle supports version 8 and above.
Step2: Download Gradle
Open a browser and go to gradle.org/releases to download the most recent and stable version of gradle. Change into the directory where you downloaded the binary into. You can use curl to download using this command
curl https://downloads.gradle-dn.com/distributions/gradle-6.8-bin.zip --output ~/gradle.zip
Next, Unzip and extract the distribution file
sudo unzip -d /opt/gradle gradle-6.8-bin.zip
Step 3: Set the environment path
We can set the path by running the command
$echo 'export PATH="$PATH:/opt/gradle/gradle-6.8/bin"' >> ~/.bashrc
if we cd into the default directory for gradle /opt/gradle and list all the files there, we will see that gradle was successfully extracted
You can also verify what version of gradle you have
gradle -v
For Windows, all the steps that you need to download gradle are here: install gradle in your environment
The basic structure of a Gradle project
Gradle supports both single and multi-structure build projects like Maven. A single project structure will produce a single build output while a multi-project structure is used for complex projects and the result is usually multiple project output.
Here are the files that make up the structure:
- build. gradle this is the main build configuration file here we can add plugins, metadata, repositories and dependencies.
- gradle directory, which contains the code and configuration for the gradle wrapper
- gradle wrapper scripts these are the gradlew script for linux and mac and also gradle bat for windows. All of these files get committed into your version control and make it possible for you not to download gradle anytime you need to work on a project.
- settings. gradle file contains additional configuration for your project outside of the build. gradle. One of such is the project name as it gets used in various places especially for naming generated build artifacts
Setting up your first Gradle project
So if you have downloaded the Gradle binary in the previous step, now is when we are going to be using it. Here are the steps to create a new Gradle project:
We first start with making a directory that we want to house our project in. In this particular case, we created one and named it azurenaija-sweet, cd into it and then start the next step from there.
gradle init command, this will create a new gradle project for you as shown below
Run through the setup wizard, from the image above,we selected basic project to generate [1]and build script [1]
Stepping through each file and directory generated in the project
We can explore the project files further
Let's look for any hidden files
lets pry into the .gitignore:
Commit everything to version control using git
Here are the steps to successfully achieve this
- Initialise the directory( /azurenaija_sweet) as a Git repository:
git init
2. Prepare all the files for committing:
git add .
3. Confirm the files that are ready tor commit:
git status
4. Commit everything:
git commit -m "Initialise azurenaija sweet with gradle"
If you have followed the steps so far, congratulations, you have successfully finished your first Gradle project and committed it to version control!
Summary
Gradle is a lightweight build automation tool that is easy to set up in any operation system, In this article, I demystified gradle so that you can set up your first project easily.