Search code examples
kotlingradlegradle-kotlin-dsl

How to apply a gradle plugin from maven


I'm learning using gradle (using kotlin).

I find it is easy to create a gradle plugin from template.

But I am still not able to understand. I have two question:

  1. Did I publish my plugin correctly?
  2. How to apply this plugin?

Here is what I did:

Here is some of history of creation

gradle init
Couldn't get final path for handle 0x11a0, error code: 2

> Task :wrapper
Caught exception: Couldn't resolve final path of, error = 2: Z:\

Select type of project to generate:
  1: basic
  2: application
  3: library
  4: Gradle plugin
Enter selection (default: basic) [1..4] 4

Select implementation language:
  1: Groovy
  2: Java
  3: Kotlin
Enter selection (default: Java) [1..3] 3

Select build script DSL:
  1: Kotlin
  2: Groovy
Enter selection (default: Kotlin) [1..2] 1

Project name (default: asd): asd
Source package (default: asd): com.asd
......
BUILD SUCCESSFUL in 18s

After doing so, the helloworld plugin has been generated

package com.asd
class AsdPlugin: Plugin<Project> {
    override fun apply(project: Project) {
        // Register a task
        project.tasks.register("greeting") { task ->
            task.doLast {
                println("Hello from plugin 'com.asd.greeting'")
            }
        }
    }
}

build.gradle.kts configration:

gradlePlugin {
    // Define the plugin
    val greeting by plugins.creating {
        id = "com.asd.greeting"
        implementationClass = "com.asd.AsdPlugin"
    }
}

So, I added maven-publish to this file. Some part is copied from gradle doc. And publish to my local maven (created by docker) successfully. (I just double click publish in the right docker of IDEA, maybe it is same as ./gradlew publish)

group = "aaa.bbb"
version = "1.4"

publishing {
    publications {
        create<MavenPublication>("myLibrary") {
            from(components["java"])
        }
    }

    repositories {
        maven {
            credentials {
                username = "admin"
                password = "123456"
            }
            url = uri("http://localhost:8081/repository/hellomaven/")
            isAllowInsecureProtocol = true
        }
    }
}

Now how can I apply this plugin in another project?

plugins {
    kotlin("jvm") version "1.9.0"
    
    id("What is the id of the plugin above?")
}

repositories {
    mavenCentral()
    maven {
        credentials {
            username = "admin"
            password = "123456"
        }
        url = uri("http://localhost:8081/repository/hellomaven/")
        isAllowInsecureProtocol = true
    }
}

dependencies {
   implementation("aaa.bbb:plugin:1.4") // should I implement this ?
}

Solution

  • The ID of the plugin is what you specified in gradlePlugin { }. For this case, you specified the ID as com.asd.greeting. You have also defined the plugin version as 1.4. Therefore, this makes your plugin application as:

    // build.gradle.kts
    plugins {
        id("com.asd.greeting") version "1.4"
    }
    

    However, since this is not published to a Maven repository such as the Gradle Plugins Portal or Maven Central, you need to configure your Gradle build to include the location in which you published the plugin. In this case, it appears to be a locally hosted Maven repository http://localhost:8081/repository/hellomaven/. This can be configured in Settings:

    // settings.gradle.kts
    pluginManagement {
        repositories {
            maven {
                url = uri("http://localhost:8081/repository/hellomaven/")
            }
            gradlePluginPortal()
        }
    }
    

    You can see here that I define a Maven repository where the location is the URL in which you published to. I also configure to use the Gradle Plugin Portal as well since Gradle will look at all repositories to find a plugin.