Monthly Archives: December 2014

Gradle Build Automation – Part 5

Creating Gradle Build Scripts

  1. Gradle build consists of two important concepts (Projects and Tasks).
  2. Gradle build always consist of one or more projects.
    • A Gradle project can be WAR, JAR, ZIP file etc etc
    • A project has can be some component we want to build
    • A project can be something other than build i.e copying the files to remote server or deployment of application on server.
    • A project can have one or more tasks.
      • A task can be made up of one or more actions.
      • We can use doFirst and doLast methods to add actions to our tasks.
      • We can also use >> and << instead of doFirst and doLast.

Defining actions with the Action interface

 

 

Gradle Tricks, Hacks, Troubleshooting Issues, Misc

Task #01 : How to execute tasks in gradle from different files other build.gradle.

|—testfile.gradle

|—–task1

|—–task2

|—–task3

|—build.gradle

> gradle -q -b  testfile.gradle task1

 

Task #02: How to execute tasks in gradle from different files other build.gradle.

Gradle Build Automation – Part 4

Understanding Gradle Processes.

How Gradle Executes Scripts

  1. Gradle reads the script file.
  2. Creates a Project Object and configure it (set up data).
  3. Finally set of tasks to be executed is determined and executed.

We can project object in Gradle script with the variable name project. We can set the project description like this,

setDescription(“My First Gradle Script”)   or    project.setDescription(“My First Gradle Script”)

Gradle tasks

Lets make it simple. Project ( has one or more tasks ), Task is made up of actions.

doFirst {

println ‘Running first’
}

Playing with Groovy – Tutorial 2

Groovy Distribution

Groovy comes with following utilities

  • Groovy Intepreter
  • Groovy Compiler
  • Groovy Shell
  • Groovy Console

 

Groovy Intepreter

An Intepreter is something which executes the Source Code on the fly without generating Intermediate Files. Till now, we are running the Groovy programs with the help of Groovy Intepreter only. In Groovy distribution, the Intepreter is available in the form of groovy.bat. To execute a Groovy Program, simply type groovy followed by the path of the Groovy file. For example, the following command executes the Hello.Groovy file available in the src directory.

1 groovy src/Hello.Groovy

One thing to note about Groovy Intepreter is that, whenever a Groovy File is fed, the Groovy Parser will parse checking for any Syntax or Construct mismatch. Then the conversion of the Groovy Source File into Java Byte-code will take place, followed by the invocation of the JVM on the byte-code.

5.3) Groovy Compiler

Invoking the Groovy Intepreter against a Groovy File may be time consuming especially if the size of the Groovy File is large. In such cases, optimization of the execution of the Groovy Script Files should be taken into consideration. Execution of the script files can be optimized by using a Groovy Compiler. The two step process of executing a Groovy File using Groovy Compiler is mentioned as below,

  • Conversion of the Groovy File into a .class File using the Groovy Compiler
  • Running the Class File using the Java Intepreter

Suppose that we have a file called Hello.groovy then the following command can be used to convert the groovy file into a class file.

1 groovyc Hello.groovy –d classes

In the above command, groovyc is the name of the Groovy compiler operating on the File Hello.groovy. The option '-d' represents the directory for the Generated Java Class files.
Running the Generated Class File using the JVM mandates the classpath to point to groovy-all-1.1-BETA-1.jar which is located in the GROOVY_HOME\embeddable location. This Jar file contains groovy specified libraries which must be added to the classpath before running the Hello.class file.

1 set classpath=%classpath%;GROOVY_HOME\embeddable\groovy-all-1.1-BETA-1.jar;
2
3 java classes\Hello.class

5.4) Groovy Shell

The Groovy Shell provides the Command Line Environment for running Groovy Scripts. To start the Groovy shell, simply type groovysh (Groovy Shell) in the command prompt.

1 groovysh

After entering into the Groovy shell, statements can be entered and executed directly. For example, suppose we wish to add two numbers and tell print the result value, then the following command along with statements will help.

1 groovy> def number1 = 10
2 groovy> def number2 = 32
3 groovy> def number3 = number1 + number2
4 groovy> println "Addition of $number1 plus $number2 is $number3"
5 groovy> go
6 Addition of 10 plus 32 is 42

The 'go' command is used to execute the command containing the statements that were previously entered.

5.5) Groovy Console

The Groovy Console provides a Graphical User Interface for typing and executing Groovy Scripts. To invoke the Groovy console, type groovyConsole in the command prompt that will open the following window.
Command Statements can be typed into the upper text-field and can be executed by pressing the Ctrl+R key or Actions->Run Menu. Scripts can be saved by using Ctrl+S and the existing Script can be loaded and executed by using the combination of Ctrl+O and Ctrl+R.

– See more at: http://www.javabeat.net/introduction-to-groovy-scripting-language/#sthash.uIdTNtri.dpuf

Playing with Groovy – Tutorial 1

