Gradle Introduction

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is part three of the course we're changing over now to the final build tool Gradle will see that this is much newer than ant and mavin Gradle takes the best of both those tools and combines them together firstly Gradle uses a proper programming language instead of an XML configuration file the language is called groovy if you know groovy then you can do some really advanced stuff but if you've never seen groovy don't worry they've made it so that you don't even need to know that groovy exists Gradle has a concept called tasks which are different to ant tasks so that's a bit confusing but we'll see how to program tasks in this chapter and if you want to get a bit clever you can manipulate Gradle tasks in ways that would be impossible in ants well we've already covered the big to Java build tools ants being the oldest of them certainly the most established build tool in Java but it is getting really old maven is much more sophisticated but as you've seen it's also fairly complicated I think you need a good grip of life cycles and plugins to work with maven and as you saw the pom can get a little bit complicated now Gradle is a much newer build tool and its general aim I think is to copy the best features of both ants and maven but at the same time to add much more power but whilst keeping the build script as small and simple as possible now it's not as widely used as maven or ants but I hope it's going to become more popular over time I've used it on a small number of projects so far and I've really liked what I see so I've decided to add a section on Gradle to this course which will kind of make the course feel a little bit more modern than it would do if we just had Anton maven and I hope if you like what you see you might be recommending this tool as an alternative to ant or maven on your projects now I'm going to assume that you're already familiar with ants and maven so I won't repeat any of the concepts from those tools I'm going to be often comparing Gradle speeches with ants and maven so this is going to be a much quicker section of the course because I won't need to repeat all of those basics as an example we're going to be using the mvn repository sites that we explored back in the Maven part of the course right then the big deal about Gradle is that it doesn't use XML for its configuration the problem with XML I think is that it's really just a collection of tags and values now that might be ok for holding properties or storing information about things but it's absolutely useless to do any programming with of course it is XML is not a pro language but that's something that you often need when you're defining a build some way of putting some logic into your build that's after all why we call them build scripts so let's say you want to repeat a task I don't know five times to do that in ant is really hard because there's no easy way of defining logic and loops and mavens the same really maven hides the complexity via those plugins so if you wanted to do something sophisticated in maven like some logic then you would have to write your own plugin with the logic inside the plugin Gradle has a completely fresh approach the build scripts in Gradle is code it's a proper first-class programming language so anything you can do in program code you can do in a Gradle build so the Gradle scripts are regular programs which means we get a lot more power from the scripts but in Gradle they haven't used Java as the language for writing the scripts instead they've opted for another language called groovy now I don't know if you've ever come across groovy before it's been around for a quite a few years now it has a few really interesting features first of all it runs on the Java Virtual Machine so that means it can interoperate with Java really well now before I talk about groovy let me mention that although we're going to be writing our build scripts in groovy we're still building Java code our project is still going to be written in Java can actually handle lots of different languages so you might have a project written in C and Gradle could build that but of course we're here for Java so we'll stick with our Java project but I kind of wanted to mention that because it can cause confusion you don't have to go and convert your entire project to be written in groovy another thing is that for most purposes in Gradle you don't actually have to learn any groovy programming that's because groove is really flexible as a language and what the Gradle people have done is they've taken the language and they've changed it around quite a lot to make it look like we can write build scripts without even realizing we're programming so as I said I don't know if you've done any groovy but a little plug here is that my colleague Matz is currently working on a groovy video training course so if you haven't seen groovy then sometime in around early 2014 we'll have a really good groovy course out on the market now as I say you don't need to learn groovy in order to write a build scripts in Gradle but before we start doing builds I thought a lot to show you at least a little bit of groovy and here is a groovy program a very basic hello world type program and all I'm doing here is I'm setting up a variable called courses now one of the most powerful features of groovy for me is that you can be very expressive with groovy by that I mean if you look on the right-hand side of this equal sign I'm using here's some syntax that isn't present in Java at all the square brackets are telling groovy that I want to set up a collection it happens to be a list and that list is going to store these three initial values they're just strings but they could have been objects so in one line of code I've been able to set up a collection and populate it with some good values notice at the end of the line there's no semicolon groovy has decided that semicolons are optional the next job I'm doing is I'm looping through that list and outputting each of the values in the list and you do it in a very different way to Java instead of using a for loop I can just take the variable and there's a method inside every collection in groovy called each I want to do to the each method is I pass in as a parameter the code that I want to run for each value inside that collection now that does take quite a bit of getting used to but once you're used to it you find that it is really a much more efficient way of writing for loops inside the curly brackets all I'm saying here is that I want to print out each element that's accessed in the collection and this variable called it Speltz Itt just means each individual element in the collection well as I say please don't panic here remember we don't need to understand groovy programming in order to write a Gradle build script but definitely to get more power from Gradle it might be worth learning groovy before I move on from this groovy basics I also want to mention that one of the perhaps surprising features of groovy is that it's backwards compatible with Java so if I'd wanted to I'll just change the caption here I could have written my groovy code exactly like this and of course is a good Java programmer you'll recognize that this is all valid Java syntax it does exactly the same job as my previous caption I'm declaring an ArrayList I'm adding in three strings and I'm looping around that list to output each element in turn it's the same code well apart from the names of the courses that I've added it's the same code as the previous caption now an experience groovy programmer would definitely not want you to write code like this they'd want to see code like on the previous caption I'm sure you can see already that the pure groovy code is a lot smaller and easier to write and read than the Java code before we install Gradle and write off there's build scripts I wanted to show you that truly a complete Gradle build script can often be very simple here I'm showing you a nearly complete build script this is a script that would be more than capable of compiling a full Java project and won't go into the details of it just yet but I want you to notice that it doesn't look like XML but it also doesn't really look like program code I've been talking a lot about how Gradle build scripts are written in groovy and what you can see here believe it or not is a groovy program that's one of the big features of groovy that you can twist and Bend the language around to make it look completely different on a programming language so this build script here would be capable of compiling a full Java project I'll talk about what the apply plug-in and default tasks repositories is shortly notice down here at the bottom I have a dependencies list you'll recognize that from the dependencies that we had in maven the only thing I've missed from this script is the full list of dependencies there would be a few other dependencies in those curly brackets so that's why you don't have to learn any groovy in order to understand Gradle but that's enough of the introduction let's get graidely installed and start writing a build for ourselves inside your practicals and code folder if you look in the software folder you'll find I've provided a folder called Gradle - 1.7 now this was the version of Gradle that was current at the time of this recording and as always on a virtual pair programmers course I recommend that you use the version that I've provided you that will make sure that what happens on my video is the same as what happens on your screen and once you've gone through the course you understand how to use Gradle you could then go to the Gradle website and download whatever version is the most current so assuming you're going group with Gradle 1.7 all you'll have to do there is copy that folder and drop it into the same kind of folder where you have tools such as ants and maven for me on this course it's been on the C Drive just in the root of the C Drive a lot of people don't like putting apps into the root of the C Drive it is a little messy but I do that for a small collection of core tools you might prefer to have it in your Program Files or wherever you want and similarly on a Macintosh or a Linux based operating system on a Mac I put my Gradle folder into the Applications folder but you really can put it any way you like now we could now run Gradle from the command line assuming that you add the bin directory to your path environment variable we won't be running Gradle from the command line in this chapter my plan is that in the very last chapter of the course I'll return to all three tools ant maven and Gradle and show you them all running under the command line for these chapters on Gradle we're going to be running entirely within Eclipse so now if you switch across to eclipse I want you to notice that I'm still working inside my maven project and I want to make an important point with Gradle that Gradle is entirely compatible with maven so I'm not going to start a new project I'm not going to copy this into a new folder I'm going to work entirely alongside my maven projects the pom can remain in place I could still run the pom if I want to but I'm going to add a new build alongside that pom if you need any help in opening this project refer back to chapter 8 on the course where we first opened this maven project now how do we run Gradle from within Eclipse well there are two ways of doing this and the first way which I'm not going to do on this course is that if you were to look on the gradle org website in the tooling folder there is details here under eclipse about a plugin which is developed by the spring source sts team they're the people behind the spring framework now according to this this provides the best IDE integration so far well actually I must admit that I've tried to use this plugin now on to previous projects and both times really to be honest we've failed to get it to work in the way that we want it to work now that might be our lack of knowledge about the plugin but we certainly didn't find it easy however I'm not here to criticize the spring source team these plugins tend to get better and better over time so by the time you watch this video this may be a viable approach to running Gradle from within Eclipse so feel free to investigate that option if you're interested but I'd much rather use the second approach for this course which is actually trivially simple if you go into eclipse you can simply click on the drop down list here on the external tools but now zoom into this area a bit so you can see it more clearly the external tools button here is the button with these small toolkits that's the red blob there and we use that earlier in the ant part of the course if you drop down the list and select external tools configuration you can very easily run Gradle by clicking on the program link here and that will make the button here which is the new launch configuration it will make that button active click on that button I'll just make this window wider and what you're looking for is on this main tab here find the location field and we're going to browse the file system and you're just looking for wherever you have installed Gradle so for me that's on my C Drive in the Gradle folder bin directory if you're on Windows you're going to select the bat file if you're on anything else you'll be selecting just the Gradle file so from a Gradle dot bat and then for working directory the easiest way to set this up is to click on the Browse workspace and all it wants here is in which folder do you have your Gradle build scripts so just select the project routes for me that's maven project and click OK you should see something like this and then you're done if I'll just move the window up a bit if you now click on the Run button it's maximized that console you should see a welcome to Gradle and a little bit more information appearing with a build successful if you don't see that then check that you've copied Gradle into the right folder check that you've done everything as I've just described but apart from that there's really very little bit should go wrong here well I'm probably going to get hundreds of support calls now for that but hopefully you're seeing what I can see and all we have to do now is write a build script in our project root folder so it's going to go in the same place where your pom used to go in a maven project so we'll right click on the project and select new now because I'm not using any special plug-in here there's no option for a Gradle build file so you're just going to select a regular file now the name of this file is really important the default file name is build dot Gradle and be very careful to type that exactly as you see here I'm sorry if that sounds patronizing but if you have even a minor typo you get quite a confusing error in fact in rehearsals I called this file Gradle dot build and I lost about half an hour trying to work out by noting what's happening so it's always build grade when you click finish it's not opening in the editor purely because by default Eclipse doesn't know what a dot Gradle file is actually on my other monitor I just drag this in you can see it's open the file in notepad which I don't want to use so I'll close that you'll need to right-click on the file select open and text-editor and now if I close and reopen that Association has now been remembered what can we put in here well the first concept to learning Gradle is the concept of a task we have a bit of a problem here because if you are familiar with ant and you should be then ant task remember is a small job that ant can do it's a built-in concept and you're expected to group together the tasks into targets but in Gradle a task is more like an ant targets sorry for that confusion and it does catch me out quite a lot I want to be clear that a Gradle task is typically a step in your build so of course we're going to see a task for compiling a task for deploying to Tomcat and so on so this is how you define one of these tasks we have here the keyword tasks followed by the name of the task that we're defining now I'll talk in a moment about what these symbols mean notice here there's a strange less than symbol that you probably haven't seen in any Java code before and we've got curly brackets followed by some code I'll talk about exactly what's going on here in a short while but for now I hope you can accept that this is the way that we define a task called hello but once you're inside the curly brackets things get interesting to define the steps involved in carrying out this task we use any valid groovy and remember because Java is also valid in the groovy language you could also add Java code in here as well now this is just a hello world task so I'm using the groovy print line statements don't worry about that it's exactly equivalent to the Java system dot out dot print line and it's as easy as that so let's go into eclipse and see if this works as always you'll want to follow along with me and do the same in your development environment so we're going to find a new task called hello and don't forget these double angle brackets pointing to the left-hand side they're really crucial and if you miss those out you get something subtly different I'll talk in a bit more detail shortly about what's going on there but then we have clearly bracket block and as I mentioned we can add in here any valid groovy code so for now that's just going to be a print line followed by hello now one minor problem that you might be worried about here is because we don't have a special plug-in installed we're not getting any syntax highlighting here and in particular Eclipse is thinking that there's some kind of an error here on the print line notice the red squiggly line well actually all it's saying is this word isn't correctly spelled because it's assuming as this is just a text document that we're just writing English in here so if that does bother you then you can install the plugin that I mentioned a short while ago that will give you syntax highlighting but you can still continue to run the build using this external tools configuration now the code we're writing in here isn't going to be very complex anyway so I'm more than happy to ignore the fact that all the text is black and a few of the words might have a little red line under them so that's our first script let's see if it runs well it might not just yet if you click on the external tools button again it will remember the last thing you did you don't have to pull that dialog box up every time you run but at the moment if I maximize that you can see that actually nothing is happening different to last time and that's because by default Gradle doesn't know which tasks to run first and there are a couple of things we can do here I'll tell you the sort of unusual way first of all we could go into the external tools configuration and in here under arguments we can specify the name of the task we want to run actually the task was called hello and we run now notice it's now attempting to run the hello task the first line here preceded by a coal is telling you the task name that's running and then of course the next line is the output from that task well that's a little bit but pain to have to constantly go into this external tools window every time we want to change the task we're running so I suggest you pull that window up again and remove that argument from there we'll go back to the defaults so now that's running no particular task what we can do the top of our build script is we can specify the default task that we want to run the syntax is just like this it's default tasks notice that's plural because we can specify more than one of them and then in quotation marks these are just single quote marks we can specify the default task to run and that's much more elegant because I can now just hit the button no need for that dialog box notice Gradle is a bit slow in starting up that's definite weakness of Gradle we'll probably edit out these pauses on future builds because it will be boring for you to sit there watching a blank screen but hopefully the same is happening for you that we're now getting that task running by default so as I mentioned we can have more than one default tasks in this list at the top so that gives us our first clue as to how we might sequence tasks together in a build this is just one way of doing it and it's certainly not the most elegant way but we could have a second task which we could call I don't know hello again it really doesn't matter don't forget the angle brackets again and this time I'm not very inspired right now I'm just going to print out and hello a second time now of course if we run the build again saving the changes hello again isn't referenced in the default task so I'm expecting yeah we're just seeing the hello tasks running but of course we can now just add following a comma the name of the second task that we want to run so if we try that this time not exciting but you can now see that both tasks running one after the other so that's possibly one way that we could sequence a build however there is another way of sequencing tasks and I think this is a better way you're familiar with ant which you should be then this will be very familiar because it's the same as ant we can specify that for any given task that that task depends upon another task executing first let's see how that works if we go to the hello again task if we specify in round brackets a parameter called depends on and notice it spelled all one word in camel case that means a capital o for the arm and then a colon and then in quote marks the name of the task that this depends upon so I'm saying here that the hello again task cannot possibly run unless Hello runs first but if I specify in my default tasks that the only one I want to run is the hello again task then Gradle is going to be clever enough to know that it needs to automatically run this task first so actually exactly the same way that we work with ant targets let's see if that works and you can see there that it does now this is perhaps a more elegant way of building and in the ants part of the course I called it declarative building in other words at the top of the script I'm declaring the task that I want in the end my goal the final finished job so I put the finished task right at the bottom so I'll highlight that now this task called finished you can see depends on two other tasks running first by the way this is groovy syntax I won't get too hung up on it but we've got a square brackets there to specify that we have a collection of tasks to run first if we have just single tasks we would just put it in quote marks but for multiples we use the square brackets so this will tell Gradle that before it runs finish it needs to go if you like back in the script first of all it will try to run do something in the middle well now this dependencies here as well this depends on start process and step two now luckily these don't have dependencies so these can now run so the first task to run will be start process then Gradle will run step two that means the dependencies for do something in the middle are now satisfied and do something in the middle can run all that remains now is for the finished task remember its dependencies were do something in the middle well that's already run and step two well that's already run as well as part of these something in the middle so although step two appears twice in the dependencies it will only run once so although you're going to find that Gradle feels very much like maven in the way that it works in terms of sequencing tasks together it works very much like hunts now I won't bother adding those tasks into this build hopefully you get the idea but of course you can try that for yourself just a few things to say before we move on to doing some more realistic boots I just want to remind you that inside these tasks the block here in the curly brackets is any groovy code so for example you could do a loop here well I don't know if you know any group Vienna I'm not a particular groovy expert either but just in case you're interested for example a loop in groovy looks something like this we can say for dot times and then curly brackets and inside the curly bracket the thing that we want to execute four times over what I love about groovy is that often the groove is very very readable so one is literally saying four times I want to print out let's say I just want to print out the first four integers we can access a special variable called i.t which effectively means the loop counter obviously this is useless for a build until you do something interesting inside there this is really just any code you like so there is zero one two three now even if you don't know any groovy remember that all Java code is valid groovy so if you're really stuck you could always use regular Java so I'll do a normal for loop from 0 up to I don't know 10 but I hope you're very bored at this stage this is very dull Java let's do something slightly more interesting a Java util dot date there's a new instance of Java dot util dot date and I'm just going to print out the date and time as of now now if you're a groovy programmer and you're watching this you'll be absolutely disgusted because you can do that very much more cleanly in regular groovy but I just wanted to show you that well in normal Java we would have system dot out dot print line so that will output the current date and time ten times over goodness knows what the use of that is but at least it serves to prove a point that was normal Java and it worked and it would look fairly ugly but we could put an import here at top of the script to import dart Java util and we could remove the Java dot util from there okay that's still running well if you know groovy you'll know that Java dot util is automatically imported anyway so I didn't need this line but just once more serves to prove the point that this isn't a configuration file that we're writing this is a proper program code so I'm going to remove this because I was just demonstrating there now in a few moments I'm going to move on to show you how to start writing a real build but before I do that I'm going to play around a little bit and do some slightly more advanced things and my aim here really isn't to make you an expert in Gradle but just to give you a hint as to the power that you can achieve you might find it a little bit overwhelming feel free to skip these next few minutes if you're not interested in you just want to get started doing building because these are things that you would do very rarely but there if you need them so the first thing I'm going to show you is that although we have now a task called hello again we can add to existing tasks so for example we can say for the hello again task now that's already been declared up here and I can add in further work I can for example print out something extra and if you like you can think of this as an extension of an existing task because I've taken what was in here and have added more behavior we give that a run and just have a look carefully at the output of hello again you can see now it's doing both print lines if you wanted this code to execute at the beginning of the task then you would delete those angle brackets and add dot do first instead now for me that's the difficult thing about working with Gradle remembering the little quirks of syntax and even in rehearsals for this course I accidentally left those angle brackets in there and that just will not work be quite confusing that that will force this extra line of code to happen at the start of that hello again sir so now we've got the extension happening before the original version of hello again so why is that useful well before long you're going to be given existing tasks that have already been written and by using this kind of syntax we can extend those tasks and add our own custom behavior which is going to be really useful we can also change the dependencies programmatically what do I mean by that well let's go for an example again it will be a silly example because we've not got realistic yet but I'm going to delete this dependency on the hello again task so at present when we run we're just going to see Hello again running but we can add the dependency down here in the if you like in the main part of the script we can do this programmatically by simply saying that for hello again and we want to add the dependency so use that depends on property that that's so far we've been adding in the brackets here and we can simply pass in the name of the task that we depend on so that should return us back to how the script was and it does we're now getting hello and hello again working so you might be thinking well what's the point of this well the point is that now you can use any logic you like to manipulate your dependencies so you could do things such as if there's a configuration property called debug set then you add a dependency to a particular task you could even do things like if the build is happening at night then we're going to add a dependency on the integration tests but if the build is run during the day we don't want to run the integration tests because the integration tests are too slow and really the sky's the limit so I'll just illustrate that by adding in a bit of logic here I could say well I'm going to do something silly I'm going to say if math dot random which you might know generates a random number between 0 & 1 and I'm going to see if that random number is less than not point five then we're going to add in that dependency otherwise we won't add in the dependency so that means half the time the dependency of hello will be added half the time it won't so this time around it was this time around it wasn't again it wasn't like I keep pressing that all day but I hope you get the idea this line of code here could be any interesting logic that your build depends upon now we need to get moving towards something like a realistic build so I'm going to stop playing around now with these little tasks but this is a good chance to introduce you to the reference manual if you go to the Gradle website and follow the link to the documentation of course the layout might have changed by the time you watch this but they'll always be documentation and there's a user guide which very usefully has a single page version and that means it's really easy to do a ctrl F in your browser and find anything you need the kind of stuff we've been doing in the last few minutes you'll find in chapter 6 under build script basics and there's a section there on projects and tasks and you'll see a few of the interesting things that you can do programmatically in Gradle and for example you can see there that they're showing you that you really can do code inside tasks so feel free to look at the reference manual and we will keep going back to the reference manual during the rest of this course that's the basics of Gradle of course we don't have anything like a build script yet but I hope I've given you an insight into why Gradle is more capable than Adams since the Gradle scripts is a program script you can do anything in these scripts that you could do in code that's something that would be nearly impossible in amps we've programmed a couple of Gradle tasks these are more like targets that you might be familiar with from ours the contents of the tasks are just groovy or indeed Java code we manipulated those tasks as well you can change the dependences on-the-fly for example now that might not be something that you want to do too often but it depends on what you want from Gradle but it's great to know that the ability is there but you're here to build a Java project so in the next chapter we'll get more realistic and we'll see how to replicate the bills that we've already done in fats and maven at the end of chapter 15 we'll have deployed our application to the Tomcat you
Info
Channel: Virtual Pair Programmers
Views: 36,065
Rating: undefined out of 5
Keywords: gradle, groovy, java, maven, java (programming language)
Id: mPpncYETnTg
Channel Id: undefined
Length: 38min 39sec (2319 seconds)
Published: Thu Oct 08 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.