Как обновить gradle в intellij idea
Перейти к содержимому

Как обновить gradle в intellij idea

  • автор:

Gradle settings

Use this page to configure settings for Gradle projects that were created, opened, or linked.

To configure the offline mode, refer to the Gradle tool window. If you need to add VM options, refer to the Gradle VM options.

Gradle user home

Use this field to specify the location for Gradle to store its global configuration properties, initialization scripts, caches, log files, and so on. For more information, refer to Gradle documentation.

The default is set to $USER_HOME/.gradle . It can be overridden in one of the following ways:

  • You can set the GRADLE_USER_HOME environment variable (for example, %APPDATA%\.gradle ). For more information, refer to Gradle documentation. The variable’s value is picked up automatically. The new path is reflected in the field. You might need to restart your IDE in order for this change to take effect.
  • You can specify the location manually: type the location in the path or click and in the dialog that opens, select the needed directory. Manually entered values take precedence over the environment variables.

If the Gradle location is set in Use Gradle from with the Specified location option and its path is defined in the environment variable GRADLE_HOME or PATH , IntelliJ IDEA deduces this location and suggests its path as the default value.

IntelliJ IDEA also supports the custom Gradle location installed from the Homebrew package manager.

Generate *.iml files for modules imported from Gradle

Select this option to store the generated .iml and library files in the .idea directory instead of idea.system.path .

It might be helpful in the following cases:

  • sharing the IDE-specific module settings via VCS since the .idea directory stores project-level settings. You can also opt for the gradle-idea-ext plugin that helps you describe the project settings in the build.gradle file.
  • correctly opening a project that contains both regular IntelliJ IDEA modules and Gradle modules.
  • accessing a project faster when you open it since IntelliJ IDEA reads the .iml files first and then starts the importing process.

When you make changes in your Gradle project, the .iml files get changed as well so don’t forget to push them along with the other project’s changes under VCS.

Enable parallel Gradle model fetching for Gradle 7.4+

Select this option to speed up the project’s import.

This option is automatically disabled for the newly created projects. It is also disabled in File | New Projects Setup | Settings for New Projects .

If you check out a project from VCS that contains the .idea folder with already disabled parallel import feature, the project will be opened with the Enable parallel Gradle model fetching for Gradle 7.4+ disabled.

You can have several linked Gradle projects when you work in IntelliJ IDEA. You can configure settings for each selected project.

Download external annotations for dependencies

When this checkbox is selected, IntelliJ IDEA downloads a file with a set of external annotations from the JetBrains public repository.

Use this section to specify what IntelliJ IDEA should use when you run tests, build, or run tasks in the selected linked project.

Build and run using

Use this list to select how you want to build and run your project. Use Gradle as a default option or select IntelliJ IDEA .

In this case when you select Build | Build Project from the main menu, IntelliJ IDEA goes through source sets in all modules executing the Gradle task classes .

Build output

If you have a pure Java or a Kotlin project, it is sometimes better to select IntelliJ IDEA for building a project. IntelliJ IDEA supports the incremental build which significantly speeds up the building process. Yet, keep in mind that the IntelliJ IDEA compiler does not support some parts of the Gradle project build processing and might cause problems in building your project correctly.

Run tests using

Use this list to select how you want to run tests in your project. Use Gradle as a default option or select IntelliJ IDEA .

Use Gradle from

Use this list to configure a Gradle version for your project.

You can select one of the following options:

  • ‘gradle-wrapper.properties’ file : this is a recommended default option that uses Gradle wrapper. In this case you delegate the update of Gradle versions to Gradle and get an automatic Gradle download for the build. This option also lets you build with a precise Gradle version. The Gradle version is saved in the gradle-wrapper.properties file in the gradle directory of your project and helps you eliminate any Gradle version problems.
  • ‘wrapper’ task in Gradle build script : select this option to configure a Gradle wrapper according to the wrapper task configuration. It might be convenient if you prefer to control which Gradle version to use in the project. If you used the default Gradle wrapper option and then switched to the Gradle wrapper task configuration, changes you made in the task automatically update during the project import.
  • Specified location : select this option if you don’t want to use the Gradle wrapper and prefer to manually download and use a specific Gradle version instead. Specify the location of your Gradle installation.

