Add Gradle to a Java program

Keyboard Shortcuts

Learn how to add Gradle functionality to an existing Java program. You'll do this manually by creating and editing files rather than using a wizard within IntelliJ. This will give you the ability to add Gradle to any existing Java project that you may have.

- [Instructor] So now let's add Gradleto a simple Java project.We'll do this in four steps.First, we'll create the build.gradle file.Then we'll add source code declarations to build.gradleso that it knows where our source code is.And then we're using an external library.Remember, for JSON we're using the GSON library,so we'll tell Gradle that we have an external dependency.And then finally, we'll tell Gradle whereto go get that external dependency.So let's look at some code.

Now I've got IntelliJ up already.But if you're starting at this point,what you can do is load the Gradle labfrom the O two, O three begin exercise files directory.So step one is to create the build.gradle file.And that's pretty easy to do, I can highlightmy root project, this is highlightedin the project view in the upper left here.And I can use my shortcut key, command N,to create a new file, and I'll call that file build.gradle.You can see that it has a green circle decorator,and this is the icon for Gradle.

So anytime you see that you know that that fileor directory has something to do with Gradle.So I've created the build.gradle file.The second step is to add or turn on the Java plugin.And that's pretty easy to do.I just add a apply plugin statement, and I name the plugin.Now at this point, I've Gradle enabled the project.How can I tell that for sure?Well let me open the terminal,and I'll use the link at the bottomof the screen, and I get a nice terminal.

And now I can run the Gradle command to build this project.So to do that, I run the Gradle executable.That's just simply the word gradle.And then I tell it to build this project.And at that point it starts to process,and it runs through all the stepsthat a Java application needs to be built.And so you can see on line two of terminal,we're doing a compile Java, then processing resources,and then on line four, classes,and on line five, jar, and so on.And in this particular case, even though wedon't have anything to really build yet,we haven't told it where our source code is,it still runs through the steps.

I'll close the terminal by simplyclicking on the link at the bottom.So we've applied the Gradle plugin.We saw that worked on line one.Now we need to tell Gradle where our source code is.And it's at the root level,Gradle lab, in the source directory.To do that, we need to add a source sets directive,and within the braces, we'll tell it thatour main Java code, as compared to our test Java code,which we don't have any of, is in the source directory.

So we'll use the java.srcDirs directive.And the syntax for that is to use square braces.And within quotations, we'll put the name of the directory.And if we had more than one directorywhere we had Java source code, we could just use commasto add additional directories, we only have one.Now remember, we're using that JSON library.And that's external dependencies, so we needto tell Gradle that we have an external dependency.To do that, we need to add the dependency sectionon line nine, and within the braces, we'll tell itthat we have a dependency for the compile configuration.

And within quotes we'll name the dependency.Why don't we drill down into how we name dependencies?They have three parts, the first part is the nameof the organization that created the dependency.So in our case, for GSON,that's com.google.code.gson.Then we use a semicolon to separate the next part,which is the name of the dependency itself.So this is the GSON library here on line 10.And another colon, and nowwe'll tell it what version to use.

Now right now, the most current version is 2.8.0,maybe more recent for you.Okay, now on line 13, I need to do one final thing.I've told Gradle that we have a dependency,but it doesn't know where to go to get it.So let's tell it that, where is the repository?And to do that, we add a repository section, braces.And then we'll name the repository.And Gradle knows about a repository called mavenCentral,where a number of artifacts are saved in Maven format.

Now one of the things that the Gradle developers didwhich was a great idea is to make Gradle compatiblewith the Maven structure for describing dependencies.So we can use any of the existing dependenciesin the Maven repository as is.So that makes it easy to adopt Gradleif you're already using Maven.So at this point, let's test this.We'll open up the terminal again,and we'll run the Gradle build.So in the terminal we ran the Gradle build,and we can see that it's gone through thevarious steps and compiled our code for us.

Let me close the terminal session.So let me summarize, what have we done?We've created the build.gradle file.Then in line one, we've applied the Java plugin.On line three, we've told Gradle where our source code is.On line nine, we've told Gradle we have a dependency.On line 13, we've told Gradle wherethe repositories are for those dependencies.And the final thing, we've a new directory herecalled build that we haven't really talked about before.

If I drill down into that, I can see that I've got buildclasses, then the package name info, garage sales, domain.And I've got my class file, which is the compiled Java code.In the libs directory, it's actually compiledour code and turned it into a jar file.So Gradle lab is actually the result of this Java build.So for this project, the build artifact is GradleLab.jar.

So even though this is a simple project, we've now gonethrough the entire process and built this project.

Resume Transcript Auto-Scroll

Author

Released

6/9/2017

Gradle is an open source build automation tool used by Java developers to define and organize the build process. In this course, learn how Gradle is used, who needs it, how to set it up, and how to tweak its performance. James Harmon explores the reasons for the creation of Gradle, and provides a technical summary of how it works. He explains how to build a Java project with Gradle, add a dependency to a project, and create a library module. Plus, he discusses the Gradle project structure, Gradle tasks, Gradle in the cloud, and more.