Pages

Tuesday, March 29, 2016

Introduction to Gradle Build Automation Tool
Hi All, Here I would like to discuss about Gradle Build Automation Tool. Kindly note that this is simple learning plan that I was prepare for myself and I am still reading in this field.

Build Automation

Before discuss about Gradle build tool, I think it will be very helpful to have an understanding about build automation.

What is Build Automation Tool?

Build tools are programs that automate the creation of executable applications from source code (eg. .apk for android app). Building incorporates compiling, linking and packaging the code into a usable or executable form. Basically build automation is the act of scripting or automating a wide variety of tasks that software developers do in their day-to-day activities like:
1.      Compiling source code into binary code.
2.      Packaging that binary code.
3.      Running tests.
4.      Deployment to production systems.

Reason for Using Build Automation Tool

In small projects, developers will often manually invoke the build process. This is not practical for larger projects, where it is very hard to keep track of what needs to be built, in what sequence and what dependencies there are in the building process. Using an automation tool allows the build process to be more consistent.

Available Build Tools

·         For java - Ant,Maven,Gradle.
·         For .NET framework - NAnt
·         c# - MsBuild.

(Credits goes to Ritesh Gune – stack overflow)

Before Comparing

Usually as humans when we get or deal with something we start to compare it with other existing things which has the same capabilities .It will be a greater mess if we have no better understanding about each in individually. So this will be the right time to look at Ant and Maven before discuss about Gradle Build tool.

Introduction to Ant


Apache Ant is the Eldest fellow among modern build tools. Ant uses XML to describe the build process and its dependencies.  Ant made it easy for willing developers to adopt test-driven development, and even Extreme Programming.
One of the primary aims of Ant was to solve Make's portability problems. Ant solves this problem by providing a large amount of built-in functionality that is designed to behave the same on all platforms.

Limitations of Apache Ant

Ant build files, which are written in XML, can be complex and verbose. The complex structure (hierarchical, partly ordered, and pervasively cross-linked) of Ant documents can be a barrier to learning. The build files of large or complex projects can become unmanageably large.

Introduction to Maven


Maven is a project management tool which can used for building and managing any Java-based project. Maven’s primary goal is to allow a developer to comprehend the complete state of a development effort in the shortest period of time. In order to attain this goal.


Mavens Objectives

Making the build process easy
While using Maven doesn’t eliminate the need to know about the underlying mechanisms, Maven does provide a lot of shielding from the details.
Providing quality project information
Maven provides plenty of useful project information that is in part taken from your POM and in part generated from your project’s sources. For example, Maven can provide:
·         Change log document created directly from source control
·         Cross referenced sources
·         Mailing lists
·         Dependency list
·         Unit test reports including coverage

As Maven improves the information set provided will improve, all of which will be transparent to users of Maven.Other products can also provide Maven plugins to allow their set of project information alongside some of the standard information given by Maven, all still based on the POM.
Providing guidelines for best practices development
Maven aims to gather current principles for best practices development, and make it easy to guide a project in that direction.
For example, specification, execution, and reporting of unit tests are part of the normal build cycle using Maven. Current unit testing best practices were used as guidelines:
·         Keeping your test source code in a separate, but parallel source tree
·         Using test case naming conventions to locate and execute tests
·         Have test cases setup their environment and don’t rely on customizing the build for test preparation.
Maven also aims to assist in project workflow such as release management and issue tracking.
Maven also suggests some guidelines on how to layout your project’s directory structure so that once you learn the layout you can easily navigate any other project that uses Maven and the same defaults.
Allowing transparent migration to new features
Maven provides an easy way for Maven clients to update their installations so that they can take advantage of any changes that been made to Maven itself.
Installation of new or updated plugins from third parties or Maven itself has been made trivial for this reason.

Comparison table for major advantages/disadvantages - Maven vs Ant.


