Radek Postołowicz
Radek Postołowicz

Reputation: 4774

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?

Upvotes: 6

Views: 1537

Answers (3)

crusy
crusy

Reputation: 1512

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.

Upvotes: 3

Andrey
Andrey

Reputation: 16391

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

enter image description here

Upvotes: 1

Related Questions