Uncategorized

What is Gradle

Gradle is a build tool which is based on groovy language which is similar to java and runs under jvm. it helps us to build our project and also do many other things like compiles the project,download required jars(dependencies) and exports the project as a war file if it’s a web-based application or as a jar if its plain java application and many more other important things, which we had to do manually if had not using a build tool like gradle .

Lets build a gradle project and understand it features and advantages over other build tools like gradle

Problems without using a build tool like gradle

Imagine you have built a java project and you want to build this project and then create a jar file out of it,If you are not using any build tool like gradle you would then probably have to build it using java command or IDE like eclipse.Let’s compare the process of building and creating a jar using java commands,Eclipse,and Gradle and then understand why gradle is better among all.

  • Building and creating jar using java commands

Create HelloWorld.java in some folder which prints Hello World ,now as we want to create a jar out of it using java commands we have to so something like this.

HelloWorld.java


public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}

  • First step is to compile so open a command prompt where this HelloWorld.java file is and run javac HelloWorld.java.It will create a HelloWorld.class file in the same d
  • Second step is to create a Manifest.text file in the same directory where HelloWorld.java exist and add Main-Class: HelloWorld to it with two blank lines and save it.Now come back to command prompt and run jar cfm HelloWorld.jar manifest.txt HelloWorld.class it will create a HelloWorld.jar in the same directory.

Manifest.txt


Main-Class: HelloWorld

1st Blank Line(Remove this in your file just add blank line)

2nd Blank Line(Remove this in your file just add blank line)

Project Directory

  • Now we can run our jar using java -jar HelloWorld.jar which will print the output as Hello World on console.

java commands

  • Building and creating jar using Eclipse

Open eclipse and create a java project named as HelloWorld and in src create a HelloWorld class

  • In eclipse build automatically happens by default ,you can verify this under project option on top eclipse.so only thing we have to do is export it as jar.
  • Next right click on HelloWorld project and click export option and in next window search for jar and then select it and click next .
  • Now click on browse and give name and path where you want to export the jar and then click next.
  • In next window keep setting as default and then click next again,here “generate manifest” is auto selected and also you can give the name of main class and click finish.your jar will be generated at the specified location.

 

  • Building and creating jar using Gradle in eclipse

Let’s create a gradle project now ,open eclipse and this time instead of java project create a gradle project by searching gradle in search box.By default gradle adds Library.java,build.gradle and other things, you can ignore that for now we will understand later,just create your HellloWorld class.

  • As manifest is required to build a jar, add manifest in the build.gradle file of HelloWorld project,you can find build.gradle at root directory itself,the final build.gradle would look like below

Build.gradle


apply plugin: 'java-library'

// In this section you declare where to find the dependencies of your project
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}

dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'

// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:23.0'

// Use JUnit test framework
testImplementation 'junit:junit:4.12'
}

jar {
manifest {
attributes 'Main-Class': 'com.codegeekslab.HelloWorld'
}
}

  • Next, at the down of eclipse you can find two tabs Gradle Tasks and Gradle Executions,under gradle tasks expand build option and double click on jar ,under “gradle executions” tab you can find the status of the jar build.
  • Now your jar is built and gradle has created build folder into your root directory of HelloWorld and inside build you can find libs folder which contains HelloWorld jar ,you can run this jar using the same java -jar HelloWorld.jar command.

As we can clearly see how easy it is to create a gradle project and building and creating a jar out of it compare to other two methods.

Understand Gradle project

The gradle based java project which we just created has many auto generated files like Library.java, LibraryTest.java, build.gradle ,gradlew and others ,as a beginner you have to mainly focus on build.gradle ,so lets understand build.gradle file.

Plugins

apply plugin: 'java-library' As this is java project gradle has added java-library plugin


<strong>Dependencies</strong>

dependencies {
api 'org.apache.commons:commons-math3:3.6.1'

implementation 'com.google.guava:guava:23.0'

testImplementation 'junit:junit:4.12'
}

This is place where you add all your jars required for your project ,As by default gradle has created Library.java and LibraryTest.java which requires junit jar so it has added testImplementation ‘junit:junit:4.12’ into it,like wise other jars like guava, commons-math.

