Page tree
Skip to end of metadata
Go to start of metadata

Last updated: Oct 01, 2017 10:15

By Raj Pasupathy

Gradle is an open source build automation system that builds upon the concepts of Apache Ant and Apache Maven and introduces a Groovy-based domain-specific language (DSL) instead of the XML form used by Apache Maven of declaring the project configuration.

 

Gradle is an open source build automation system that builds upon the concepts of Apache Ant and Apache Maven and introduces a Groovy-based domain-specific language (DSL) instead of the XML form used by Apache Maven of declaring the project configuration.
Visit https://gradle.org/ for more information. 

Getting Started with Gradle Project
Install Eclipse prior to starting with Gradle. Install the Eclipse Plugin by navigatingtohelp > Eclipse Marketplace as shown in the diagram below.

 

Installing Gradle Eclipse Plugin

Installing Gradle Eclipse Plugin


Ensure to select Eclipse (under Marketplace) as shown in the diagram below and type in Gradle in “find”. This should bring up the eclipse Gradle plugin (see below diagram). Install the Gradle IDE Pack and the Gradle editor as shown below. 

Eclipse Marketplace Gradle IDE


Create a new Gradle Project
Once installed, restart eclipse and then go to project > new > type in Gradle as shown in the diagram below.


New Gradle Project


Provide a Gradle project name and select “Java Quickstart” under Sample Project. 


Creating a new Gradle Project - NameTag


The below Gradle project should be created along with a sample build.gradle as shown below. 


GradleSampleProject


Now let’s see how to modify this .gradle file which is similar to maven .pom file.

Adding Gradle Wrapper to .gradle
Why do need a Gradle Wrapper? This allows other developers help execute Gradle jobs without downloading Gradle beforehand. In addition, users of the builds are guaranteed to use the specific Gradle version that was used to build the initial project.First we will need to add the wrapper task to the build.gradle file as shown below.

task wrapper(type: Wrapper) {
    gradleVersion = '2.14'
}

Note: Please remove line #4 in the build.gradle file as this is and old java version which is not needed. See below code snippet.

 

apply plugin: 'java'
apply plugin: 'eclipse'
version = '1.0'

jar {
    manifest {
        attributes 'Implementation-Title': 'Gradle Quickstart',
                   'Implementation-Version': version
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'commons-collections', name: 'commons-collections',version: '3.2'
    compile group: 'junit', name: 'junit', version: '4.+'
}

test {
    systemProperties 'property': 'value'
}

uploadArchives
{
    repositories {
       flatDir {
        dirs 'repos'
       }
    }
}

task wrapper(type: Wrapper) {
     gradleVersion = '2.14'
}

build.gradle configure


Once we add the task wrapper to the .gradle file, we need to enable the gradle view in eclipse. See below screenshots. 




Click on the G as shown in the below diagram and enter “wrapper” or click on the wrapper listed under the project. Thiswillrunthegradlefileandinthe processcreatethe 4 files listed below.

Gradle Wrapper Files: 
1) gradle-wrapper.jar
2) gradle-wrapper.properties
3) gradlew
4) gradlew.bat

 


Below diagram shows the newly created files. 

 

Executing Gradle Tasks from cmd Prompt:
Open a cmd prompt as an administrator and navigate to the project directory. Users should be able torunallthegradle commands via admin cmd prompt. Some oftheconstantlyusedone I have used in the past are:

  • gradlecleanEclipse
  • gradleassemble
  • gradleclean
  • gradlebuild
  • gradlewrapper

 


Gradle comRef: https://docs.gradle.org/current/userguide/tutorial_gradle_command_line.html#sec:excluding_tasks_from_the_command_line

Add Perfecto Jars to Gradle dependencies
The below .gradle file youcanaddmaven {} tag to the repositories andaddtheperfecto central jars. In addition, you will need to update the dependency section as shown below. This will download the appropriate jars intoyourreferencelib.

 

 

Execute ‘gradlew eclipse assemble’ which will download all the jars respectively as shown in the below. In addition, it will fix the format issue in eclipse for “Package Explorer” and “Project Explorer”.


 

Try refreshing your project to see the updated jar files in your Referenced Libraries. All your dependency jars should be downloaded here as shown below.

 


What if you don’t have access to the perfecto mobile Central Jars?
Some clients don’t have access to the outside network. Create a lib folder in your project as shown in the below (see diagram).



 

dependencies {    
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'

/* JUnit */    
Compile group: 'junit', name: 'junit', version: '4.+'

/* testNG */       
compile 'org.testng:testng:' + '6.+'

/* Perfecto Mobile Dependency */    
compile 'com.perfectomobile:pm-webdriver:' + perfectoDriverVer    
compile 'com.perfectomobile:http-client:' + perfectoDriverVer

/* local project jars */       
compile fileTree(dir: 'libs', include: '*.jar')   
}

 

Refer to the Gradle documentation for additional information:

TestNG & Gradle
See the below code snippet to use testing. You can specify the testng XML file name as shown in the diagram below.

 

test {       
    useTestNG() {            
          suiteXmlFiles << new File("src/test/resources/testng.xml")      
    }
}

 

 

Verifying all jar dependencies

The below should be able to provide you a list of all the jars used by your project.

 

/* Verify all jar dependency */
task listJars {
    configurations.compile.each {               
        File file -> println file.name        
    }
}

 

Github & Eclipse Setup
Install the github plugin in eclipse along with the SVN plugin:
http://download.eclipse.org/egit/updates
Refer to the Tips & Tricks article link on how to setup github and check-in your code.

Gradle & TestNG & Jenkins Setup – CI Automation
Refer to Tips & Tricks article: