IntelliJ Gradle Support: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(39 intermediate revisions by the same user not shown)
Line 12: Line 12:


=Concepts=
=Concepts=
==Gradle Projects and IntelliJ Projects==
Multiple Gradle projects can exists inside an [[IntelliJ_Concepts#Project|IntelliJ Project]], but the usual setup is to have one IntelliJ Project per Gradle project. The relationship between an IntelliJ Project and a Gradle project is established when the Gradle project is opened from the IDE, by opening the directory containing the Gradle project (<code>build.gradle</code> or <code>settings.gradle</code>). Also see [[#Link_a_Gradle_Project_to_an_IntelliJ_Project|Link a Gradle Project to an IntelliJ Project]] below.
==Gradle JVM==
==Gradle JVM==
{{External|https://www.jetbrains.com/help/idea/gradle.html#gradle_jvm}}
{{External|https://www.jetbrains.com/help/idea/gradle.html#gradle_jvm}}
IDE determines the Gradle JVM according to the following algorithm, evaluating in order:
IDE determines the Gradle JVM according to the following algorithm, evaluating in order:
* When the project is opened, check <code>gradle.properties</code> in the root of the project for <code>[[Gradle_System_Properties#org.gradle.java.home|org.gradle.java.home]]</code> system property and use the value if found.
* When the project is opened, check <code>gradle.properties</code> in the root of the project for <code>[[Gradle_System_Properties#org.gradle.java.home|org.gradle.java.home]]</code> system property and use the value if found.
* Use the Project SDK.
* Use the value specified in [[#Gradle_Settings_Gradle_JVM|Gradle Settings → Gradle JVM]]. By default, that value is [[IntelliJ_Concepts#Project_SDK|Project SDK]]. The "Gradle Settings" UI gets that information from the project's <code>.idea/gradle.xml</code>. If no "gradleJvm" option is present, the Project SDK is assumed.
* Use the value specified in Gradle Settings
<syntaxhighlight lang='xml'>
<project version="4">
  <component name="GradleSettings">
    <option name="linkedExternalProjectsSettings">
      <GradleProjectSettings>
        <option name="gradleJvm" value="1.8 (2)"/>
      </GradleProjectSettings>
    </option>
  </component>
</project>
</syntaxhighlight>
 
==Language Level==
The language level settings are applied for a source root or for a module. For a module,  the module language level is set to <code>[[Gradle_Java_Plugin#sourceCompatibility|sourceCompatibility]]</code> in the build.gradle file. If a IDE Gradle project has a single linked Gradle project then the project default language level is set to the minimum language level among the module language levels. <font color=darkgray>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 <code>sourceCompatibility</code> property and <code>--enable-preview</code> flag.</font>
==<span id='Gradle_Version'>Gradle Distribution==
The distribution (version) of Gradle to be used for a project can be configured in [[#Gradle_Settings|Gradle Settings]] by selecting one of the following sources:
* <code>[[Gradle_Wrapper#gradlew-wrapper.properties|$PROJECT_HOME/gradle/wrapper/gradle-wrapper.properties]]</code>. This is the default option and instructs IntelliJ to use the same Gradle version <code>./gradlew</code> would use. If this option is used, Gradle update is delegated to Gradle, which will automatically download gradle from the build. If this option is selected, the <code>distributionType</code> configuration option in <code>$PROJECT/.idea/gradle.xml</code> is set to the value "DEFAULT_WRAPPED".
* The "wrapper" task in the Gradle build script. If this option is selected, the <code>distributionType</code> configuration option in <code>$PROJECT/.idea/gradle.xml</code> is set to the value "WRAPPED". <font color=darkgray>TODO: https://www.jetbrains.com/help/idea/gradle.html#gradle_version_install</font>.
* A specific location. If this option is selected, the <code>distributionType</code> configuration option in <code>$PROJECT/.idea/gradle.xml</code> is set to the value "LOCAL". <font color=darkgray>TODO: https://www.jetbrains.com/help/idea/gradle.html#gradle_version_install</font>
 
If asked to specify Gradle home, and get "Gradle location is incorrect", use the "libexec" subdirectory (/usr/local/Cellar/gradle/4.9/libexec).
 
==JVM Options for Gradle==
{{External|https://www.jetbrains.com/help/idea/gradle.html#gradle_vm_options}}
JVM options can be added to the Gradle project by specifying them in the <code>[[Gradle.properties|gradle.properties]]</code> file.
==Build Delegation==
IntelliJ can be configured to execute the build, even if it uses Gradle dependency and other metadata, or it can delegate the build to Gradle. This can be set from [[#Build_and_run|Gradle Settings]] "Build and run using" option. If "IntelliJ IDEA" is chosen, IntelliJ will execute the build. If "Gradle (Default)" is chosen, Gradle will execute the build. If IntelliJ is used to build, it'll do an [[IntelliJ_Concepts#Incremental_Build|incremental build]]. The setting corresponds to the <code>delegatedBuild</code> configuration option in <code>$PROJECT/.idea/gradle.xml</code>. For "IntelliJ IDEA" builds, <code>delegatedBuild</code> is set to "false". For "Gradle" builds, <code>delegatedBuild</code> is set to "true".
 
Also see: {{Internal|IntelliJ_Concepts#Compile_Server|Compile Server}}
 
==Test Execution Delegation==
IntelliJ can be configured to execute the tests itself, delegate to Gradle, or choose the mode on a test-by-test basis. This can be set from [[#Build_and_run|Gradle Settings]] "Run tests using" option. If "IntelliJ IDEA" is chosen, IntelliJ will execute the tests. If "Gradle (Default)" is chosen, Gradle will execute the tests. The setting corresponds to the <code>testRunner</code> configuration option in <code>$PROJECT/.idea/gradle.xml</code>. For "IntelliJ IDEA" test execution, <code>testRunner</code> is set to "PLATFORM". For "Gradle" builds, <code>testRunner</code> is set to "GRADLE". For a test-by-test basis test execution, <code>testRunner</code> is set to "CHOOSE_PER_TEST"
 
==Gradle Settings==
==Gradle Settings==
{{External|https://www.jetbrains.com/help/idea/gradle-settings.html}}
{{External|https://www.jetbrains.com/help/idea/gradle-settings.html}}
====Gradle user home====
This fields specified the location of stored Gradle caches, downloaded files, etc. If Gradle location has been defined by the environment variables <code>GRADLE_HOME</code> or <code>PATH</code>, IntelliJ deduces this location, and suggests this path as the default value. If not, the path must be specified manually.
====Generate *.iml files for modules imported from Gradle====
Usually disabled.
====Gradle projects====
=====Download external annotations for dependencies=====
=====Build and run=====
See [[#Build_Delegation|Build Delegation]] and [[#Test_Execution_Delegation|Test Execution Delegation]] above.
====<span id='Gradle_Settings_Gradle_JVM'></span>Gradle JVM====
See [[#Gradle_JVM|Gradle JVM]] above.
====Use Gradle from====
See [[#Gradle_Distribution|Gradle Distribution]] above.


=Operations=
=Operations=
Line 26: Line 75:
==Open an Existing Gradle Project==
==Open an Existing Gradle Project==
{{External|https://www.jetbrains.com/help/idea/gradle.html#gradle_import_project_start}}
{{External|https://www.jetbrains.com/help/idea/gradle.html#gradle_import_project_start}}
 
==Link a Gradle Project to an IntelliJ Project==
=Gradle Home=
{{External|https://www.jetbrains.com/help/idea/gradle.html#link_gradle_project}}
 
==Add a New Gradle Module to an Existing Gradle Project==
If asked to specify Gradle home, and get "Gradle location is incorrect", use the "libexec" subdirectory (/usr/local/Cellar/gradle/4.9/libexec).
{{External|https://www.jetbrains.com/help/idea/gradle.html#gradle_add_module}}


=<span id='IDEA_Plugin'></span>IDEA Gradle Plugin=
=<span id='IDEA_Plugin'></span>IDEA Gradle Plugin=
Line 38: Line 87:


=<span id='Import_Project'></span>Importing a Project form a Gradle Model=
=<span id='Import_Project'></span>Importing a Project form a Gradle Model=
<font color=darkgray>


'''Use auto-import''' - If selected, re-import of the project is done automatically every time you make changes to the project.
'''Use auto-import''' - If selected, re-import of the project is done automatically every time you make changes to the project.


'''Group modules'''. See [[IntelliJ_Concepts#Grouping_Modules|Grouping Modules]].
'''Group modules'''. See [[IntelliJ_Concepts#Grouping_Modules|Grouping Modules]].
</font>
=Troubleshooting=


=SNAPSHOT Refresh Problem=
==SNAPSHOT Refresh Problem==
 
There are cases when Gradle pulls the latest version of the SNAPSHOT and caches it in its [[Gradle_Dependencies_and_Dependency_Configurations#The_Dependency_Cache|dependency cache]], but IntelliJ IDEA Gradle plugin does not seem to notice it and uses an old version of the snapshot. This is troublesome during debugging, when the source code as displayed by IntelliJ does not match the bytecode being debugged.
There are cases when Gradle pulls the latest version of the SNAPSHOT and caches it in its [[Gradle_Dependencies_and_Dependency_Configurations#The_Dependency_Cache|dependency cache]], but IntelliJ IDEA Gradle plugin does not seem to notice it and uses an old version of the snapshot. This is troublesome during debugging, when the source code as displayed by IntelliJ does not match the bytecode being debugged.



Latest revision as of 00:38, 1 July 2021

External

Internal

Overview

Gradle is supported by IntelliJ via two plugins, which are installed and enabled by default: "Gradle" and "Gradle Extension". Gradle integration allows creating a Gradle project from the IDE, or opening an existing Gradle project with the IDE and managing it.

Concepts

Gradle Projects and IntelliJ Projects

Multiple Gradle projects can exists inside an IntelliJ Project, but the usual setup is to have one IntelliJ Project per Gradle project. The relationship between an IntelliJ Project and a Gradle project is established when the Gradle project is opened from the IDE, by opening the directory containing the Gradle project (build.gradle or settings.gradle). Also see Link a Gradle Project to an IntelliJ Project below.

Gradle JVM

https://www.jetbrains.com/help/idea/gradle.html#gradle_jvm

IDE determines the Gradle JVM according to the following algorithm, evaluating in order:

  • When the project is opened, check gradle.properties in the root of the project for org.gradle.java.home system property and use the value if found.
  • Use the value specified in Gradle Settings → Gradle JVM. By default, that value is Project SDK. The "Gradle Settings" UI gets that information from the project's .idea/gradle.xml. If no "gradleJvm" option is present, the Project SDK is assumed.
<project version="4">
  <component name="GradleSettings">
    <option name="linkedExternalProjectsSettings">
      <GradleProjectSettings>
        <option name="gradleJvm" value="1.8 (2)"/>
      </GradleProjectSettings>
    </option>
  </component>
</project>

Language Level

The language level settings are applied for a source root or for a module. For a module, the module language level is set to sourceCompatibility in the build.gradle file. If a IDE Gradle project has a single linked Gradle project then the project default language level is set to the minimum language level among the module language levels. 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.

Gradle Distribution

The distribution (version) of Gradle to be used for a project can be configured in Gradle Settings by selecting one of the following sources:

If asked to specify Gradle home, and get "Gradle location is incorrect", use the "libexec" subdirectory (/usr/local/Cellar/gradle/4.9/libexec).

JVM Options for Gradle

https://www.jetbrains.com/help/idea/gradle.html#gradle_vm_options

JVM options can be added to the Gradle project by specifying them in the gradle.properties file.

Build Delegation

IntelliJ can be configured to execute the build, even if it uses Gradle dependency and other metadata, or it can delegate the build to Gradle. This can be set from Gradle Settings "Build and run using" option. If "IntelliJ IDEA" is chosen, IntelliJ will execute the build. If "Gradle (Default)" is chosen, Gradle will execute the build. If IntelliJ is used to build, it'll do an incremental build. The setting corresponds to the delegatedBuild configuration option in $PROJECT/.idea/gradle.xml. For "IntelliJ IDEA" builds, delegatedBuild is set to "false". For "Gradle" builds, delegatedBuild is set to "true".

Also see:

Compile Server

Test Execution Delegation

IntelliJ can be configured to execute the tests itself, delegate to Gradle, or choose the mode on a test-by-test basis. This can be set from Gradle Settings "Run tests using" option. If "IntelliJ IDEA" is chosen, IntelliJ will execute the tests. If "Gradle (Default)" is chosen, Gradle will execute the tests. The setting corresponds to the testRunner configuration option in $PROJECT/.idea/gradle.xml. For "IntelliJ IDEA" test execution, testRunner is set to "PLATFORM". For "Gradle" builds, testRunner is set to "GRADLE". For a test-by-test basis test execution, testRunner is set to "CHOOSE_PER_TEST"

Gradle Settings

https://www.jetbrains.com/help/idea/gradle-settings.html

Gradle user home

This fields specified the location of stored Gradle caches, downloaded files, etc. If Gradle location has been defined by the environment variables GRADLE_HOME or PATH, IntelliJ deduces this location, and suggests this path as the default value. If not, the path must be specified manually.

Generate *.iml files for modules imported from Gradle

Usually disabled.

Gradle projects

Download external annotations for dependencies
Build and run

See Build Delegation and Test Execution Delegation above.

Gradle JVM

See Gradle JVM above.

Use Gradle from

See Gradle Distribution above.

Operations

Create a New Gradle Project from the IDE

https://www.jetbrains.com/help/idea/gradle.html#project_create_gradle

Open an Existing Gradle Project

https://www.jetbrains.com/help/idea/gradle.html#gradle_import_project_start

Link a Gradle Project to an IntelliJ Project

https://www.jetbrains.com/help/idea/gradle.html#link_gradle_project

Add a New Gradle Module to an Existing Gradle Project

https://www.jetbrains.com/help/idea/gradle.html#gradle_add_module

IDEA Gradle Plugin

IDEA Plugin

Configuring Gradle Sources

TODO: Gradle Operations | IDE Configuration

Importing a Project form a Gradle Model

Use auto-import - If selected, re-import of the project is done automatically every time you make changes to the project.

Group modules. See Grouping Modules.

Troubleshooting

SNAPSHOT Refresh Problem

There are cases when Gradle pulls the latest version of the SNAPSHOT and caches it in its dependency cache, but IntelliJ IDEA Gradle plugin does not seem to notice it and uses an old version of the snapshot. This is troublesome during debugging, when the source code as displayed by IntelliJ does not match the bytecode being debugged.

Possible solutions for this (have not tested any of them, do that next time when I need it):

1. Refresh Dependencies of the Gradle Module

Gradle tab -> Select the module -> Right click -> Refresh Dependencies

2. Remove the Problem Dependency

Project Tab -> External Libraries -> expand Gradle: library-name -> Select "library root" -> Delete.

Then go to the Gradle tab and refresh dependencies.