Gradle has an amazing feature called as dependency management,Imagine you are building web based project and you need jars like servlet, jdbc ,spring etc,instead of downloading them manually you just have mention their names in build.gradle and it will get downloaded automatically,

Now consider in our HelloWorld project we want to add “spring web mvc” jar using gradle, what you would do is you go to https://mvnrepository.com search for “spring web mvc” and click on any version you want and from gradle tab you can copy the dependency compile group: 'org.springframework', name: 'spring-webmvc', version: '5.0.2.RELEASE'and add into build gradle under dependencies like below. and then in eclipse under gradle tasks click jar again and then open console tab ,you see there gradle has downloaded spring mvc web jar and as spring mvc jar is dependent on other jars like spring core ,spring aop and many others it has downloaded them as well.

console output


Working Directory: C:\Users\saukumar\Desktop\Backup\codegeekslab\codegeekslablive\HelloWorld
Gradle User Home: C:\Users\saukumar\.gradle
Gradle Distribution: Gradle wrapper from target build
Gradle Version: 4.3
Java Home: C:\java
Offline Mode Enabled: false
Gradle Tasks: jar

:compileJava
Download https://jcenter.bintray.com/org/springframework/spring-webmvc/5.0.2.RELEASE/spring-webmvc-5.0.2.RELEASE.pom
Download https://jcenter.bintray.com/io/projectreactor/reactor-bom/Bismuth-SR4/reactor-bom-Bismuth-SR4.pom
Download https://jcenter.bintray.com/org/springframework/spring-beans/5.0.2.RELEASE/spring-beans-
Download https://jcenter.bintray.com/org/springframework/spring-aop/5.0.2.RELEASE/spring-aop-5.0.2.RELEASE.jar
Download https://jcenter.bintray.com/org/springframework/spring-core/5.0.2.RELEASE/spring-core-5.0.2.RELEASE.jar
Download https://jcenter.bintray.com/org/springframework/spring-web/5.0.2.RELEASE/spring-web-5.0.2.RELEASE.jar
Download https://jcenter.bintray.com/org/springframework/spring-jcl/5.0.2.RELEASE/spring-jcl-5.0.2.RELEASE.jar
Download https://jcenter.bintray.com/org/springframework/spring-context/5.0.2.RELEASE/spring-context-5.0.2.RELEASE.jar
:processResources NO-SOURCE
:classes
:jar UP-TO-DATE

BUILD SUCCESSFUL in 9s
2 actionable tasks: 1 executed, 1 up-to-date

You can see these jars downloaded under “C:\Users\your-pc-username\.gradle\caches\modules-2\files-2.1\org.springframework” folder.

and if you click the same jar again under gradle tab it won’t download it again as gradle has already downloaded it before.

console output after clicking jar again


Working Directory: C:\Users\saukumar\Desktop\Backup\codegeekslab\codegeekslablive\HelloWorld
Gradle User Home: C:\Users\saukumar\.gradle
Gradle Distribution: Gradle wrapper from target build
Gradle Version: 4.3
Java Home: C:\java
JVM Arguments: None
Program Arguments: None
Build Scans Enabled: false
Offline Mode Enabled: false
Gradle Tasks: jar

:compileJava UP-TO-DATE
:processResources NO-SOURCE
:classes UP-TO-DATE
:jar UP-TO-DATE

BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 up-to-date

Repositories 

repositories {
jcenter()
} 

jcenter is a placec where all our jars get downloaded ,you can notice that under console tab after you add any dependency in build.gradle,it gets downloaded from “jcenter.bintray.com” Download https://jcenter.bintray.com/org/springframework/spring-webmvc/5.0.2.RELEASE/spring-webmvc-5.0.2.RELEASE.pom .

Comparison with Maven and Ant.

Before gradle people used to use maven, and before maven ant, which were also build tools but they had some issues and shortcomings which gradle addressed ,the major problem with maven and ant was that they were xml based which gradle replaced with groovy language(Imaging writing if else using <if><else> xml tags instead of our usual java if else syntax) ,and as groovy is based on java so you don’t have to learn much about groovy.

Apart from writing script in xml in maven and ant there were slow compared to gradle ,gradle is a high performance build tool and build projects very fast compared to ant and maven.Maven had a predefined build life cycle which was very difficult to modify but gradle is fully customizable and that’s the reason people are using gradle to build any new projects.