Maven
Ant
Description of project
Development of a build script per project
Invocation of defined goals (targets)
Invocation of project specific targets
Project knowledge
"Just" the build process
build lifecycle, standard project layout
too complex scripts
reusable plugins, repositories
scripts are not reusable
moving fast forward
development are slowed down
(Table credit goes to - http://www.javafaq.nu/java-article1168.html)

Brief Introduction to Gradle


Gradle is a build automation tool which is used for building, Testing, Publishing deployments, Packaging Software. Build automation means helping developers to complete their software development without unnecessary efforts for
·         Directory preparation
·         Compilation
·         Packaging
·         Managing dependencies

When we use automated build tool it manage the required libraries and dependencies. Simply gradle is a build automation tool which has
·         A very flexible general purpose build tool like Ant.
·         Switchable, build-by-convention frameworks like Maven.
·         Very powerful support for multi-project builds.
·         Very powerful dependency management (based on Apache Ivy).
·         Full support for your existing Maven or Ivy repository infrastructure.
·         Support for transitive dependency management without the need for remote repositories or pom.xml and ivy.xml files.
·         Ant tasks and builds as first class citizens.
·         Groovy build scripts.
·         A rich domain model for describing your build.

Comparison between Three Build Tools

  • Ant has the issue of lack of conventions, because of that need to specify everything. Simply it’s not readable
  • Maven has many conventions. Maven POM allows us to mention the dependency and it will resolve the dependency when it needed without developer interaction (No more struggling with jars).
  • Maven written in xml and maintainable than ant.

If you are new to this probably you may confused with the meaning of “Gradle is declarative and uses build-by-convention”. What does this mean? Before going into the depth of the meaning let’s see the difference of imperative programming and declarative programming.
  • With imperative programming, you tell the compiler what you want to happen, step by step
  • With declarative programming, on the other hand, you write code that describes what you want, but not necessarily how to get it (declare your desired results, but not the step-by-step)
Using gradle we can achieve declarative build. Declarative build is the goal of the gradle. In declarative approach we no need to deal with steps (No lots of codes in build file).It’s much readable than past once. It helps to overcome the difficulty in maintaining, accessing and testing.
Now let’s dig down into our problem.

Build-by-convention
Build by convention is the idea that if you follow the default conventions, then your builds will be much simpler. So while you can change the source directory, you don't need to explicitly specify the source directory. Gradle comes with logical defaults. This is also called convention over configuration.
In java plugin there are conventions like source must be in src/main/java,tests must be in src/main/test, resources in src/main/resources, ready jars in build/libs and so on. However, Gradle do not oblige you to use these conventions and you can change them if you want.

Declarative 
The idea behind declarative is that you don't have to work on the task level, implementing/declaring/configuring all tasks and their dependencies yourself, but can work on a higher, more declarative level. You just say "this is a Java project" (apply plugin: "java"), "here is my binary repository" (repositories {...}), "here are my sources" (sourceSets {...}), "these are my dependencies" (dependencies {...}). Based on this declarative information, Gradle will then figure out which tasks are required, what their dependencies are, and how they need to be configured.
The idea of the build being declarative is that you don't need to specify every step that needs to be done. You don't say "do step 1, do step 2, etc". You define the plugins (or tasks) that need to be applied and gradle then builds a task execution graph and figures out what order to execute things in.
Gradle has three distinct build phases

  1. Initialization
Gradle supports single and multi-project builds. During the initialization phase, Gradle determines which projects are going to take part in the build, and creates a Project instance for each of these projects.

  1. Configuration
During this phase the project objects are configured. The build scripts of all projects which are part of the build are executed. Gradle 1.4 introduced an incubating opt-in feature called configuration on demand. In this mode, Gradle configures only relevant projects

  1. Execution
Gradle determines the subset of the tasks, created and configured during the configuration phase, to be executed. The subset is determined by the task name arguments passed to the gradle command and the current directory. Gradle then executes each of the selected tasks.

Introduction to Groovy


When we discuss about gradle, the discussion may incomplete if we forget to mention a word about Groovy. Groovy is a JVM language which has the OOP capabilities .In Groovy website they have mention that







Apache Groovy is a powerfuloptionally typed and dynamic language, with static-typing and static compilation capabilities, for the Java platform aimed at improving developer productivity thanks to a concise, familiar and easy to learn syntax. It integrates smoothly with any Java program, and immediately delivers to your application powerful features, including scripting capabilities, Domain-Specific Language authoring, runtime and compile-time meta-programming and functional programming.”


Basic Gradle Commands


By typing gradle  – – help  in cmd you can see the list of commands that you can use in command line .There are list of preconfigured properties in gradle which is configured to make your work easy. You can view those properties by typing gradle  --properties in your command window . gradle task is another popular gradle command which we will use very often and will discuss it later in this post.

build.gradle File


This is the name that they use for build file in gradle.Using build.gradle file we can give instructions to the Gradle.As developers we heavily work with this file.This is the place where you can find Groovy Scripts.We can specify different tasks inside this file.When we specify tasks in build.gradle file gradle build system will execute those tasks for us.

Gradle Installation


 We can install gradle in different ways
  1. From web
  2. Using Groovy environment manager
After downloading gradle from http://gradle.org/ website extract it in your machine and set your environment variable path to the bin folder of the extracted gradle.Now you need to check whether environment variables are properly set and gradle is working on your machine. Type gradle in your cmd

Working with Tasks in Gradle


Now you can select any preferred location in your machine and create build file with .gradle extension. Every Gradle project contains a build file called build.gradle. Which contains the tasks (plugins and dependencies)
Fundamental activity in Gradle is task .Now you may think what is the meaning of a task in gradle is. Simply it’s a code that gradle execute .Each task has its own lifecycle with initializing, execution and configuring phases. Task will also contains properties and actions (first and last)
Groovy is a JVM language and its Object Oriented .In gradle build script top-level object is called as project. There is a one-to-one relationship between a Project and a build.gradle file. During build initialization, Gradle assembles a Project object for each project which is to participate in the build.If you need to learn more about project visit https://docs.gradle.org/current/dsl/org.gradle.api.Project.html
project.task("TaskA")  

Let’s see some different ways of declaring task inside build.gradle

You can use any of these methods to declare a task inside build file by typing gradle tasks in the cmd (should be inside the directory where you place your build.gradle) you can get the declared task list.

Above we have discussed that task can have properties and actions .Here I have write some task which contains actions and properties.

To view all task you can use gradle tasks & if you need to execute any task alone you can use gradle <task_name>





We have lot more to discuss about Gradle .It seems we need to discuss those in another post .I have some reference suggestions for you
·         http://www.groovy-lang.org/
Happy build :D

Monday, March 7, 2016

JavaScript Functions

If you read my previous post about objects (Nested Objects in JavaScript) in JavaScript Objects are the kings. You can find only two things in JavaScript, objects and primitives. Do you know that Functions are also objects in JavaScript, functions inherited from objects, In JavaScript we can find 2 types of JavaScript function definitions.

·         Function declaration
·         Function expression

They're actually really similar. How you call them is exactly the same, but the difference lies in how the browser loads them into the execution context. Function declarations loads before any code is executed. While function expressions loads only when the interpreter reaches that line of code. So if you try to call a function expression before it's loaded, you'll get an error but if you call a function declaration, it'll always work. Because no code can be called until all declarations are loaded, before drilling down into the depth of this problem lets have simple understanding of each.

Function Declaration

Function expression

Now it’s the right time to discuss about hoisting .Hoisting is JavaScript's default behavior of moving declarations to the top of the current scope. Hoisting applies to

  • Variable declarations 
  • Function declarations. (Because of this, JavaScript functions can be called before they are declared).
Functions defined using an expression are not hoisted. Read more on hoisting (http://www.w3schools.com/js/js_hoisting.asp )

The Function () Constructor

It’s really discouraging to write your function using default Function constructor, because of its messy appearance.

Self-Invoking Functions
This self-invoking expressions are invoked automatically. When functional expression followed by () it’s automatically invoked and this mechanism is not working with function declaration.






Also functions can use within expressions.








As I state earlier functions are objects. Then as usual functions should have methods and properties. Then you may think of what are the methods and properties of function object.






Let’s see few of the important objects things that you can do with a function in JavaScript

Function is an instance of object type


When we run above script it gives “true”


Function has its own properties




When we discuss about JavaScript functions we need to have some idea about closure and recursion.
Recursion
If you dealing with any programming language probably you may know the meaning of recursion .when a function calling itself we called it as recursion.
Always when I hear the word recursion factorial numbers are the first thing comes into my mind. Let’s look at a simple example which use recursion to display the value of a factorial number.

Closure
A closure is an inner function that has access to the outer (enclosing) function’s variables—scope chain. The closure has three scope chains: it has access to its own scope (variables defined between its curly brackets), it has access to the outer function’s variables, and it has access to the global variables. As this topic should discuss in an informative way hope to have another post on JavaScript Closure.


Hope above information help you in understanding functions in JavaScript. Until meet next time HAPPY CODING J

Thursday, March 3, 2016

Nested Objects in JavaScript

First of all let’s see how to create an empty object in JavaScript. Open up your browser console (Open new tab and right click on it and go to inspect element -> console) and type
var empty = {};   and then empty.toString(); then it will give "[object Object]"
Output shows that “empty” is an object.
As we are going to discuss about objects it’s really need to know the importance of JavaScript objects. In JavaScript objects are treated as the kings because JavaScript is designed on a simple object-based paradigm.
“Objects in JavaScript, just as in many other programming languages, can be compared to objects in real life. The concept of objects in JavaScript can be understood with real life, tangible objects.” If you want to read more on objects visit https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

For an example let’s take mobile phone as a real world object
Figure - 01


Figure - 02



Let’s create a simple HTML file which includes script tag to implement above sample in JavaScript
Figure - 03

In the above file I created a simple h3 tag call display (display is the id of h3 tag) to print the message that I pass into displayInfo( ) function.

Our main objective of this post is to learn about Nested Objects. Here we simply refresh our knowledge on JavaScript objects, properties and methods so let’s start in creating phone object.

Figure - 04

Figure - 05

Here I create an empty object call phone and print it inside my HTML document. Now we are going to include all properties into phone object.
Figure - 06


Figure - 07

Now we need to add all 4 methods into phone object.

Figure - 08


Below will be the final output of the above script
Figure - 09

Now our memorizing part is finished J .Didn’t you find something strange which we are not address up to this level
Figure - 10

Phone Battery: Simple real-world object which is located inside the phone.
Then how we should represent it inside our program. It’s useless to discuss about a phone battery without a phone. Now the Nested Objects come into action.
Figure - 11



After adding Nested Object into our existing phone object our updated code is
Figure - 12


Figure - 13

I have done small modification to our for loop to display the content of battery object which is located inside the phone.
Figure - 14


Figure - 15

Now I am going to share some different ways which we can use to represent Nested Objects.
This is the person object which contains two Nested objects call address and mood
Figure - 16

This is another way of representing vehicle object Nested Loop object
Figure - 17

Finally I include a different representation of phone object with nested battery object
Figure - 18

To check whether your objects are working fine write below code to print them.


Figure - 19


This is the end of Nested Objects .If you have any suggestions for improvements, please let me know .Happy Coding J