Plugin - How can I run a task before Gradle project is linked?
Answered
- Our company works in Gradle git repositories that don't have a gradle or gradlew file on a fresh checkout.
- Users are required to run an initialization script to pull a gradle wrapper and gradle-wrapper.properties file from a remote host before opening IDEA
- IDEA works with previously initialized wrapper, but fails if users forget to run the script as IDEA will use a wrapper from services.gradle.org that doesn't contain information the repository needs
In a plugin, I would like to make this a seamless process for users by running the initialization script that sets up gradlew before Gradle will attempt to initialize the module and link it to the project. I need to do the following:
- Run script just before Gradle links module to project
- Run script on project open for Gradle projects already linked
I feel I'm on the right track but can't really find the right way to go about it. I've looked at:
ProjectOpenProcessor, GradleTaskManagerExtension, GradleProjectResolverExtension
Please sign in to leave a comment.
Please take a look at ExternalSystemExecutionAware.prepareExecution
Parameter `task: ExternalSystemTask` allows you to check if it is a reload (sync) or just Gradle task launch.
Parameter `taskNotificationListener: ExternalSystemTaskNotificationListener` allows to send events (text) to sync/build output toolwindow
This was exactly what we needed. Thanks Nikita!
We also added a quickfix option to detect invalid state of the Gradle project
All of this together looked something like this:
We also added a quickfix action to detect issues with Gradle sync
plugin.xml
References:
https://github.com/JetBrains/intellij-community/blob/9bbb7c809fda2bbeadf229a69b5ee145f6fb7ae7/plugins/gradle/src/org/jetbrains/plugins/gradle/service/execution/GradleExecutionAware.kt
https://github.com/JetBrains/intellij-community/blob/idea/223.8617.56/plugins/gradle/src/org/jetbrains/plugins/gradle/issue/GradleIssueChecker.kt
Thank you for the followup!
Londonchaim Nikita Skvortsov I am trying to achieve something similar and seeing the following behavior using the code above for MyGradleExecutionAware - execution aware gets triggered during Gradle Sync but does not when a gradle task is invoked through IDE Run button. Is that expected or should this work for both Sync/Builds through IDE?
sshah that is expected to work on both, Sync and Run tasks (Builds) through IDE.
There was a slight problem with starting tasks using double-click in the Gradle toolwindow: IDEA-298823 . Does it look like your case?
That is not the case for me. I am starting task with single-click on Run button in IDE. I was able to achieve my use case using GradleBuildListener. Still not sure why GradleExecutionAware worked for Sync but not Run.