Gradle Tutorial

Filed Under: Gradle

Welcome to Gradle Tutorial. In my earlier posts, we looked into What is Gradle and Gradle Eclipse Plugin.

Gradle Tutorial

Now that we know how to install gradle into your system and configure it’s eclipse plugin, it’s time to look into the gradle script. Since most of us working in java technology are already familiar with maven, we will discuss the same elements in both maven and gradle build scripts so that new users will grasp Gradle build script knowledge easily.

This post is very important for gradle beginners to understand gradle build script. Please go through each and every section clearly.

Gradle Tutorial – build.gradle

While discussing the gradle tutorial points, I will refer the build.gradle file from my previous post. Below is the build.gradle file from our earlier gradle example project.


apply plugin: 'java'
apply plugin: 'eclipse'

sourceCompatibility = 1.5
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'
testCompile group: 'junit', name: 'junit', version: '4.+'
}

test {
    systemProperties 'property': 'value'
}

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

Below is the explanation of different elements one by one.

  1. Gradle Tutorial – java version

  2. If we observe at line 4, it is referring to sourceCompatibility with value as 1.5. That means it is referring Java 1.5. By default, Eclipse Gradle plugin refers to Java 1.5 using “sourceCompatibility” element as shown below:

    
    sourceCompatibility = 1.5
    

    Now I am using Java 1.7, so above gradle script will produce following warning message.

    
    :compileTestJavawarning: [options] bootstrap class path not set in conjunction with -source 1.5
    1 warning
    

    gradle tutorial

    So we need to update this to 1.7 as shown below.

    
    sourceCompatibility = 1.7
    

    If we run same gradle build command in Eclipse IDE, we won’t see these warnings because we are using same java version in both IDE and Gradle’s build script file.

    In build.gradle file, we use “sourceCompatibility” element to define java version to be used by gradle build tool.

  3. Apply required plugins

  4. We need to use the following syntax to apply required plugins in Gradle build script file.

    
    apply plugin: <gradle-plugin-name-here>
    

    For example, if we are going to develop Java application in Eclipse IDE using Gradle, then we need to apply two plugins: “eclipse and java” as shown below

    
    apply plugin: 'java'
    apply plugin: 'eclipse'
    

    The following Gradle build script definitions are;

    
    apply plugin: 'java'
    apply plugin: 'eclipse'
    
    sourceCompatibility = 1.7
    

    similar to the following maven build script definition.

    
    <build>
      <plugins>
    	<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-eclipse-plugin</artifactId>
        <version>3.1</version>
            <configuration>
              <source>1.7</source>
              <target>1.7</target>
            </configuration>
      </plugin>
     </plugins>
    </build>
    

    Here we are defining “maven-eclipse-plugin” plugin to use Java 1.7 version.

    NOTE: Like Gradle Eclipse Plugin, Maven Eclipse Plugin uses Java 1.5 version as default value.

  5. Packaging Project files

  6. After developing any project or to test/deploy project in different environments (Dev, QA, PROD etc), we need to package it into our required format like Jar file, War file or EAR file.

    Then how to define this kind of packaging in build.gradle file so that “gradle build” command will create that file at the end of build execution.

    In Gradle, we need to use “jar” element to inform “gradle” command to create JAR File as shown below:

    
    jar {
    }
    

    This Gradle element is similar to the following Maven element

    
    <packaging>jar</packaging>
    

    To create WAR (WebApplication Archive) file, we need to use below gradle syntax.

    
    war {
    }
    

    This Gradle element is similar to the following Maven element

    
    <packaging>war</packaging>
    

    To create EAR(Enterprise Application Archive) file, we need to use this syntax:

    
    ear {
    }
    

    This Gradle element is similar to the following Maven element

    
    <packaging>ear</packaging>
    

    If we miss packaging or assemble element definition, both maven and gradle uses default value: “jar” package plugin.

    As “jar” is default assemble value, we don’t need to apply jar plugin in gradle’s build script file. However, to create WAR or EAR we need to apply the respective plugins as shown below:

    To create WAR file:

    
    apply plugin: "war"
    war {
    }
    

    To create EAR file:

    
    apply plugin: "ear"
    ear {
    }
    
  7. Jar file name and version

  8. Jar Filename: By default, gradle uses project name as jar file name. In our example, our Project name is “JavaGradleSimpleExample”.

    Jar File Version: We need to define project jar file version using “version” element in build.gradle file as shown below.

    
    version = '1.0'
    

    This Gradle element is similar to the following Maven element.

    
    <version>1.0</version>
    

    Finally, our project jar file name = Project name + Version name. That’s why our jar file name is JavaGradleSimpleExample-1.0.jar.

  9. How to provide our required Project name and Version

  10. As we discussed , we can use “version” element of build.gradle file to define Jar/WAR/EAR file’s version. But it’s recommended to use this syntax to define our jar file name and version.

    
    jar {
        baseName = 'MyJavaGradleProject'
        version =  '2.0'
    }
    

    Here baseName element defines jar file name and version element defines version number.

    Now our newly created jar file name is MyJavaGradleProject-2.0.jar.

    To create war file, we need to use this element definition:

    
    war{
        baseName = 'MyJavaGradleProject'
        version =  '2.0'
    }
    

    It will create a WAR file with this name : MyJavaGradleProject-2.0.war. We can deploy this war file into any Web or Application Server like Tomcat, Weblogic etc. or we can run this using “java -jar” command.

    NOTE:- If we define both version elements like below, what will happen?

    
    version = '1.0'
    jar {
        baseName = 'MyJavaGradleProject'
        version =  '2.0'
    }
    

    gradle command use “version” element from “jar{ }” element so that our Jar file name will be MyJavaGradleProject-2.0.jar because inner “version” element has higher priority than outer element.
    Inner “version” element overrides outer “version” element value.

  11. Gradle dependencies

  12. We need to use “dependencies” element in build.gradle file to define our project dependencies. When we define dependencies, Gradle will check those jar files from MavenRepository and download them into local and add to our project build path.

    We have defined our “JavaGradleSimpleExample” Project’s dependencies as shown below:

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

    Here we use “compile” element to define project development dependencies and “testCompile” element to define JUnit/Testing Dependencies.

    We can define above “dependencies” element even simpler as shown below:

    
    dependencies {
    compile("commons-collections:commons-collections:3.2")
    testCompile("junit:junit:4.+")
    }
    

    We can also use the following syntax to define “dependencies” element (without brackets):

    
    dependencies {
    compile "commons-collections:commons-collections:3.2"
    testCompile "junit:junit:4.+"
    }
    

    That means, without specifying group, name and version of each dependency jar file, we can define them using this format.

    But we should take care of defining them in same order: group, name then version. Instead of comma(,), We need to use colon(:) operator to separate them.

    Like maven, in gradle build script also we define group, name and version of each dependency jar file.

    This gradle element definition;

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

    is similar to the following Maven definition

    
    <dependencies>
    	<dependency>
    		<groupId>commons-collections</groupId>
    		<artifactId>commons-collections</artifactId>
    		<version>3.2</version>
    	</dependency>
    <dependencies>
    

    That means Gradle’s “group” element is similar to Maven’s “groupId” element.

    Gradle’s “name” element is similar to Maven’s “artifactId” element and Gradle’s “version” element is similar to Maven’s “version” element.

  13. Gradle Repository Definitions

  14. We use the following Gradle build script element to define our required repository to connect and download our project dependencies to the Local Repository.

    
    repositories {
        mavenCentral()
    }
    

    Here we are informing to Gradle eclipse plugin that use maven repository to download required dependencies.

