Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute gradle task during project import in Intellij Idea

Let's assume my build.gradle file contains task generateSources which as name suggests generates additional java files. It's easy to ensure that generateSources is executed before compileJava: compileJava.dependsOn generateSources. How can I make sure generateSources is called when importing project into Intellij Idea as well?

like image 884
Radek Postołowicz Avatar asked Aug 03 '18 13:08

Radek Postołowicz


3 Answers

To elaborate on @vladimir-sitnikov's answer: I added the idea-ext-plugin to my root project:

apply plugin: 'org.jetbrains.gradle.plugin.idea-ext'

// ...

buildscript {
  dependencies {
    classpath "org.jetbrains.gradle.plugin.idea-ext:org.jetbrains.gradle.plugin.idea-ext.gradle.plugin:0.7"
  }
}

Because without that I wasn't able to use it in my sub project, but now it works like this:

idea.project.settings.taskTriggers {
  beforeSync tasks.getByName("generateSources")
}

Adding the plugin to the sub-project only didn't do it.

Note: The plugin's documentation is kind of limited, but in "DSL spec v. 0.2" is stated

  • beforeSync - before each Gradle project sync. Will NOT be executed on initial import

Didn't try that, but it works with existing projects.

like image 136
crusy Avatar answered Oct 15 '22 19:10

crusy


This can be done via id("org.jetbrains.gradle.plugin.idea-ext") plugin (https://github.com/JetBrains/gradle-idea-ext-plugin).

See sample code in Gradle sources: https://github.com/gradle/gradle/blob/135fb4751faf2736c231636e8a2a92d47706a3b9/buildSrc/subprojects/ide/src/main/kotlin/org/gradle/gradlebuild/ide/IdePlugin.kt#L147

like image 22
Vladimir Sitnikov Avatar answered Oct 15 '22 19:10

Vladimir Sitnikov


You can set the task in Gradle tool window: Execute Before Sync:

enter image description here

like image 27
Andrey Avatar answered Oct 15 '22 20:10

Andrey