Build with Gradle

Recently one of my friends asked me to explain what a build tool is and what is Gradle. So I thought of explaining it here and go much as further as I can go smoothly and neatly.

What is a software build ?

You write code for an application. Those are the source codes. Lets say its an android application and your write Java code. When you have done writing the code you will have to make a .apk file if you want to run it on an android device. The process of converting source code -> something deployable is called building. So building may include
  • Compiling process
  • Linking related files 
  • If your software depends on external .jars, programs or whateve, solve those dependecies
  • Package your software in a deployable manner (in this case to an .apk file) 
Build tools are the tools that can do those things.
Some good tools are
C -- make
Java -- Ant,Maven,Gradle
.net --NAnt

Do we need tools for building ?

If you are doing indpendant small scale coding, you dont any FANCY build tools. But when you have large scale developement to handle with lot of different people coding on different parts and you should have like different versions of builds for different reasons and they depend on diff third party plugins, software, files your life can be like living in hell if you dont have a build tool with you. But this not the case with interpreted programs coz interpreters dont involve a compiling process hence the interpreters ;).

What is Gradle ?

Gradle is a also a built autoamtion tool. Meaning you can automatically build your project into several builds and at the same time you can automate some tests also. So gradle is opensource and used at enterprise level which is more cool. I think it came to enterprise level just because it is opensource. Power of often source huh :D. Gradle has lot of good features of ant and Maven. Now you can build Java,C,C++ apps with gradle.

Gradle is built with a Groovy a good OO Java like scripting language. So all the gradle tasks are in Groovy and if you wanna do stuff with Gradle, you ll have to learn Groovy a lil bit. Its not that actually hard. Gradle came to popularity with the start of Android Studio. I remember when I worked with Android Studio and Gradle for the first time in 2 years back , Gradle was a crappy part of the studio with that greenish slimy logo and build scripts. But as it turns out, gradle has become a indipensable tool for me now. ;) Never judge a book by its cover, huh :D

So if you wanna start with gradle, you should have it in your system. Get with package managers or download binaries or install with ecplise. There are so much you can do with gradle but we will see most common reoccurring ones like jar configs, dependency management and configs.


gradle tasks

This can show you what you can do with gradle and are as follows.
Build tasks 
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles classes 'main'.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles classes 'test'.

Build Setup tasks
init - Initializes a new Gradle build. [incubating]
wrapper - Generates Gradle wrapper files. [incubating]

Documentation tasks
javadoc - Generates Javadoc API documentation for the main source code.

Help tasks
components - Displays the components produced by root project 'GradeDemo'. [incubating]
dependencies - Displays all dependencies declared in root project 'GradeDemo'.
dependencyInsight - Displays the insight into a specific dependency in root project 'GradeDemo'.
help - Displays a help message.
model - Displays the configuration model of root project 'GradeDemo'. [incubating]
projects - Displays the sub-projects of root project 'GradeDemo'.
properties - Displays the properties of root project 'GradeDemo'.
tasks - Displays the tasks runnable from root project 'GradeDemo'.

Verification tasks
check - Runs all checks.
test - Runs the unit tests.

Rules
Pattern: clean<TaskName>: Cleans the output files of a task.
Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration.
Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration.


apply plugin: "java" 

This will tell the gradle system that this is a Java project. So when build is done
source files will be in -> src/main
testing source files -> src/test
all your builds -> builds/lib


repositories{mavenCentral()}
dependencies {
compile  'org.glassfish.jersey.core:jersey-client:2.22.1'
runtime 'orf.groovy:groovy:2.2'
}
Gradle can reslove dependecy problems just like in Maven and pom.xml does. So when a dependency is declared it just downloads the dependecies when the build happens from a repo mentioned. Luckily we have a in built Maven repo comes with gradle ie: mavenCentral which has lot of dependencies. Or you can use Ivy whihc is like maven repo or custom define repos.
You can also specify the type of the dependency a compile time or run time. It should be in the format of Group id : artifact id: version number (ex: 'org.glassfish.jersey.core:jersey-client:2.22.1)

sourceCompatibility = 1.8
version = '2.5'
jar {
  manifest { 
    attributes "Main-Class": 'com.kapal.gradle.Main',
"Implementation-title":"Build with gradle"
"Implementation version": version
  }

We can  add attributes to Manifest.mf which is an information file which comes with built jar. You can add any attribute to the file.

configurations {
  
 customCompile
 customCompile.transitive = false
 compile.extendsFrom(customCompile)
 compile.transitive = false
 testCompile.transitive = false
    
}

You can add configure your build(s) add custom configs for build(s) and hanldle how your build happens and how your testing builds happen.

This is what you get when a succussfull build happens













Share:

0 comments: