Difference between revisions of "Gradle"

(Gradle)
Line 1: Line 1:
==Gradle==
+
==Gradle Quickstart ==
Gradle is a dependency management and a build automation tool which was built upon the concepts of Ant and Maven.
 
  
Gradle combines good parts of both tools and builds on top of them with DSL and other improvements. It has Ant’s power and flexibility with Maven’s life-cycle and ease of use. The end result is a tool that was released in 2012 and gained a lot of attention in a short period of time. For example, Google adopted Gradle as the default build tool for the Android OS.
+
===The Java plugin===
 +
As we have seen, Gradle is a general-purpose build tool. It can build pretty much anything you care to implement in your build script. Out-of-the-box, however, it doesn’t build anything unless you add code to your build script to do so.
  
Gradle does not use XML. Instead, it had its own DSL based on Groovy (one of JVM languages). As a result, Gradle build scripts tend to be much shorter and clearer than those written for Ant or Maven. The amount of boilerplate code is much smaller with Gradle since its DSL is designed to solve a specific problem: move software through its life cycle, from compilation through static analysis and testing until packaging and deployment.
+
Most Java projects are pretty similar as far as the basics go: you need to compile your Java source files, run some unit tests, and create a JAR file containing your classes. It would be nice if you didn’t have to code all this up for every project. Luckily, you don’t have to. Gradle solves this problem through the use of plugins. A plugin is an extension to Gradle which configures your project in some way, typically by adding some pre-configured tasks which together do something useful. Gradle ships with a number of plugins, and you can easily write your own and share them with others. One such plugin is the Java plugin. This plugin adds some tasks to your project which will compile and unit test your Java source code, and bundle it into a JAR file.
  
Initially, Gradle used Apache Ivy for its dependency management. Later own it moved to its own native dependency resolution engine.
+
The Java plugin is convention based. This means that the plugin defines default values for many aspects of the project, such as where the Java source files are located. If you follow the convention in your project, you generally don’t need to do much in your build script to get a useful build. Gradle allows you to customize your project if you don’t want to or cannot follow the convention in some way. In fact, because support for Java projects is implemented as a plugin, you don’t have to use the plugin at all to build a Java project, if you don’t want to.
  
 +
We have in-depth coverage with many examples about the Java plugin, dependency management and multi-project builds in later chapters. In this chapter we want to give you an initial idea of how to use the Java plugin to build a Java project.
  
Over time, developers became more and more interested in having and working with a domain specific language – which, simply put, would allow them to solve problems in a specific domain using a language tailored for that particular domain.
+
===A basic Java project===
 +
Let’s look at a simple example. To use the Java plugin, add the following to your build file:
  
This was adopted by Gradle, which is using a DSL based on Groovy. This led to smaller configuration files with less clutter since the language was specifically designed to solve specific domain problems. Gradle’s configuration file is by convention called build.gradle.
+
'''Example: Using the Java plugin'''
 
 
Here is an example of a build.gradle file for the same simple Java project with the HelloWorld main class from before:
 
  
 +
'''build.gradle'''
 
<source>
 
<source>
 
 
apply plugin: 'java'
 
apply plugin: 'java'
 
repositories {
 
    mavenCentral()
 
}
 
 
jar {
 
    baseName = 'gradleExample'
 
    version = '0.0.1-SNAPSHOT'
 
}
 
 
dependencies {
 
    compile 'junit:junit:4.12'
 
}
 
 
</source>
 
 
We can compile the code by running:
 
 
<source>
 
 
gradle classes
 
 
</source>
 
 
At its core, Gradle intentionally provides very little functionality. Plugins add all useful features. In our example, we were using java plugin which allows us to compile Java code and other valuable features.
 
 
Gradle gave its build steps name “tasks”, as opposed to Ant’s “targets” or Maven’s “phases”. With Maven, we used Apache Maven Dependency Plugin, and it’s specific goal to copy dependencies to a specified directory. With Gradle, we can do the same by using tasks:
 
 
<source>
 
 
task copyDependencies(type: Copy) {
 
  from configurations.compile
 
  into 'dependencies'
 
}
 
 
 
</source>
 
</source>
We can run this task by executing:
 
 
<source>
 
 
gradle copyDependencies
 
 
</source>
 
 
  
Gradle effort can be summed as “convention is good and so is flexibility”.
 
  
  

Revision as of 09:34, 24 August 2018

Gradle Quickstart

The Java plugin

As we have seen, Gradle is a general-purpose build tool. It can build pretty much anything you care to implement in your build script. Out-of-the-box, however, it doesn’t build anything unless you add code to your build script to do so.

Most Java projects are pretty similar as far as the basics go: you need to compile your Java source files, run some unit tests, and create a JAR file containing your classes. It would be nice if you didn’t have to code all this up for every project. Luckily, you don’t have to. Gradle solves this problem through the use of plugins. A plugin is an extension to Gradle which configures your project in some way, typically by adding some pre-configured tasks which together do something useful. Gradle ships with a number of plugins, and you can easily write your own and share them with others. One such plugin is the Java plugin. This plugin adds some tasks to your project which will compile and unit test your Java source code, and bundle it into a JAR file.

The Java plugin is convention based. This means that the plugin defines default values for many aspects of the project, such as where the Java source files are located. If you follow the convention in your project, you generally don’t need to do much in your build script to get a useful build. Gradle allows you to customize your project if you don’t want to or cannot follow the convention in some way. In fact, because support for Java projects is implemented as a plugin, you don’t have to use the plugin at all to build a Java project, if you don’t want to.

We have in-depth coverage with many examples about the Java plugin, dependency management and multi-project builds in later chapters. In this chapter we want to give you an initial idea of how to use the Java plugin to build a Java project.

A basic Java project

Let’s look at a simple example. To use the Java plugin, add the following to your build file:

Example: Using the Java plugin

build.gradle

apply plugin: 'java'




Gradle link: https://gradle.org/

Install gradle on Mac OSX

About the App

App name: gradle App description: Build system based on the Groovy language App website: https://www.gradle.org/

Install the App

  • Press Command+Space and type Terminal and press enter/return key.
  • Run in Terminal app:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null and press enter/return key.

If the screen prompts you to enter a password, please enter your Mac's user password to continue. When you type the password, it won't be displayed on screen, but the system would accept it. So just type your password and press ENTER/RETURN key. Then wait for the command to finish.

  • Run:
brew install gradle

Done! You can now use gradle.