Gradle - 4.9 Installing, Configuring, Creating build tasks
by
In this post i will be briefing about gradle & installation & configuration
Lets get started!!!
Gradle
Gradle is an open-source build automation tool focused on flexibility and performance. Gradle build scripts are written using a Groovy or Kotlin DSL
Gradle supports many major IDEs, including Android Studio, Eclipse, IntelliJ IDEA, Visual Studio 2017, and XCode. You can also invoke Gradle via its command line interface in your terminal or through your continuous integration server. Gradle build scans help you understand build results, improve build performance, and collaborate to fix problems faster.
Features of Gradle
–> Highly customizable — Gradle is modeled in a way that customizable and extensible in the most fundamental ways.
–> Fast — Gradle completes tasks quickly by reusing outputs from previous executions, processing only inputs that changed, and executing tasks in parallel.
–> Powerful — Gradle is the official build tool for Android, and comes with support for many popular languages and technologies.
How to install & configure Gradle
Prerequisites
Gradle runs on all major operating systems and requires only a Java JDK version 7 or higher to run. To check, run java -version. You should see something like this:
❯ java -version
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)
Step-1 : If Java is not installed, then install
sudo yum -y install java-1.8.0-openjdk wget unzip
then check version
java -version
Step-2 : Download Gradle
Gradle distribution archive comes in two types: “binary-only” and “complete”. The “binary-only” archive contains the Gradle software only wheres “complete” archive comes with binary, documentation and source. Run the following command to download Gradle to your system.
wget https://services.gradle.org/distributions/gradle-3.4.1-bin.zip
Step-3 : Install Gradle
Create a directory for the Gradle installation & Extract the downloaded archive to the newly created directory.
❯ mkdir /opt/gradle
❯ unzip -d /opt/gradle gradle-4.9-bin.zip
❯ ls /opt/gradle/gradle-4.9
LICENSE NOTICE bin getting-started.html init.d lib media
Step-4 : Configure your PATH environment variable to include the bin directory of the unzipped distribution, e.g.:
export PATH=$PATH:/opt/gradle/gradle-4.9/bin
Step-5 : Verify installation
Open a console (or a Windows command prompt) and run gradle -v to run gradle and display the version, e.g.:
❯ gradle -v
------------------------------------------------------------
Gradle 4.9
------------------------------------------------------------
Build time: 2018-02-21 15:28:42 UTC
Revision: 819e0059da49f469d3e9b2896dc4e72537c4847d
Groovy: 2.4.12
Ant: Apache Ant(TM) version 1.9.9 compiled on February 2 2017
JVM: 1.8.0_151 (Oracle Corporation 25.151-b12)
OS: Mac OS X 10.13.3 x86_64
Creating New Gradle Builds
First lets initialize the project
❯ mkdir basic-demo
❯ cd basic-demo
Now we can use Gradle’s init command to generate a simple project. We will explore everything that is generated so you know exactly what’s going on.
❯ gradle init
Starting a Gradle Daemon (subsequent builds will be faster)
BUILD SUCCESSFUL in 3s
2 actionable tasks: 2 executed
The command should show “BUILD SUCCESSFUL” and generate the following “empty” project. If it doesn’t, please ensure that Gradle is installed properly, and that you have the JAVA_HOME environment variable set correctly.
This is what Gradle generated for you.
Groovy
├── build.gradle (1)
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar (2)
│ └── gradle-wrapper.properties (3)
├── gradlew (4)
├── gradlew.bat (5)
└── settings.gradle (6)
Kotlin
├── build.gradle.kts (1)
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar (2)
│ └── gradle-wrapper.properties (3)
├── gradlew (4)
├── gradlew.bat (5)
└── settings.gradle.kts (6)
(1) Project configuration script for configuring tasks in the current project
(2) Gradle Wrapper executable JAR
(3) Gradle Wrapper configuration properties
(4) Gradle Wrapper script for Unix-based systems
(5) Gradle Wrapper script for Windows
(6) Settings configuration script for configuring which Projects participate in the build
Boom! Roasted. We could just end the guide here, Lets get started by creating simple build tasks
Create a task
Gradle provides APIs for creating and configuring tasks through a Groovy or Kotlin-based DSL. A Project includes a collection of Tasks, each of which performs some basic operation.
Gradle comes with a library of tasks that you can configure in your own projects. For example, there is a core type called Copy, which copies files from one location to another. The Copy task is very useful (see the documentation for details), but here, once again, let’s keep it simple. Perform the following steps:
Step-1: Inside your project directory create src and myfile.txt inside that
[root@ip-172-31-26-233 src]# ls
myfile.txt myfile1.txt
[root@ip-172-31-26-233 src]#
Step-2: Define a task called copy of type Copy (note the capital letter) in your build file that copies the src directory to a new directory called dest. (You don’t have to create the dest directory — the task will do it for you.)
Once you initialize your projects gradle init
[root@ip-172-31-26-233 basic-demo1]# gradle init
> Task :init SKIPPED
The build file 'build.gradle.kts' already exists. Skipping build initialization.
BUILD SUCCESSFUL in 4s
[root@ip-172-31-26-233 basic-demo1]#
[root@ip-172-31-26-233 basic-demo1]# ll
total 28
-rw-r--r-- 1 root root 237 Jul 25 13:09 basic-demo-1.0.zip
-rw-r--r-- 1 root root 526 Jul 25 13:16 build.gradle.kts
drwxr-xr-x 2 root root 58 Jul 25 13:13 dest
drwxr-xr-x 3 root root 21 Jul 25 03:07 gradle
-rwxr-xr-x 1 root root 5296 Jul 25 03:07 gradlew
-rw-r--r-- 1 root root 2260 Jul 25 03:07 gradlew.bat
-rw-r--r-- 1 root root 358 Jul 25 03:07 settings.gradle.kts
drwxr-xr-x 2 root root 43 Jul 25 13:02 src
drwxr-xr-x 2 root root 21 Jul 25 13:12 test1
-rw-r--r-- 1 root root 114 Jul 25 13:16 test1.zip
[root@ip-172-31-26-233 basic-demo1]#
Step-3 : Add the below tasks in build.gradle.kts
tasks.create<Copy>("copy") {
description = "Copies sources to the dest directory"
group = "Custom"
from("src")
into("dest")
}
Step-4 : Now execute your new copy task:
[root@ip-172-31-26-233 basic-demo1]# ./gradlew copy
BUILD SUCCESSFUL in 1s
1 actionable task: 1 up-to-date
[root@ip-172-31-26-233 basic-demo1]#
[root@ip-172-31-26-233 basic-demo1]#
[root@ip-172-31-26-233 basic-demo1]# cd dest;ls
abc.txt myfile.txt myfile1.txt
[root@ip-172-31-26-233 dest]#
Subscribe via RSS