4
votes

What is the exact dependency I need to develop a Gradle Plugin in Java? Ideally I would like to get it from a well-known repository such as Maven Central or similar.

I have a Maven project with a core functionality and I just added two extra plugins, one for Ant, one for Maven. They are already tested and working; easy! Now, I wanted to add a third module for a Gradle plugin to make this functionality also available from any Gradle project.

However, I can't find the exact dependencies I need to develop a Gradle plugin.

The Gradle docs (such as https://docs.gradle.org/current/userguide/java_gradle_plugin.html) are not very well written to say the least. They mention:

  • the gradleAPI() dependency
  • or the java-gradle-plugin dependency

But they are quite unclear... no group, no version (really?).

If anyone can enlighten me to where I can get these dependencies from, I would be very thankful.

2
the top voted ans here possibly address your question stackoverflow.com/questions/33906081/…Nigel Savage
@NigelSavage Thanks for the link. According to the discussions there it seems that Gradle is not 100% transparent about its internal dependencies. I guess the other option would be to switch my entire project from Maven to Gradle; but I have already the Ant and Maven plugins modules running and tested. Sigh.Joe DiNottra
with some refactoring should be able to keep the ant plugin, not sure about the maven one though, but I think you would just declare both as class path dependencies in the buildscript(older not plugins) syntax then apply.Nigel Savage

2 Answers

5
votes

Gradle's public and internal APIs, aka gradleApi(), are bundled with the Gradle distribution and not independently published and therefore not easily consumable by Maven builds. There's the pending epic #1156 (Ensure plugin cross-version compatibility by allowing a user to depend on gradlePublicApi()) that might help here.

Since Gradle plugins are best to be built with Gradle, a pragmatic solution is to invoke the Gradle build from Maven and attach the produced artifact to the Maven build. Andres Almiray (aalmiray) once described this in the blog post Running Gradle Inside Maven (Web Archive Link). He describes the following high level steps:

  1. Create a new Maven module (e.g. gradle-plugin) and add attach it to the parent POM
  2. In the POM of gradle-plugin add a dependency to your core module. Use the maven-dependency-plugin to store dependencies to the Maven build folder, e.g. target/dependencies.
  3. Create the build.gradle, add a Maven repository that points to target/dependencies (step 2) and let it depend on the core module as well as gradleApi(). Implement the Gradle plugin.
  4. Use the exec-maven-plugin to invoke the Gradle build.
  5. Use the maven-resources-plugin to copy the Gradle built plugin jars to the standard Maven build folder.
  6. Use the build-helper-maven-plugin to attach the copied jars to the Maven build.

Sample project to be found here (gradle-in-maven).

1
votes

https://docs.gradle.org/current/userguide/custom_plugins.html#sec:custom_plugins_standalone_project

In here it is mentioned that it is gradleApi() and I know that this works (from experience). The localGroovy() on that page is only needed if your plugin code uses groovy (does not apply if you only use groovy in the build.gradle of your plugin).

java-gradle-plugin is a library that makes it a bit simpler to make plugins, it is not required though. I personally prefer using gradleApi only.

EDIT:

It appears I've misunderstood the question. Here are the steps to get gradleApi jar:

  1. Create a Gradle project with your desired Gradle version.
  2. Add implementation gradleApi() dependency.
  3. Import/run the project once.
  4. Go to your .gradle folder (located in home folder in Linux-based operating systems).
  5. Open caches folder
  6. Open the version folder you want, e.g. 6.0.1
  7. Open generated-gradle-jars folder.
  8. Copy the jar to wherever you want and use it.

For me the 6.0.1 jar is at ~/.gradle/caches/6.0.1/generated-gradle-jars/gradle-api-6.0.1.jar

Please note that I have not tested this, I know the jar is there but I haven't tried using it.