Gradle Tutorial With Examples

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.

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.

Below is the explanation of different elements one by one.

  • Gradle Tutorial – java version

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_example_build_command_output1

 

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.

  • Apply required plugins

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.

  • Packaging Project files

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 {
}
  • Jar file name and version

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.

  • How to provide our required Project name and Version

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.

  • Gradle dependencies

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.

  • Gradle Repository Definitions

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.

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

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.

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].m2repository” as shown below. In Unix/Mac systems, .m2 folder is created in home directory of user.

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

By admin

Leave a Reply

%d bloggers like this: