Gradle Tutorial - why you should use it and how to get started

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everybody its tom here and welcome to my channel Tom Gregory Tech and this is a Gradle tutorial and whether you're a complete Gradle novice or you've been using it for a while this video is going to really go into the fundamentals of Gradle and show you how you can build up a project so let's jump right into it so first up let's answer the question what is Gradle graidely is a build automation tool it can be used to build small projects quickly or build very complex projects that take a long time Gradle is written in groovy which is a scripting language built on top of the Java JVM it's similar to Java but has two key characteristics you need to remember to understand a Gradle build file firstly brackets are optional if the method has at least one argument also groovy supports closures defined using curly brackets you can think of a closure as a block of code that can be passed around like a variable and executed at a later point it's also worth pointing out at this point that you can also write groovy build scripts using Kotlin which is another Java JVM language for the purposes of this tutorial though we'll be focusing on the groovy version Gradle is also highly configurable meaning that even if you have some obscure requirement in your build you'll almost certainly be able to find a way to do it in Gradle why should you use Gradle it makes building and running applications very easy because it's specifically designed for that purpose it's very likely that if you tried to create a build mechanism for your application from scratch yourself you'll waste a lot of time and end up with something not as performant and streamlined as you would with Gradle there's no need to install anything on your machine with Gradle just download a repository and run the Gradle W script Gradle is concise and less verbose than tools like maven because the build script is defined in code and not XML lastly it's very performant and supports incremental builds so you don't do the same things unnecessarily for example it won't rerun the same tests again if the code hasn't changed what are the key Gradle concepts the build.gradle file is essentially your build script where you'll define how your project is built it uses groovy and consists of a Gradle DSL or domain-specific language to make defining or build as concise as possible the build.gradle file is the equivalent of mavens pom to xml tasks are the equivalent of a maven phase they can be passed the Gradle command to run like here I'm running the Gradle W build command they can also have dependencies on other tasks you can see here that the build task depends on the check and assemble tasks and you can reuse predefined tasks to achieve your desired outcome here I'm writing a task that uses the built-in exec type tasks to execute a command line this task prints out the contents of the working build directory you can see a list of all the tasks available in Gradle by running Gradle W tasks dependencies are libraries that your project depends on you can pull them from any repository you want to such as maven central or your local maven repository you can define dependencies to have a specific scope like compile and test much like with maven plugins are the main way you can build up your build script to provide more and more functionality they provide additional tasks that help build your application here you can see that when I added the Java plug-in to this simple project I get many more tasks such as build and test Gradle has a plug-in ecosystem most things that you need to do already have a plug-in it's also easy to write your own plugins and lastly plugins usually come with a default configuration but you can override them using plug-in configuration blocks here I'm configuring the Java plug-in we saw before by specifying this jar configuration block to tell the plug-in some additional jar manifest attributes i want to include next we're going to create a Gradle project from scratch we'll create a basic Java project with the ability to run it and also run tests afterwards you'll know exactly what to do to setup Gradle for your own projects at this point it's worth talking about the Gradle wrapper this is essentially a script that is committed into the project version control system that always uses a predefined version of Gradle this means that for someone who wants to build your project there's no need for them to have Gradle installed on their machine also if they have a different version of Gradle installed it doesn't matter as a script we'll always use the same predefined version for these reasons you should always use the Gradle wrapper script by running Gradle W I'm going to show you two ways to initialize your project with Gradle and set up the Gradle wrapper script the first is to use IntelliJ IDEA to create a Gradle project for you this has the advantage that it uses the bundled version of Gradle and doesn't require you to install Gradle manually just go to file new project select Gradle on the left hand side click Next choose a project group ID and artifact ID click Next if you're happy with the project name just click finish you'll see now that you have a skeleton project which already has the group and version properties specified in the build Gradle also note that you have a Gradle wrapper script and a Gradle directory which contains the Gradle wrapper properties and file the second way to achieve the same outcome is to download Gradle from Gradle Torg and install it on your machine in any directory you can then run Gradle in it to set up Gradle and its wrapper in that directory the only difference here is that we'll have to manually set the group and version properties now that we've got our basic project we can add the Java plug-in if you get a pop-up in IntelliJ about importing changes then enable auto import now add a source directory a main directory and a Java directory this is the standard project setup and you can now add a package and a Java class add a main method which just prints out some text now you can run this application and see the text output in the run output here now let's switch over to the command line and try running Gradle w build this is succeeded and actually builds a jar file in the build libs directory let's run that using Java dash jar unfortunately you can see here that we've got an error because we don't have the manifest attribute that is required by Java to describe which file to execute no problem let's switch back to build Gradle and configure the Java plug-in using the jar configuration and specifying the manifest attribute the value of this will be the main class that we want to run in this case my application let's run Gradle W build again and execute the jar file and this time it works now let's add a test for our application and a test Java directory create a package create a test class and now I'm just going to add a simple test that checks that the main method of the application doesn't throw an exception and when we add the tare sanitation you'll see that we don't actually have this available in our class path right now no problem jump back to build Gradle and we're going to add into the repository section maven central where we're going to get the j-unit dependency in the dependencies section you can use o insert to do a dependency search for Ju new add in the J unit dependency and change it to a test compile dependency so it's available for our test now we can import J unit tests and run Gradle W test and you can see Gradle is compiling the code and running the tests now that we've covered the basics of Gradle and you've seen how to create a project yourself you can try using Gradle on your own projects you can also find a ton of great resources at grad org or if you've got any specific questions then feel free to post them down below and if you've enjoyed this video and if you've got value from it and if you'd like to hear about future videos then please subscribe to my channel Tom Gregory Tech otherwise thanks a lot for watching and I'll see you in the next video bye
Info
Channel: Tom Gregory
Views: 69,345
Rating: undefined out of 5
Keywords: Gradle, Gradle tutorial, Java, build automation, build tool
Id: ojx49J1JCdQ
Channel Id: undefined
Length: 10min 38sec (638 seconds)
Published: Fri Aug 23 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.