Convert Gradle to Maven

Finally, if we convert our Gradle’s build.gradle build file into Maven’s pom.xml file, it will looks like the following:


<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.journaldev</groupId>
  <artifactId>JavaGradleSimpleExample</artifactId>
  <version>1.0</version>
  <packaging>jar</packaging>

    <dependencies>
      <dependency>
           <groupId>commons-collections</groupId>
           <artifactId>commons-collections</artifactId>
           <version>3.2</version>
      </dependency>
      <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.1</version>
      </dependency>
    </dependencies>

    <build>
       <plugins>
           <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-eclipse-plugin</artifactId>
               <version>3.1</version>
               <configuration>
                 <source>1.7</source>
                 <target>1.7</target>
               </configuration>
           </plugin>
     </plugins>
</build>
</project>

If we observe both Gradle build.gradle file and maven pom.xml file, we can observe that maven’s pom.xml file is XML file and need a lot of XML start tags and end tags.

Where as Gradle’s build.gradle file is plan text and simple file. This is one of the greatest advantages of gradle over maven.

Maven/Gradle Local Repository

When we execute Maven/Gradle commands, they will interact with online maven repository and download required jars into Local repository.

In Windows Systems, this Local Repository is stored at “C:\Users\[Windos-UserName]\.m2\repository” as shown below. In Unix/Mac systems, .m2 folder is created in home directory of user.

maven gradle local repository, gradle tutorial

That’s all about gradle tutorial. I have covered most of the gradle build script elements that you will use.

Reference: Official User Guide

Comments

  1. Sumanjali says:

    Hello Rambabu,

    Do we need to install maven also in our machine to run the gradle project. could you please tell me?

  2. Muralidharan says:

    I am new to gradle. But i have hands on experience on maven.

    Here packaging as war/ear/jar is explained. But How do i define pom packaging in gradle.

    I want to refer the project as folder(module) in to overlay project.

    In maven , we are refering projectname . equivalent to gradle ??

  3. Kapil says:

    Interesting article series.

  4. joe liu says:

    very good, if you are familiar with maven, it will be easy to understand. thanks.

  5. Anoop V A says:

    Very good article

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages