Javascript required
Skip to content Skip to sidebar Skip to footer

Re-import a Gradle Project and Try Again

Gradle projects

IntelliJ Idea lets you manage Gradle projects. You can link, ignore projects, work with profiles, and synchronize changes in Gradle and IntelliJ IDEA projects. You can as well configure a Gradle composite build, Gradle source sets, the build and run actions.

  1. In the Gradle tool window, correct-click a linked project.

  2. From the context bill of fare, select Open Gradle config F4.

    IntelliJ IDEA navigates to the appropriate Gradle configuration file and the related build.gradle file opens in the editor.

IntelliJ Thought supports a navigation to subprojects inside the parent build script of a multi-module Gradle project.

  1. Open build.gradle of the parent project.

  2. Use the Ctrl+Click shortcut to navigate to a subproject.

    Navigate inside the build script

    You tin besides come across the usages of a subprojects with the Alt+F7 and check the results in the Find tool window.

    the Find tool window

Unlink a linked Gradle project

When you unlink a Gradle project, IntelliJ Thought removes all relevant modules and content roots, removes the Gradle project from the Gradle tool window and stops its synchronization. Information technology might be helpful if you need to fully remove the previously linked Gradle projection from the current IntelliJ Idea projection.

  1. In the Gradle tool window, right-click a linked projection.

  2. From the context card, select Unlink Gradle projection (Delete). Alternatively, you tin can select the linked project and click the Remove icon on the tool window's toolbar.

  3. In the Import Gradle Projects popup, clear the checkbox against the modules if you don't want to delete the project from the IntelliJ Thought Project tool window.

  4. Click OK.

    If you lot need to link back the project, in the Project tool window, correct-click the project'south build.gradle file or build.gradle.kts if it is a Kotlin project, and select Import Gradle Project.

Ignore a Gradle project

You can de-activate a Gradle project using the Ignore Gradle Projection selection. In this example, IntelliJ Thought keeps the ignored Gradle projects and subprojects in the Gradle tool window, but stops their import (modules, content roots, tasks, and so on) to the project. However, IntelliJ Idea synchronizes the ignored projects with the electric current one. Information technology might be helpful if you need to skip an irrelevant subproject such as buildSrc.

  1. In the Gradle tool window, right-click the project that you lot want to ignore.

  2. From the context card, select Ignore Gradle Project.

  3. In the window that opens, select projects and modules that you want to de-activate and click OK.

If you want to activate your Gradle projects or modules, select Unignore Gradle Projects from the context carte du jour.

Orphan modules

Orphan modules are the IDE modules that were removed during the import process in the following cases:

  • when you manually deleted the modules in the build.gradle file and and then re-imported your projection.

  • when you used the Ignore Project action, on a module in the Gradle tool window and and so re-imported your project.

In all these cases, IntelliJ IDEA prompts you lot to restore removed modules.

Orphan modules notification

You lot can select the ones y'all want to restore in the Orphan Modules dialog.

Orphan modules dialog

Usually you do non need to restore any modules since these are simply the .iml files that contain the IDE modules settings. You just might want to restore them if you have some user-defined settings specified inside them.

Reload a linked Gradle project

When you open a Gradle project the synchronization is washed automatically. Also, when IntelliJ IDEA detects whatever external changes to the build scripts, such as VCS updates or some edits fabricated outside of the IDE, the related projects volition be reloaded automatically.

If y'all demand, yous tin manually trigger the synchronization of your project.

  1. In the Gradle tool window, right-click a linked project.

  2. From the context menu, select Reload Gradle projection the Reload icon.

    On invoking this activeness, IntelliJ IDEA parses the project structure in the Gradle tool window.

    IntelliJ IDEA cannot reload only a office of your project, information technology reloads the whole project including modules and dependencies.

    If you configure a dependency through the Project Structure dialog (click the Project Structure button, from the main carte), the dependency will but appear in the IntelliJ IDEA Projection tool window, not in the Gradle tool window. Annotation that the next time you re-import your project, IntelliJ IDEA will remove the added dependency since IntelliJ Thought considers the Gradle configuration equally a single source of truth.

  3. Click the Build icon on the status bar to view the results of the sync in the Build tool window.

Configure the auto-reload

  1. In the Settings/Preferences dialog (Ctrl+Alt+South), go to .

    Alternatively, In the Gradle tool window, click Build Tools Settings and select the Motorcar-Reload Settings option.

    the Gradle tool window
  2. In the Build tools settings, specify the following options:

    the Build Tools settings
    • Reload changes in the build scripts: this pick is selected past default. If you lot want to disable the machine-reload and manually control the reloading process, unselect this checkbox.

    • Any changes: select this option if y'all desire to automatically reload the projection after any changes you make to build.gradle or external changes.

      Every fourth dimension you lot manually change the Gradle build script in the editor, you need to load the changes. IntelliJ Thought displays a notification icon in the right part of the editor suggesting to Load Gradle Changes made to the project (Ctrl+Shift+O).

      Editor: build.gradle

      With the Whatsoever changes option, IntelliJ Idea reloads all the changes automatically.

    • External chages: when you select this option, IntelliJ Thought automatically reloads the project only after the VCS changes and changes made to the build files exterior the IDE.

Configure Gradle Composite Build

Before you first configuring your blended build, make sure yous have the Gradle version 3.1 or later configured for your projection.

You tin can utilize the settings.gradle file to include Gradle builds for your Gradle composite build.

  1. Open up the settings.gradle file in the editor.

  2. Using the includeBuild command, specify the location of the builds you want to add as dependencies to your project.

You can also apply the Gradle tool window to configure the blended build.

  1. Open a Gradle projection.

  2. Link other Gradle projects that y'all want to utilize for the composite build.

  3. In the Gradle tool window, right-click your chief projection and from the context menu select Composite Build Configuration.

  4. In the Gradle Project Build Composite dialog, select projects that you lot want to include in your Gradle composite build.

  5. Re-import your master Gradle project.

    IntelliJ IDEA finds the included Gradle projects and treats them equally IntelliJ IDEA modules.

Apply Gradle source sets

IntelliJ Idea lets y'all employ Gradle source sets in resolving Gradle projects. The source fix is treated as a module in an IntelliJ IDEA projection. You can declare a custom source set and IntelliJ Idea adds it as a module to the project.

When you lot create a Gradle project, IntelliJ Idea automatically creates a master Source Sets directory that contains ii source sets - main and exam. IntelliJ IDEA also displays compile and runtime configurations in the Dependencies node in the Gradle tool window.

Gradle tool window: dependencies