Use this field to specify the JVM under which IntelliJ IDEA will run Gradle when you import the specified Gradle project and when you execute its tasks. The default is set to your project JDK.

This field overrides any other Gradle JVM selection. You can check a process of how IntelliJ IDEA selects the Gradle JVM version in the Gradle JVM selection section.

Gradle

IntelliJ IDEA supports a fully-functional integration with Gradle that helps you automate your building process. You can easily create a new Gradle project, open and sync an existing one, work with several linked projects simultaneously, and manage them.

You can also create a Gradle project and store it in the WSL environment or open it from the WSL file system. For more information, refer to the WSL section.

Create a new Gradle project

  1. Launch the New Project wizard. If no project is currently opened in IntelliJ IDEA, click New Project on the welcome screen. Otherwise, select File | New | Project from the main menu.
  2. Name the new project and change its location if necessary.
  3. Select the Create Git repository to place the new project under version control. You will be able to do it later at any time. The .gitignore file is generated in any case.
  4. Select a language that you want to use in your project. Click if you want to add other languages available via plugins.
  5. Select Gradle in the list of Build system .
  6. Specify project’s SDK (JDK) or use the default one. If you don’t have a JDK on your machine, IntelliJ IDEA can quickly download the JDK for you. The Gradle project sync will wait until the JDK is downloaded.
  7. The selected Add sample code option will create a file with a basic code sample.
  8. Select Gradle DSL . You can select Groovy for traditional syntax or Kotlin as an alternative.
  9. In Advanced Settings , specify the fields which resemble the Maven coordinates. These settings might be helpful if you decide to deploy your project to a Maven repository. The fields you specify are added to the build.gradle file.
    • Gradle distribution : select Gradle wrapper that is a default-preferable option or Local installation when you want to use the specific local Gradle version.
    • Gradle version : if the wrapper option is selected, the latest Gradle distribution will be used unless you unselect the Auto-select option and select the specific version from the drop-down list. Alternatively, if the Local installation was selected then the version you defined locally is used.
    • Auto-select : unselect this option to specify the Gradle version you want to use in your project.
    • Use these settings for future projects : select this checkbox to use the configured Gradle version in your future projects.
    • GroupId — groupId of the new project. You can omit this field if you plan to deploy your project locally.
    • ArtifactId — artifactId that is added as a name of your new project.
    • Version — version of the new project. By default, this field is specified automatically.

For more information about Maven coordinates, refer to Maven naming conventions.

Create a Java EE project with Gradle as a build tool

  1. Launch the New Project wizard. If no project is currently opened in IntelliJ IDEA, click New Project on the welcome screen. Otherwise, select File | New | Project from the main menu.
  2. Under the Generators section, select Jakarta EE .
  3. Configure your project selecting the appropriate options such as your project’s name, location, language, and select Gradle as your build tool.
  4. Click Create . IntelliJ IDEA creates a Gradle project with the dedicated Gradle tool window and adds necessary dependencies. For the more detailed information, refer to Tutorial: Your first Java EE application.

Open an existing Gradle project

If you have the offline mode enabled in your project, the opening or re-importing of the project might fail. To fix the issue, disable the offline mode and re-import your project.

  1. If no project is currently opened in IntelliJ IDEA, click Open on the welcome screen. Otherwise, select File | Open from the main menu. If you have some custom plugins that require you to import your project from the IntelliJ IDEA model, press Ctrl+Shift+A and search for the Project from Existing Sources action.
  2. In the dialog that opens, select a directory containing a Gradle project and click OK . IntelliJ IDEA opens and syncs the project in the IDE.

If you need to adjust the Gradle settings options, refer to Gradle settings.

Check Gradle JVM and language level

  • Gradle JVM : when IntelliJ IDEA opens the Gradle project, it checks the gradle.properties file for the appropriate JVM version specified in org.gradle.java.home and uses it for the project. If it is not specified, then the project SDK is used. Alternatively, you can use the Gradle settings to configure the Gradle JVM.
  • Language level : the language level settings are applied for a source root or for a module. If a Gradle project has a single linked project then the project default language level is set to the minimum language level among the module language levels. The module language level is set to sourceCompatibility in the build.gradle file. The preview part is set to the conjunction of preview flags of the module source sets. The source set module language level is set to the corresponding combination of sourceCompatibility property and —enable-preview flag.

