How to setup a Gradle plugin project in IntelliJ?

2020-05-23 16:36发布

问题:

I want to create a standalone Gradle plugin project as described in the Gradle documentation. I would like to use IntelliJ with code completion for Groovy and Gradle. Since there is no specialized wizard to create a Gradle plugin project I have to do it manually.
I already managed to add the Groovy SDK (binary) in the IDE via: File / Other Settings / Default Project Structure as shown in the screenshot.

To start with I created a new Gradle project which also contains the Gradle wrapper. I then create a Groovy script named MyExamplePlugin.groovy following the project structure of the sdk-manager-plugin; please note me if this project does not follow the desired setup.

.
├── MyExamplePlugin.iml
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── plugin
│   └── src
│       └── main
│           ├── groovy
│           │   └── com
│           │       └── example
│           │           └── MyExamplePlugin.groovy
│           └── resources
│               └── META-INF
│                   └── gradle-plugins
│                       └── myexample.properties
└── settings.gradle

Then I start implementing the class:

import org.gradle.api.Plugin
import org.gradle.api.Project

class MyExamplePlugin implements Plugin<Project> {
   // ...
}

The problem is that org.gradle.api.* cannot be found.

回答1:

Go to a new, empty folder and type:

gradle init --type groovy-library

Then edit the generated build.gradle file and add:

compile gradleApi()

To the dependencies, and:

apply plugin: 'idea'

To the plugins near the top.

Then run:

./gradlew idea

And open the generated project in IntelliJ