Skip to main content

Make the wrapper the default in IntelliJ when generating the project files with Gradle

When generating the project files for IntelliJ with Gradle (e.g. gradle idea), the default makes IntelliJ ask about an existing Gradle project and defaults to the local Gradle version. Since the project is generated already via Gradle the point is moot and defaulting to the wrapper is the most sensible default.

Gradle allows manipulation of the generated files of the idea plug-in. It's easy to compare the XML file <project>.ipr before and after the changes are made from IntelliJ. Extract the XML fragment and store it (e.g. in gradle/idea/gradleSettings.xml):

<component name="GradleSettings">
        <option name="linkedExternalProjectsSettings">
                <GradleProjectSettings>
                        <option name="distributionType" value="WRAPPED" />
                        <option name="externalProjectPath" value="$PROJECT_DIR$" />
                        <option name="modules">
                                <set>
                                        <option value="$PROJECT_DIR$" />
                                </set>
                        </option>
                </GradleProjectSettings>
        </option>
</component>

Add in your build.gradle the code to append this chunk:

idea {
        project.ipr {
                withXml { provider ->
                        // set local wrapper via gradle settings
                        provider.node.append(new XmlParser().parse(file("gradle/idea/gradleSettings.xml")))
                }
        }
}