kotlingradlegradle-plugin

How to configure gradle to use private gradle plugins pushed to GitHub package registry or mavenLocal


Plugin repository

build.gradle.kts

plugins {
  kotlin("jvm") version "1.9.22"
  id("me.qoomon.git-versioning") version "6.4.3"
  `maven-publish`
  `kotlin-dsl`
  id("java-gradle-plugin")
  id("com.gradle.plugin-publish") version "1.2.1"
}

group = "io.github.benkeil"

gradlePlugin {
  website = "https://github.com/benkeil/openapi-dsl-gradle"
  vcsUrl = "https://github.com/benkeil/openapi-dsl-gradle.git"
  plugins {
    create("openapiDsl") {
      id = "io.github.benkeil.openapi-dsl-gradle"
      displayName = "OpenAPI Kotlin DSL Gradle Plugin"
      description =
          "Generates special kotlin classes from OpenAPI schemas, that can be used to build custom DSLs"
      tags = listOf("openapi", "dsl", "kotlin")
      implementationClass = "pub.keil.openapi.dsl.plugin.OpenapiDslPlugin"
    }
  }
}

publishing {
  repositories {
    mavenLocal()
    maven {
      name = "GitHubPackages"
      url = uri("https://maven.pkg.github.com/benkeil/${rootProject.name}")
      credentials {
        username = "not_required"
        password = System.getenv("GITHUB_PACKAGES_WRITE_TOKEN")
      }
    }
  }

  publications { register<MavenPublication>("gpr") { from(components["java"]) } }
}

I publish to GitHub (or mavenLocal) and not publishPlugin.

Dependant repository

settings.gradle

rootProject.name = "cdktf-lib"

pluginManagement {
  repositories {
    gradlePluginPortal()
    google()
    maven { url = uri("https://plugins.gradle.org/m2/") }
    mavenLocal()
    mavenCentral()
  }

  resolutionStrategy {
    eachPlugin {
      if (requested.id.namespace == "io.github.benkeil") {
        useModule("${requested.id.namespace}:${requested.id.name}:${requested.version}")
      }
    }
  }
}

build.gradle.kts

repositories {
  mavenLocal()
  mavenCentral()
  maven {
    name = "GitHubPackages"
    url = uri("https://maven.pkg.github.com/benkeil/*")
    credentials {
      username = "not_required"
      password = System.getenv("GITHUB_PACKAGES_READ_TOKEN")
    }
    content {
      includeGroupByRegex("de\\.benkeil.*")
      includeGroupByRegex("pub\\.keil.*")
      includeGroupByRegex("io\\.github\\.benkeil.*")
    }
  }
}

dependencies {
  implementation(kotlin("reflect"))
  implementation(libs.bundles.implementation)
  testImplementation(libs.bundles.testImplementation)
}

When trying to build I get:

> Could not resolve all files for configuration ':classpath'.
   > Could not find org.gradle:gradle-tooling-api:8.9.
     Searched in the following locations:
       - https://plugins.gradle.org/m2/org/gradle/gradle-tooling-api/8.9/gradle-tooling-api-8.9.pom
       - https://dl.google.com/dl/android/maven2/org/gradle/gradle-tooling-api/8.9/gradle-tooling-api-8.9.pom
       - file:/Users/ben/.m2/repository/org/gradle/gradle-tooling-api/8.9/gradle-tooling-api-8.9.pom
       - https://repo.maven.apache.org/maven2/org/gradle/gradle-tooling-api/8.9/gradle-tooling-api-8.9.pom
     Required by:
         project : > io.github.benkeil:openapi-dsl-gradle:main

How to configure gradle to use GitHub for plugins?


Solution

  • Besides that you have a lot of non-sense, redundancy, and bad practices in your files, your problem is not how to configure to use your plugin from there. As you can see in your error message it did look in Maven Local, but it is not your plugin that is not found, but the Gradle Tooling API dependency you declared which is not found in any of the repositories you declared on the consumer build as plugin repositories.

    If your plugin needs dependencies from other repositories, your consumer builds also have to specify that repository - or any other repository that provides the dependency like an in-house corporate mirror - that provides that dependency. Repository declarations are not transitive, not even if they would end up in the POM, which they do not.

    In your specific case, the problem is one layer deeper though. It makes absolutely no sense to depend on the Gradle Tooling API dependency, because the Tooling API is already part of the Gradle distribution. So in a Gradle plugin you can simply use the Gradle Tooling API without declaring any dependency and thus without the need for any other repository. The extra tooling API dependency is only necessary when you build something that is not running in the context of Gradle like an IDE plugin that should communicate with Gradle builds.