Link a Gradle project to an IntelliJ IDEA project

You can have multiple Gradle projects inside one IntelliJ IDEA project. It might be helpful if you keep parts of code in different projects, have some legacy projects on which you need to work, have Gradle composite build or work with microservices. You can link such projects in IntelliJ IDEA and manage them simultaneously.

When you open a Gradle project, the link of the project is established automatically and the Gradle tool window is enabled.

If an IntelliJ IDEA project is not linked to a Gradle project, then the Gradle tool window is disabled. In this case, IntelliJ IDEA displays a message with a link that quickly lets you reimport your Gradle project and enable the Gradle tool window. If the Gradle tool window is active, then you have at least one Gradle project linked.

  1. Open the Gradle tool window.
  2. In the Gradle tool window, click to attach a Gradle project.
  3. In the dialog that opens, select the desired build.gradle file, and click OK .
  4. In the Import Module from Gradle window, specify options for the Gradle project that you are trying to link and click OK . The project is linked. The Gradle tool window shows the toolbar and a tree view of Gradle entities.

If you need to link back the previously unlinked project, in the Project tool window, right-click the added build.gradle or if it is a Gradle Kotlin module the build.gradle.kts file and select Import Gradle Project .

Add a new Gradle module to an existing Gradle project

You can add a Gradle module to a project in which you are already working.

  1. In the main menu, go to File| New | Module to open the New Module wizard .
  2. If the existing project is not the Gradle project then the process of adding a module is the same as Creating a new Gradle project. If the existing project is a Gradle project, then the process of adding a new module is shorter. You need to specify the name of your module in the ArtifactId field. The rest of the information is added automatically, and you can use either the default settings or change them according to your preferences. Also, note that Add as module to field, by default, displays the name of your project to which you are trying to add a module. You can click to select a different name if you have other linked Gradle projects.

Convert a regular project into a Gradle project

  1. Open your project in IntelliJ IDEA.
  2. In the Project tool window, right-click the name of your project and select New | File .
  3. In the dialog that opens enter build.gradle and click OK .
  4. Open the build.gradle file in the editor, add the information you need and re-open your project. The following minimal information should be included in the project’s build script file:

plugins < id 'java' >group ‘org.example’ version ‘1.0-SNAPSHOT’ repositories < mavenCentral() >sourceSets < main < java < srcDirs = ['src'] >> > dependencies < compile 'junit:junit:4.12' >

Access the Gradle settings

Use the Gradle settings to configure the build and run actions for each linked Gradle project, a Gradle version, importing of the project’s changes, and so on.

Gradle settings

  1. In the Settings dialog ( Ctrl+Alt+S ) , go to Build, Execution, Deployment| Gradle . Click on the toolbar, in the Gradle tool window to access the Gradle settings.
  2. On the Gradle settings page, configure the available options and click OK to save the changes.

Configure a Gradle version for a project

IntelliJ IDEA lets you use different options to configure a Gradle version for your Gradle project. You can use the default Gradle wrapper, use a Gradle wrapper as a task, or configure a local Gradle distribution.

Gradle settings

  1. Select in the Gradle tool window to quickly access the Gradle settings page.
  2. In the Distribution list select one of the following options:
    • Wrapper : this is a recommended default option that uses Gradle wrapper. In this case you delegate the update of Gradle versions to Gradle and get an automatic Gradle download for the build. This option also lets you build with a precise Gradle version. The Gradle version is saved in the gradle-wrapper.properties file in the gradle directory of your project and helps you eliminate any Gradle version problems.
    • Wrapper task : select this option to configure a Gradle wrapper according to the wrapper task configuration. It might be convenient if you prefer to control which Gradle version to use in the project. If you used the default Gradle wrapper option and then switched to the Gradle wrapper task configuration, changes you made in the task automatically update during the project import.
    • Local installation : select this option if you want to manually download and use a specific Gradle version. Specify the location of your Gradle installation and JVM under which IntelliJ IDEA will run Gradle when you import the specified Gradle project and when you execute its tasks.

