Every framework has a purpose and Gradle is no exception. Gradle was created to make the building of deployment files simple. To achieve that goal, Gradle was made to be configurable with an easy-to-learn scripting language called Groovy, and withouth complex XML files that other tools use. Gradle can be run using simple command line scripts which allow automated builds to be set up and performed simply. Gradle also allows customization using Groovy to accommodate complex build requirements.
- [Narrator] What is Gradle and what is its purpose? Every framework and job is created to solve a specific problem. So what kind of problem does Gradle solve? Well, let's step back a bit. Our job as developers is to create applications. And these days applications can be pretty complicated and involve lots of files and artifacts. So for instance, they might contain Java source code, Java byte code, XML files, graphic and audio files. All contained within a complicated directory structure.
So building a deployable application can be pretty difficult. Now, certainly the initial difficulty is just writing all the code, and that's something we have to deal with. But that's not what Gradle's for. Gradle's purpose is to solve the problem of building and deploying the application once we've written it. How do we, in a consistent and automated way, build the deployment artifacts, in other words the runnable, executable code, that's going to run on our server or on our mobile devices? How do we perform the build in that automated repeatable fashion? And that's what Gradle is for.
A framework for building applications. So we can see in this diagram on the left we have class files, Java doc, XML properties. All these things that we just talked about. All the artifacts. We build those into deployment artifacts. Those might be Java jar files, Java war files, Java apk files. And if only we had to do this once, how great the world would be. But we have to repeat this constantly. So we're constantly modifying our code and rebuilding it. And we have to make sure every time we do that new build it works correctly.
So what does Gradle do? Sort of two things. Builds the deployment artifacts themselves, and it manages the dependencies. So when we build an application, the actual amount of code that we write ourselves may be very small, but we're using lots of code that we're leveraging from existing libraries and projects. And that concept is referred to as a dependency. And that's the other big feature of Gradle, that it will help us manage these dependencies.
So what are dependencies? They're often open-source Java libraries that somebody else has created and made available for free to the world. They might be libraries from other parts of your enterprise. So libraries that your project or your company have developed that they're making available to the enterprise, but not to the wider world. Or they might be proprietary vendor libraries. So depending on your application, you may have purchased proprietary vendor software and they may be providing libraries that you can leverage and reuse.
I use the term artifacts a lot, and what's an artifact? It's really just a general term to describe a file. That file might be Java source code or Java byte code, might be an XML file or some kind of DOT properties configuration file. For modern applications that tend to use a lot of audio and video, these files might be graphic or audio or video files. And then any other additional file resources that your project might need, although those can be referred to as artifacts. So Maven, Ivy, and ANT were all based on XML.
And XML can be a bit hard to read and a little bit verbose. Let's take a look at a quick example. Here's a very simple Maven POM file, a Project Object Model file, that's used to describe the Maven build. We can see that it's XML and it's got information about the project and any dependencies that it might have. And XML can be a little bit hard to read. So as projects got more and more complex, and the builds got correspondingly more complex, in about 2007, Gradle was released to allow builds to be described in a different language.
Rather than XML, they would be described in a programming language. And that programming language was Groovy, which was a variant of Java. And so now it became easier to make modifications to the build and make them more complicated because we were using a programming language, rather than this declarative XML technique. And so that kind of brings us up to the present. We may not be at the end of history. There may be another build tool in our future somewhere, but at least for right now Gradle appears to serve the purpose and probably is going to be the primary Java build system for many years to come.
- The purpose of Gradle
- Building a Java project with Gradle
- Adding a dependency to a project
- Creating a library module
- The Gradle project structure
- Creating new Gradle tasks
- Profiling and using the Gradle cloud