Add a custom source ready

  1. Open the gradle.build file in the editor.

  2. Declare a custom source set (In our case, it's api).

    sourceSets { api } dependencies { compile sourceSets.api.output }

    (This source ready contains interfaces without implementations. The implementations for the interfaces are in the default main source set up.)

  3. Open the Gradle tool window to run into that IntelliJ Idea added the api compile and runtime configurations.

    Gradle tool window: api Source Set

    The test source set contains the appropriate dependencies. Note that the default principal source fix has the compile dependency on the output of the api source set.

  4. From the main menu, select File | Projection Structure Ctrl+Alt+Shift+S to open up the project construction. Find that all source sets are represented every bit carve up modules that are grouped into a single module. If you click the examination module and select the Dependencies tab, you lot will see a listing of dependencies for the source prepare.

    Project Structure dialog / Dependencies tab

Utilize source sets for custom tests

You can add custom tests and run them separately from the main ones using a source set feature.

  1. Declare a source set up the aforementioned mode as you would declare the custom source set. Besides the proper name of y'all source fix, specify an output directory and a task that will run the alleged test. For example, declare an integration test integrationTest.

    sourceSets { integrationTest { coffee { srcDir 'src/integrationtest/java' } resources { srcDir 'src/integrationtest/resources' } compileClasspath += sourceSets.main.runtimeClasspath } } job integrationTest(type: Test) { description = "Runs Integration Tests" testClassesDirs = sourceSets.integrationTest.output.classesDirs classpath += sourceSets.integrationTest.runtimeClasspath }

  2. In the Gradle tool window, click .

  3. In the list that opens, double-click the integrationTest to run it.

    Gradle tool window: tasks

Add package prefixes in the Gradle project

If you use package prefixes in your Gradle project, specify them in the build.gradle file. That fashion, everything is saved when you reimport your project.

For more data, meet https://github.com/JetBrains/gradle-thought-ext-plugin.

  1. Open the build.gradle file.

  2. Add the following plugin to support package prefixes:

    plugins { id "org.jetbrains.gradle.plugin.idea-ext" version "0.5" }

  3. Add together the bundle prefixes. For example, you have the following set of source sets:

    sourceSets { principal.java.srcDirs = [] main.coffee.srcDirs += "src" chief.java.srcDirs += "src/principal/java" main.java.srcDirs += "../other-root/src/chief/coffee" }

    Add together the package prefixes (in our example it is "org.example") to them with the following lawmaking:

    idea { module { settings { packagePrefix["src"] = "org.example" packagePrefix["src/primary/java"] = "org.example" packagePrefix["../other-root/src/chief/java"] = "org.example" } } }

  4. Reimport your changes or use the auto-import.

Specify IDE-specific settings in the build.gradle file

Using the gradle-thought-ext plugin, you tin can depict project settings such as project encodings, and the encoding for properties files inside the build.gradle file.

  1. Open up the build.gradle file.

  2. Add the post-obit plugin to support the encodings configuration:

    plugins { id "org.jetbrains.gradle.plugin.idea-ext" version "0.5" }

  3. Depict the project encodings with the following code:

    import org.jetbrains.gradle.ext.EncodingConfiguration.BomPolicy idea { projection { settings { encodings { encoding = 'windows-1251' bomPolicy = BomPolicy.WITH_NO_BOM backdrop { encoding = '<Arrangement Default>' transparentNativeToAsciiConversion = fake } mapping['../sample-gradle-free/module'] = 'windows-1251' mapping['module'] = 'windows-1251' mapping['module2/src/main/java'] = 'windows-1251' } } } }

  4. Reimport your changes or employ the auto-import.

Use buildSrc

If you have a large Gradle script that includes several Java, Bully, or Kotlin classes, you tin can move such classes to the buildSrc directory and refer to them from your main Gradle script. In this case yous ensure the readability of the build.gradle file.

  1. If you don't have and existing buildSrc, add information technology as a Gradle module to your main projection.

    Project tool window: buildSrc module
  2. Open your main build.gradle file in the editor and motion the classes y'all need to the main subdirectory of the buildSrc directory.

    Project tool window: buildSrc
  3. Run your task from your projection'south build.gradle file.

    Gradle main build file

Configure the build and run actions

By default, IntelliJ Thought uses Gradle for building and running projects.

When you build a projection (), IntelliJ Thought invokes the corresponding tasks using Gradle. Gradle besides executes the Run and Debug deportment from the menu. HotSwap is also gets triggered and the classes are reloaded during a debugging process.

If you have linked projects, you tin configure how to build each linked project.

  1. In the Settings/Preferences dialog (Ctrl+Alt+S), go to .

  2. On the Gradle settings folio, in the Gradle Projects section, select a Gradle project you need.

  3. In the Build and run using list, select the advisable option and click OK to salvage the changes.

If you want to use IntelliJ Thought for edifice a Gradle project, you lot need to explicitly specify and then.

Delegate a build to IntelliJ IDEA

It might be helpful to use IntelliJ IDEA for edifice a pure Java or Kotlin project. It could speed up the edifice process since IntelliJ Thought supports the incremental build. Yet, keep in mind that the IntelliJ Idea compiler does non support some parts of the Gradle project build processing and might cause problems in building your projection correctly.

  1. Click Gradle settings in the Gradle tool window.

    Alternatively, in the Settings/Preferences dialog (Ctrl+Alt+S), go to .

  2. On the Gradle page, from the Build and run using list, select Intellij IDEA.

    the Gradle settings

    Note that the Run test using option stays active, and you can select how you want to run your tests even if you delegated all build and run actions to IntelliJ Thought.

  3. Click OK.

At present if you build your Gradle project, it will be built with IntelliJ Thought.

Last modified: 20 October 2021

mclarenporn1972.blogspot.com

Source: https://www.jetbrains.com/help/idea/work-with-gradle-projects.html