Click OK to save the changes.

Add VM options for the Gradle project

You can specify VM options for your Gradle project using the gradle.properties file.

the gradle.properties file

  1. Create or open your Gradle project.
  2. In the Project tool window, right-click the project and from the context menu, select New | File .
  3. In the New File dialog, enter gradle.properties as a filename and click OK .
  4. Open the created file in the editor and add the VM options you need.

For more information, refer to the Gradle documentation.

Increase daemon heap size

You can adjust the existing Gradle daemon heap size for your project using the gradle.properties file.

  1. Create or open your Gradle project.
  2. In the Project tool window, right-click the project and from the context menu, select New | File .
  3. In the New File dialog, enter gradle.properties as a filename and click OK .
  4. Open the created file in the editor and specify the needed daemon size using the org.gradle.jvmargs=(JVM arguments) property. The default size is -Xmx512m «-XX:MaxMetaspaceSize=256m» . Check the following code snippet as a syntax example:

org.gradle.jvmargs=-Xmx4096m

For more information, refer to the Gradle documentation.

Getting Started with Gradle

In this tutorial, we’ll create a Gradle project, will run and test it, and run the executable JAR file using Gradle.

The project used in this tutorial can be found on GitHub.

Step 1. Create a project

Let’s create a Gradle project with Java.

Create a new Gradle Project with IntelliJ IDEA

New project Gradle

  1. On the welcome screen, click New Project .
  2. On the page that opens, let’s specify our project’s name (FizzBuzz) and the location.
  3. Let’s select the Java option, which is what we need for our project and Gradle since we are creating a Gradle project.
  4. IntelliJ IDEA automatically adds a project SDK (JDK) in the JDK field. In our tutorial we use the open JDK 14 version. You can change the selected JDK, IntelliJ IDEA will download the appropriate Gradle version. The only thing you need to have is the internet connection. Let’s leave the default Groovy for Gradle DSL and unselect the Add sample code option since we’re going to add our own code from scratch.
  5. We can use the default information for ArtifactId which basically is the name of our project and leave the default information in the GroupId field. Click Create .

After we’ve created our project, and it finished indexing, let’s see what is inside:

    IntelliJ IDEA creates a project with the build.gradle file including the following code:

plugins < id 'java' >group ‘org.example’ version ‘1.0-SNAPSHOT’ repositories < mavenCentral() >dependencies < testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.2' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2' >test < useJUnitPlatform() >

