What is Gradle?
Gradle is an automated build tool based on the Groovy language and mainly for Java applications and DSL syntax. Speaking of Java's automated build tools, everyone must be familiar with Ant and Maven. Gradle is such a similar tool, but it is much more powerful than Ant and Maven.
What can Gradle do?
Gradle uses easy-to-understand DSL syntax to compile, build, test, package, and deploy tasks required during the development process, making it very simple and easy to reuse. Moreover, the most important thing is that in Gradle, you can define your own model as needed, unlike in maven, xml already limits the model you use.
What are the advantages of Gradle?
Gradle has many advantages. Here we will first look at the main advantages:
1. Dependency Management
Maven is required for dependency management. I believe that one of the main reasons why many developers like maven is maven dependency management. For example, the following is the configuration of maven's junit dependency:
<Project xmlns = "Export xmlns: xsi =" http://www.w3.org/2001/xmlschema-instance" xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 www.2cto.com">
<ModelVersion> 4.0.0 </modelVersion>
<GroupId> com. mycompany. app </groupId>
<ArtifactId> my-app </artifactId>
<Packaging> jar </packaging>
<Version> 1.0-SNAPSHOT </version>
<Dependencies>
<Dependency>
<GroupId> junit </groupId>
<ArtifactId> junit </artifactId>
<Version> 3.8.1 </version>
<Scope> test </scope>
</Dependency>
</Dependencies>
</Project>
In comparison, let's see how Gradle works:
Apply plugin: "java"
Group = "com. mycompany. app"
ArchivesBaseName = "my-app"
Version = "1.0-SNAPSHOT"
Repositories {
MavenCentral ()
}
Dependencies {
TestCompile "junit: 3.8.1 ″
}
Not only does it have less code, but it looks clear at a glance.
2. Task
Similar to Ant, Gradle uses tasks as the smallest running unit.
First, let's take a look at the definition of task in ant.
<? Xml version = "1.0"?>
<Project name = "foo">
<Target name = "hello">
<Echo> Hello World </echo>
</Target>
</Project>
Let's take a look at how Gradle works.
Task hello <{
Println "Hello World"
}
For the "Hello World" of only one row, the two may look similar. However, just imagine that Ant uses a defined task to do things, while Gradle uses a Groovy dynamic script for implementation, as long as you are familiar with Groovy, you can do anything you want in the build script.
3. Flexibility
For Ant or Maven, XML or plug-ins are generally used to define the building. Due to XML defects (complicated and difficult to read, it can only describe data rather than the process), in complicated projects, maintaining XML configurations is a nightmare. Instead, Gradle is built using the groovy scripting language, so Groovy code can be flexibly used in the build, not just a single model of constraints and XML.
Task time <{
Int hours = new Date (). hours
Switch (hours ){
Case 0 .. 11:
Println "Good Morning! It's $ {hours} am ."
Break
Case 12 .. 17: // noon to 5
Println "Good Afternoon! It's $ {hours> 12? Hours-12: hours} pm ."
Break
Default: // 6 to 11 pm
Println "Good Evening! It's $ {hours-12} pm. Shouldn't you be going home ?"
}
}
4. scalability
Relying on the dynamic nature of Groovy, Gralde can write DSL-based code, which is easy to maintain for complicated projects.
In addition, Gradle also supports plug-ins. There are many available plug-ins for Gradle, such as Java, War, and Jetty, which are very convenient to use.
5. Community Support
The development of Gradle is inseparable from the support of the community. Currently, many well-known open-source components, such as Hibernate and Spring, have begun to use Gradle as an automated building tool.
This article explains why the author of Hibernate gave up maven and switched to Gradle:
In addition, the official Gradle website also provides very detailed documents and examples, it is easy to get started.
6. Summary
Finally, let's take a look at a comparison between Maven and Gradle:
Maven pros
* Lots of third party plugins for tasks
* Fairly robust IDE support (eclipse, netbeans, intellij)
* Dependency management
* Jetty plugin (you can run the web app off the compiled source with hot deployment)
Maven cons
* XML based configuration. which usually ends up being very verbose.
* Writing simple and custom tasks (for instance copy a set of files somewhere) is tedious.
You have to write a plugin and testing + development time for simple tasks may end up too long and time consuming.
For things to go smooth you usually will have to follow maven conventions and standard plugins.
* Everything is a plugin. Even simple tasks like compile and running tests.
So for a fresh project, downloading all the plugins takes considerable time.
Plus there might be updates to the plugin which will be downloaded occasionally when you run the build.
* Lots of maven plugins usually have problems and a general opinion is that it is hard to debug.
* Building is slow compared to ant and gradle
* Does not prepare the html unit test case report by default (but something called a surefire reports)
Gradle (pros)
* Based on groovy. Scripting tasks are quite coz it is in a programming language.
* Uses ivy dependency management (which uses maven repositories). Dependency downloads are quite fast.
* Integrates ant tasks also.
* Supports the standard directory layout.
So for compilation, testing and packaging no extra tasks need to be written as long the files are in the layout.
* Come bundles with a default set of plugins which are core to gradle. So it does not have to download/update plugins
* Builds are quite fast.
* Standard junit reports
* Supports jetty server
Gradle (cons)
* IDE support or the lack of it. Eclipse does not come with good support for gradle or groovy.
Intellij idea and netbeans supports it (idea supports gradle projects) however.
* Third party plugin support is not as good as maven
Overall, Gradle's advantages have become increasingly obvious. If it is a new project, do not hesitate to go directly to Gradle. There is definitely a different experience. :)
Author: I want to fly