Introduction to Gradle: A Modern Build System
- Why Use Gradle Versus Maven or Ant?
- Installing Gradle
- Creating Tasks
- Using the Gradle Wrapper
I was once told, "Show me a developer who likes Ant or Maven, and surely he is one who hasn't used them long enough." More than a decade old, they are beginning to show their age.
Gradle is a build automation tool that integrates and improves Ant and Maven fundamentals. Instead of clunky XML files as in an Ant project, it uses the Groovy programming language to script all parts of the software development life cycle. It can leverage Maven conventions for familiarity while making it easy to customize to the needs of your project. Users are able to add, extend, and override tasks and plugins to create new functionality. Though Gradle is primarily used for Java, Grails, and Android projects, there are community plugins for building C++ and Mac apps as well as generating Linux packages.
In this article, you learn how to build, manage, and package projects with Gradle.
Why Use Gradle Versus Maven or Ant?
In addition to a long and clunky XML file, there is no standard format for an Ant project. Unless your company has code guidelines, each project’s structure might be unique. Much of the XML in the build.xml file is telling Ant where to find the files to compile and the project’s dependencies. Having to write and manage all this XML can be tedious.
Maven also uses XML files to run a build, but has a very different philosophical view than Ant. As opposed to a Wild West mentality when it comes to project structure, Maven has a standard way to structure your projects. We can see that basic structure listed in the following table:
Path |
Description |
|
/pom.xml |
File describing how to build the project. |
/src/main |
Source code for the project. |
/src/main/resources |
Resources needed by the project build. |
/src/test |
Source code for the project's test. |
/src/test/resources |
Resources needed by the test suite. |
The benefit of this is once you learn the structure of a standard project, you know the structure of all Maven projects. Another improvement over Ant is in the area of dependency management. Maven uses collections of libraries called repositories that are organized by project and version number. The most commonly used repository, or repo for short, is Maven Central. When a project is run for the first time, the dependency JARs are downloaded from the repos listed in the project's POM (Project Object Model) XML file. It's also possible to have many local or remote repos. Maven also has a plugin system for extending functionality. Here is a sample pom.xml file:
[Sample pom.xml file] <?xml version="1.0"?> <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/maven-v4_0_0.xsd"> <parent> <groupId>org.sonatype.oss</groupId> <artifactId>oss-parent</artifactId> <version>5</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>org.mirah</groupId> <artifactId>shared</artifactId> <packaging>pom</packaging> <version>0.1.0-SNAPSHOT</version> <name>Mirah Shared</name> <url>http://mirah.org</url> <description>The Mirah Programming Language</description> <issueManagement> <system>Github issues</system> <url>https://github.com/mirah/mirah/issues</url> </issueManagement> <scm> <connection>scm:git:git://github.com/mirah/mirah.git</connection> <developerConnection>scm:git:git@github.com:mirah/mirah.git</developerConnection> <url>http://github.com/mirah/mirah</url> </scm> <licenses> <license> <name>Apache License, Version 2</name> <url>http://www.apache.org/licenses/LICENSE-2.0.html</url> <distribution>repo</distribution> </license> </licenses> <developers> <developer> <id>headius</id> <name>Charles Nutter</name> <email>headius@headius.com</email> </developer> <!-- TODO: Add other developers --> </developers> <modules> <module>maven/mirah</module> <module>maven/mirah-complete</module> </modules> <build> <defaultGoal>install</defaultGoal> </build> </project>
Gradle is somewhat of an evolution of Ant and Maven. An idiomatic Gradle project shares the same format as a Maven project. Gradle can make use of Maven repositories and, as opposed to boatloads of XML, it uses a Groovy DSL (Domain Specific Language) to define tasks and run builds.