As you can see, IntelliJ IDEA conveniently adds a test dependency. IntelliJ IDEA supports code completion inside the build.gradle file. So, if we decide to add more dependencies, IntelliJ IDEA will quickly locate their names and versions.

  • IntelliJ IDEA also creates the src folder with main and test subdirectories in the Project tool window. Gradle project view
  • IntelliJ IDEA enables the dedicated Gradle tool window with a liked project and its default tasks. We will use this window to run our tasks. Gradle tool windowIf you closed this window, you can always access it from the main menu by selecting View | Tool Windows | Gradle .
  • The Gradle settings in our project are used to store the information about the linked projects, Gradle JVM, and build actions. You can quickly access them from the Gradle tool window (click on the toolbar). the Gradle settingsAs you can see, the build and test actions are delegated to Gradle. Also, the Gradle wrapper was used to determine Gradle for our project.
  • The project structure ( Ctrl+Alt+Shift+S ) contains information about the project’s JDK and a language level used in the project. Project Structure
  • Step 2. Add Java code

    Now let’s create a Java application that outputs the first 100 FizzBuzz numbers.

    Add a Java class to the Gradle project

    Create a new class dialog

    1. In the Project tool window open the src folder.
    2. Click the main directory then right-click the java subdirectory and from the list select New | Package .
    3. In the New Package dialog, let’s enter a name of our package which is com.gradle.tutorial .
    4. Now right-click the package and select New | Java Class .
    5. In the New Java Class dialog specify a name of your Java class and click OK . In our case it is FizzBuzzProcessor .
    6. Add the following code to the main FizzBuzzProcessor class:

    Our application is ready. Now, let’s create the necessary tests for it.

    Create a test class

    1. Open the main class FizzBuzzProcessor in the editor, place the caret at the class name and press Ctrl+Shift+T .
    2. In the dialog that opens, let’s make sure that our testing library is JUnit4 (if you want to use JUnit5 then make sure you have the JUnit5 ) and the destination package is com.gradle.tutorial . We add the name FizzBuzzTest and leave the rest of the default options as is and click OK .
    3. Now open the created test class and add the following code:

    Step 3. Run the application with Gradle

    Let’s quickly run the application to see if it works.

    Run main class from the editor

    1. Open the main class FizzBuzzProcessor in the editor.
    2. In the gutter, click and select Run ‘FizzBuzzProcessor.main()’ . Run application in Gradle
    3. Check the result in the Run tool window. Run tool window

    Step 4. Run tests

    Now, let’s run the test we’ve created.

    Run tests in a Gradle project

    We can run our test from the editor or from the Gradle tool window using the test task. We will use the editor.

    Gradle Run test from the gutter

    • Click in the gutter of the editor.

    The result of the test will be displayed in the Run tool window.

    Run tool window /test passed

    If we change the default number in one of the tests, it will fail.

    Run tool window / test failed

    As you can see, the Run tool window displays information obout the failed test including the specific line of the code where the error occurred.

    Step 5. Create an executable JAR file

    Now let’s build our application to create an executable JAR file.

    1. In the Project tool window, double-click the build.gradle file to open it in the editor.
    2. Add the following code:

  • Click in the editor to load the changes to your project.
  • In the Gradle tool window, open the project’s node, then the Tasks node and double-click the build task to run it. Gradle tool window: build taskIntelliJ IDEA creates the build directory that contains our JAR file. Project tool window: build directoryYou can run the created JAR file in the command line with java -jar command.
  • Check the Run tool window for the results. Run tool window: build taskNote that the build task includes the test task that Gradle executes. So, if we make a mistake in one of our tests, the test task will fail and the build task will fail as well. Run tool window: build with failed test
  • Step 6. Run the JAR file with Gradle

    Now let’s tweak the build.gradle file a little bit more, so we can execute our JAR file in the Run anything window.

    Run the JAR file

    1. In the Project tool window, double-click the build.gradle file to open it in the editor.
    2. Let’s add id ‘application’ to the plugins section and the following code:

    application < mainClassName = 'com.gradle.tutorial.FizzBuzzProcessor' >

  • Click in the editor to load the changes to your project.
  • In the Gradle tool window, open the project’s node, then the Tasks node. We can see that Gradle added the distribution node. Open the node and double-click the assembleDist task to run it. If we check the build directory now, we’ll see that IntelliJ IDEA created additional directories. Project tool window: build directory
  • In the Gradle tool window, click on the toolbar.
  • In the window that opens, enter the gradlew run command. Run anything: gradlew runWe should have the same result as when we ran the application in the IntelliJ IDEA editor. Run tool window: run task outputAlternatively, you can execute the run task under the application node. Gradle tool window: run task
  • How can i change the Gradle Version in InteliJ?

    I have the Error: «Found invalid Gradle JVM configuration» «JDK 17.0.1 isn´t compatible with gradle 7.1. Please fix JAVA_HOME enviroment variable» Im not sure why it says gradle 7.1. The gradle Version i have is 7.4.2. This version should work with JDK 17

    asked Jul 2, 2022 at 16:26
    23 3 3 bronze badges

    1 Answer 1

    Take a look at the gradle-wrapper.properties file in the ./gradle/wrapper directory.

    In the file, you can define the distribution URL from which Gradle is loaded. For example:

    distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-bin.zip 

    As far as I know, IntelliJ adopts the version.

    According to the Gradle documentation, Gradle 7.3 is the first Gradle version that supports JDK 17. You could try that one.

    Добавить комментарий

    Ваш адрес email не будет опубликован. Обязательные поля помечены *