Groovy!  Yes 🙂 , although there are so many languages running on JVM, but the reason i choosed groovy , so that i can effectively write Java EE build automation scripts in Gradle.  Well dont need to confuse yourself with #gradle , #groovy #jvm etc… We are here to explore Groovy.

Groovy is

Object Oriented Scripting Language.

A Loosely Typed Language i.e there is no need to define the data-types for the variables and for the return type of the methods.

Provides the ability to statically type check and statically compile your code for robustness and performance … etc

Ability to Implicit Return

 

Supports Default Parameters , if u dont pass the parameter , the default value of the parameter will be utilized.

 def setPrice(price = 100.00){
    this.price = price
}


Well there are so many pros we can discuss about groovy , lets start our development with downloading Groovy

 

 

References

 

Gradle – Build Automation – Part 3

We will understand some out of box gradle jargons.

Create a file build.gradle (file name should be build.gradle)

task helloWorld << {
    println 'Welcome to Gradle First Project'
 }

From command prompt , move to the folder where build.gradle file resides and type following command in command prompt

gradle helloWorld

Welcome to Gradle First Project

BUILD SUCCESSFUL

Total time : 3.593 secs

// Gradle execution is displaying time and success message because as gradle runs in JVM, there fore it must be started each time we run a Gradle Build.

gradle -q helloWorld

Welcome to Gradle First Project

// using command line options of gradle will suppress messages except error messages.

 

We build a simple task using Gradle, although Gradle has several built-in tasks we can execute, which can be displayed using

gradle -q tasks  // displays task of our project

gradle -q dependencies  // displays project dependancies

gradle -q projects  // display sub-projects (if any) for root project

 

Task name abbreviation

A fantastic feature of Gradle , which helps us to shortcut names to our tasks  i.e, in our build.gradle file we have following tasks

Task #01 deleteOldDataIfAny
Task #02 getDataFromSvn
Task #03 compileData
Task #04 publishWAR

Instead of writing complete task name in gradle command line options, we can  use

gradle deleteOl
gradle dODI
gradle compileD
gradle cD

 

Executing Multiple Tasks

gradle deleteOldDataIfAny  getDataFromSvn //executing multiple tasks

Gradle GUI

gradle –gui //enable GUI mode for Gradle

Running Gradle Tasks without Execution

gradle –dry-run taskName //enable GUI mode for Gradle

Executing Gradle From Different Folders

D: Parent_Folder

|— Folder A

|— Folder B

|— GradleTask_Folder

|————|—————- build.gradle

Suppose we are standing at D:\Parent_Folder>, to execute build.gradle in GradleTask Folder we need to

gradle –project-dir GradleTask_Folder taskName  //executing taskName from build.gradle inside GradleTask_Folder.

 

Gradle Daemon

We know that Gradle runs on JVM and each time we invoke gradle command ( a new java virutal machine ) is started.

Gradle classes and libraries are loaded, and build is executed.

We can reduce build time , if we dont have to load JVM, Gradle Classes, libraries each time we execute a build.

gradle –daemon    //starts a new Java process that will have all classes , libraries already loaded and then build is executed.

After above command if we execute gradle command again , only Build is executed

GRADLE – BUILD AUTOMATION – PART 2

Eclipse Integration with Groovy is very simple, here we go!

  1. Using Eclipse Install New Software, install Groovy-Eclipse plug-in from IDE section.
  2. Create new project from eclipse and select new Groovy Project
  3. Create a new Groovy Class with the following code
  4. package com.vogella.groovy.first
    class FirstGroovy {
      static void main(def args){
        def mylist=[1,2,"Lars","4"]
        mylist.each{ println it }
      }
    } 
    
  5. Right-click the Groovy class, and select Run AsGroovy Script from the context menu.

 

Gradle – Build Automation – Part 1

Well Gradle time guys!

I have started to blog down things, i come across developement, first it will help others to troubleshoot and find improvement in my engineering and second to start sharing with community from whom i have learned a lot!

We will be discussing installation of Gradle in this post on Windows 7. Following steps will help in setting up Gradle in your local machine.

 

  1. Download Gradle from Gradle Website
  2. Extract the Zip File to some folder , i m using Drive D:\GradleWare\
  3. Need to Setup Environment Variable , Add GRADLE_HOME with value  D:\GradleWare and add Gradle bin to PATH environment variable i.e PATH value  = …..;D:\GradleWare\bin
  4. Open Command Prompt and Type gradle -v
------------------------------------------------------------
Gradle 2.2.1
------------------------------------------------------------
Build time: 2014-11-24 09:45:35 UTC
Build number: none
Revision: 6fcb59c06f43a4e6b1bcb401f7686a8601a1fb4a
Groovy: 2.3.6
Ant: Apache Ant(TM) version 1.9.3 compiled on December 23 2013
JVM: 1.7.0_45 (Oracle Corporation 24.45-b08)
OS: Windows 7 6.1 amd64