Learn the Gradle Build Script Basics in 12 Minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
plugins repositories dependencies these all make up the gradle build script but what do they do and how do we use them and what even is the build script in the first place if you find the gradle build script confusing keep watching to learn the answers to all these questions and more by seeing line by line how to create a build script for this simple java application you will be able to work more confidently in your own gradle projects before getting our hands dirty in the build script it's helpful to understand why it's there in the first place of course gradle's purpose is to build your application to do that it needs some basic information about it like what language you're using if you correctly configure gradle for your application then it will efficiently build it every single time to configure gradle we use the build script it's a file containing either groovy or kotlin code these jvm languages have many similarities but we'll use groovy because this survey says people like it anyway by writing groovy code in the build script we configure what's called the gradle project with this in place gradle does awesome things like compiling code running tests and executing our application with the background out the way let's build up a gradle build script from scratch for an application that generates inspiring messages like this follow along as we go create an empty project using gradle init or your ide here's our empty build script a file named build.gradle it's just a simple text file where we can write groovy code fortunately you don't need to be a groovy black belt to use gradle what we do in this tutorial uses very simple syntax with an empty build script gradle doesn't do anything useful we can't actually build anything that makes sense because we haven't told gradle anything about our project yet let's fix that most importantly we need to tell it what language we're using for us that's the world's best programming language java how do we configure gradle to build java applications we use a plugin a plugin is a bundle of functionality written by the gradle team themselves or by the wider community we'll apply the java plugin then explore what it does type plugins then opening and closing curly brackets in this block is where we apply any plugins we want by id the id for the java project is wait for it java type id passing the string java in single quotes that's it simple huh for core gradle plugins use this syntax but for third-party community plugins you also need to specify a version with the java plugin applied our project has extra tasks available for compiling code building jars and more if you're working in intellij idea click this button to ensure build script changes are reflected the plugin also configures specific directories for application code and resources let's go ahead and add a package for source main java call the package what you like and add a java class ascii art generator copy the code from the github repository linked in the description in summary this class takes an argument and adds a random but hilarious exclamation to the end and prints the value this code is deliberately simple but we'll extend it later to output some mesmerizing ascii art build the application by running the gradle wrapper script from your project directory passing the build task this most importantly compiles the code but does other work like generating a jar file once complete the build directory contains compiled classes and a jar file if you're curious execute the jar file with this command and a single argument to the application oops java doesn't know which class to execute we'll fix it later with some build script features we haven't covered yet but there's actually an easier way to run the application directly through gradle to do that swap out the java plugin for the application plugin this plugin automatically applies the java plug-in so we get all the same functionality plus some juicy extras for executing applications specifically it adds a run task but before we run that we need to configure the application plugin to say which class to execute in gradle plugins expose properties which change the way they behave you configure these properties on what's called ingradle and extension object accessed from the build script if that sounds complicated it's a lot simpler in practice to access the application plugins extension object called application with curly brackets in here set the main class property to the fully qualified class name string now run the run task oh that sounded a bit silly didn't it now execute the run task using dash dash args to pass the argument okay cool so you now have the basics of applying and configuring plugins before we move on note that plugins appear at the top of the build script if anything else comes first you get this error other than that the order of the build script elements isn't important it's time to jazz this application up a bit let's make it shine with some beautiful ascii art to do that there's a handy library called jfiglet and it's available from the maven central repository this neat library lets you create ascii r in just one line sweet make this small change to ascii art generator to use figlet font uh-oh when we tried to import this class it's not available any idea why jfiglet isn't on the java class path even with the right import statement the run task gives a nasty compilation error that's fair enough since we haven't told gradle about this library yet in gradle to pull in external libraries you configure dependencies these are jar files with a specific group name and version which gradle downloads from the configured repositories it then adds them to the java class path so they're available to your application first we'll tell gradle to look in the maven central repository called repositories then in curly brackets cool maven central this is a method so don't forget the round brackets now gradle knows where to look let's tell it what to look for call dependencies with curly brackets then implementation and pass a string with the dependency group name and version what's the significance of the cryptic sounding implementation its gradle lingo which means the dependency is added to both the compile and runtime class paths with the dependency added the ide now recognizes the import we just need to add this i o exception thrown by the new method to the method signature now execute run again amazing trippy [Music] okay now you know how to use external libraries here's a tasty tip to make working in the build script a piece of cake like i said earlier the build script configures the gradle project it's a project class whose api docs are on the gradle website why is this helpful well as well as the repositories and dependency methods we already used you can see other methods and properties you might be interested in for example you can set the group and version used when publishing a java library to a repository and now it's the bit i know you've been looking forward to let's add a test this is easy because the java plugin automatically runs tests whenever you run build create a new test source directory source test java and add the same package as earlier add an ascii art generator test class and copy the code from the github repository this code uses the junit 5 test framework with a single test to check that an exception isn't thrown junit 5 is an external library and how do we tell gradle about external libraries that's right independencies just like we did for jfiglet except with a subtle twist rather than implementation we call test implementation this makes the library available to tests but not to our production code a clean class path is a happy class path these are examples of what's known in gradle as dependency configurations to learn about other dependency configurations check out this video by following the link in the description with the junit 5 dependency configured run the test task this executes any tests and generates a report that's just perfect oh it's empty unfortunately gradle by default assumes junit 4. since we're so bleeding edge we'll configure the modern junit 5 instead we'll use another build script feature configuring a task we'll set up the test task to use junit 5. type the name of the task with curly brackets any code in this block configures the task you can browse the docs for a specific task type to learn how to configure it for the test task we'll call use junip platform re-run test and our test report shows one beautiful test was successful cool before we move on though note the subtle difference between configuring a plugin and configuring a task configuring a plugin may affect multiple tasks exposed by that plugin but configuring a task only affects that task now we've ticked the test checkbox let's fix the error from earlier running the jar file the manifest file which gets packaged up in the jar file is missing the main class attribute to fix that configure the jar task which generates the jar file like many tasks it gets executed automatically with build cool jar with curly brackets then manifest with curly brackets then attributes which takes a map the key is main class and the value the fully qualified class name string run build run the jar file and everything's fantastic oh we got a different error by default the jar file doesn't include the java runtime dependencies let's make another tweak to turn our jar file into a fat jar that's a jar that contains not only our code but also any library code our code uses copy this snippet from the github repository it configures the jar task to add runtime dependencies to the jar run build run the jar file and now it's working a treat and inspecting the jar file shows it contains all those tasty jade regular classes this jar runs standalone ready to be shared with your victim of choice but did you know that a gradle project can actually have multiple build scripts what yes they're called multi-project builds and help improve maintainability and performance on growing gradle projects to learn how to set one up the right way check out the awesome gradle multi-project masterclass it's free so just click the link to get started thank you so much for watching until next time
Info
Channel: Tom Gregory
Views: 1,981
Rating: undefined out of 5
Keywords:
Id: -PgdjodNf7I
Channel Id: undefined
Length: 11min 44sec (704 seconds)
Published: Sun Aug 21 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.