IntelliJ IDEA Tutorial. Working With Gradle in IntelliJ IDEA

IntelliJ IDEA Tutorial. Working With Gradle in IntelliJ IDEA

Views:93031|Rating:4.79|View Time:6:47Minutes|Likes:497|Dislikes:22

Gradle is a build and dependency management tool for the JVM. IntelliJ IDEA provides full support for Gradle projects, including:
– Creating new projects
– Importing existing projects
– Managing dependencies
– Code completion in the build files
– Running tasks and delegating IDE actions to Gradle

For more information, take a look at:

Category: Building Projects

#intellijidea_tutorial #intellijidea #gradle #jetbrains

in this screencast we'll give an overview of how to work with Gradle in IntelliJ IDEA we'll look at creating new Gradle projects importing projects managing dependencies and running tasks let's create a new Gradle project we need to choose the Java version to use we're going to use the latest version of Java 8 if our project used other languages we can choose those here but for this demo let's just stick with Java Gradle projects needed group ID and artifact ID so we'll enter those here at this stage we have some Gradle specific options also import will automatically apply changes if you update Gradle settings we'll look at this later the second setting is really useful for new projects as it sets up the directory structure for your project so will tick this will explore the third option later for this simple project we don't need to worry about multiple source sets we do want to use the Gradle wrapper for this project by using the Gradle wrapper it means that anyone who wants to work on this project code doesn't have to install Gradle separately we can choose a different project name and location if we want but the defaults look fine here IntelliJ IDEA has created a simple project for us and as we requested the default Java and test directories have been created – if we look at the basic build Gradle file that was created for us we already have a suggestion waiting we can make a simple change to give us access to the Gradle sources and documentation if we open the Gradle window we can see the Gradle tasks that are available to us and any dependencies that have been declared the basic build file included a dependency on J unit 4 so we can see this as well as the handcraft library that J unit is dependent upon these are also visible as external libraries in the project window with a Gradle prefix so you know where they came from as you'd expect IntelliJ IDEA gives you code completion in the build up Gradle file if we make changes to this file IntelliJ IDEA detects this and offer suggestions we can either apply the changes manually when we're ready or we can enable auto import to apply changes as they're made so we don't have to remember to import them IntelliJ IDEA has recognized that we're now using the groovy plug-in and has created the directories for our groovy files IntelliJ IDEA can add dependencies to our build file we can search for the library we want by name or path and select the one we want we'll see the new dependency and it's transitive dependencies in the Gradle window and in the project window the library's needed by gradle are also shown in the project library's window again with the Gradle prefix to show that these have been added by Gradle note that while it's still possible to add other external libraries here and even use them in the IDE as soon as the Gradle settings are reapplied these dependencies are removed Gradle projects should always use Gradle to manage the dependencies we can use the greater window to see which tasks are available for us to run and it's updated when we add new plugins which give us additional tasks running a task from here shows the output in the run window either as a summary of stages with time taken for each step or as a familiar command-line Gradle output we've looked at the basics of creating and working with a Gradle project it's more likely we'll be working with an existing application that uses Gradle let's import a Gradle project selecting the folder for a project that contains a build.gradle file will usually mean IntelliJ IDEA imports the project as a Gradle project however selecting the build.gradle file itself is the most certain way to import the project as a Gradle project and not some other format that may be in the project we see similar options to when we created a new project I'm not going to use auto import on this project as it has a complex build which I don't want to be run every time I make a small change to the build.gradle file I'm also not going to create directories automatically either since this is an existing project and all the folders we need should already be there we'll import this project without creating a separate module per source set and later we'll look at the difference it makes if we do tick this box this project does have the Gradle wrapper so we'll use this IntelliJ IDEA runs Gradle on the project when it's first imported to initialize it and bring in the dependencies once the structure of the application is understood we're given a list of all the IntelliJ IDEA modules that could be created to the project and we can choose which ones to create we can see that IntelliJ IDEA has created modules for all the sub projects in this Gradle project it also correctly recognizes the source and test directories as defined in the Gradle build even though this project doesn't follow the usual standard each of these modules has its own dependencies as defined by Gradle now let's see what a difference it makes to import this project with slightly different settings specifically let's select create separate module per source set so we understand what this means because we've previously imported this project we already have settings defined for IntelliJ IDEA but what we want to do is override these with our updated settings this time IntelliJ IDEA not only suggests modules for each sub project but also sub modules for all the source sets in each module this project has separate source sets for different types of tests so these can all be set up as independent IntelliJ IDEA modules we'll see why we might want this in a minute now each of these test folders is a module not just a folder inside a larger module this means that each of these modules has its own dependencies and doesn't simply share all the dependencies from the sub project this may more accurately represent the Gradle build so compiling and running inside the IDE gives the same result as compiling and running from Gradle the Gradle window shows each of the sub projects that make up the application including the different source sets with their own dependencies and of course the tasks available as we saw earlier tasks like the test tasks can be run from here and the test results will be shown as the familiar IntelliJ IDEA test results along with the Gradle task output but we can also run the tests or a subset of them from inside the IDE if we want if the project needs the build to be performed by Gradle for example to generate code or to deploy artifacts we can tick the delegate IDE build to Gradle settings if we choose this even when we run the test through the IDE IntelliJ IDEA will use Gradle to build the project before running the tests in this video we've looked at how to work with Gradle projects in IntelliJ IDEA take a look at the documentation for more details or check out the YouTube channel for more videos thanks for watching

24 Replies to “IntelliJ IDEA Tutorial. Working With Gradle in IntelliJ IDEA”

  1. maali82

    if you're new to IDEA you might want to go to FILE/SETTINGS/BUILD,EXECUTION,DEPLOYMENT/BUILDTOOLS/MAVEN/REPOSITORIES/ and there select the repo1 remote repository and press 'update' first, otherwise your Maven repository shows nothing when you wanna add artifacts (like at ~!2:30) and then grab a coffee or go for a business meeting because that'll take a while 😛

  2. C King

    My newest Java Version ist not everytime set as default.
    So I habe to set it in Project Structure manually …

    And very often there is "Project SDK is not defined" and I have to set it for every new project. Isn't there a way so set a default, which is chosen automatically? Thank you 🙂

  3. NoChance

    My source folder just wont create.. everything works fine but there is no src folder to find. Could you please help ?

  4. Sylwester Guzek

    I have following error
    "Could not determine Java version using executable C:Program FilesJavajdk-10binjava.exe."
    can you advise how to solve it ?

  5. Nkanyiso Gwane

    I downloaded IntelliJ IDEA a month ago. But now it is slow in my laptop, what can I do?

    Even VStudio IDE is slow, but other IDEs works great. Please help

Leave a Reply

Your email address will not be published. Required fields are marked *