Applying & Publishing Plugins
Learn how to apply custom plugins to projects and publish them to a local or remote repository.
Applying & Publishing Plugins is a free Groovy & Gradle: JVM Automation and Build Engineering lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Groovy & Gradle: JVM Automation and Build Engineering learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Reusing Your Gradle Plugins
So you've learned to create custom Gradle plugins. Great! But how do you actually use them in different projects, and how can you share them with others?
This lesson explores how to apply your custom plugins to a Gradle project and how to publish them to a local or remote repository for wider use.
Plugin Application Review
Before diving into custom plugins, let's quickly recall how you apply standard plugins.
- Most plugins are applied using the
plugins { id 'plugin-id' }block in yourbuild.gradlefile. - This method is preferred for its declarative nature and better tooling support.
- It works for plugins from Gradle Plugin Portal, local
buildSrcplugins, and published plugins.
Applying Script Plugins
You can define build logic directly within your build.gradle or include it from another script. This is the simplest form of reusing logic.
Below, a custom task myCustomTask is defined. It's available to your build script just like any other task.
task myCustomTask {
doLast {
println "Hello from my custom task!"
}
}
task executeMyTask {
dependsOn myCustomTask
doLast {
println "Main build script finished after custom task."
}
}
// To run: gradle executeMyTaskPlugins from buildSrc
For more complex, project-specific plugins, Gradle automatically compiles and makes available any plugins defined in the buildSrc directory.
If you create a plugin in buildSrc/src/main/groovy/com/example/MyPlugin.groovy, it can be applied directly by its ID:
- Define plugin:
buildSrc/src/main/groovy/my.project.plugin.gradle - Apply plugin:
plugins { id 'my.project.plugin' }
Gradle ensures buildSrc is built before your main project, making its plugins ready for use.
Why Publish Plugins?
While buildSrc is great for single-project or monorepo plugins, what if you want to share your plugin across completely separate projects, or even with the community?
This is where publishing comes in. Publishing makes your plugin available in a repository, just like any other library you use (e.g., from Maven Central).
- Local: Share within your machine for personal use.
- Remote: Share within your organization (Artifactory, Nexus) or globally (Gradle Plugin Portal, Maven Central).
Publishing with maven-publish
Gradle's maven-publish plugin is the standard way to publish your plugin artifacts to Maven-compatible repositories.
By applying this plugin, you gain access to a publishing block in your build.gradle, where you'll define the details of what gets published and how.
It also introduces tasks like publishToMavenLocal and publish.
plugins {
id 'java-library' // Or 'groovy' for Groovy plugins
id 'maven-publish'
}
group = 'com.coddykit.example'
version = '1.0.0'
// Applying maven-publish plugin
// This is part of setting up for publication.
// No direct output from this snippet alone.Defining Publication Details
Inside the publishing block, you configure the details of your artifact: its name, what components to include (like JARs), and where it should go.
A "publication" defines a set of artifacts and their metadata (group, artifact ID, version).
plugins {
id 'java-library'
id 'maven-publish'
}
group = 'com.coddykit.example'
version = '1.0.0'
publishing {
publications {
maven(MavenPublication) {
// The artifact ID is usually derived from project name,
// but can be explicitly set.
artifactId 'my-groovy-plugin'
from components.java // Publish the Java library component
}
}
}
// This defines how the plugin will be published.
// No direct output from this snippet alone.Publishing to Maven Local
After configuring your publication, you can publish your plugin to your local Maven repository. This is great for local testing and development.
Gradle provides a task for each publication you define (e.g., publishMavenPublicationToMavenLocal if your publication is named maven).
plugins {
id 'java-library'
id 'maven-publish'
}
group = 'com.coddykit.example'
version = '1.0.0'
publishing {
publications {
maven(MavenPublication) { // 'maven' is the publication name
artifactId 'my-groovy-plugin'
from components.java
}
}
repositories {
mavenLocal() // Explicitly declare mavenLocal for publishing
}
}
// To publish: gradle publishMavenPublicationToMavenLocal
// This command will build your plugin and place its artifacts
// into your local Maven cache (~/.m2/repository).Applying a Published Plugin
To use a plugin you've published to mavenLocal() in another project, you need two things:
- Declare
mavenLocal(): AddmavenLocal()to thepluginManagement.repositoriesblock in your consuming project'ssettings.gradle. - Apply by ID and Version: Use the
plugins { id '...' version '...' }block in your consuming project'sbuild.gradle.
This tells Gradle to look for the plugin in your local Maven repository.
// settings.gradle (consuming project)
pluginManagement {
repositories {
mavenLocal()
gradlePluginPortal()
}
}
// build.gradle (consuming project)
plugins {
id 'com.coddykit.example.my-groovy-plugin' version '1.0.0'
// Replace 'com.coddykit.example.my-groovy-plugin' with your plugin's actual ID
}
// This code snippet shows how to apply a published plugin.
// It assumes the plugin has been published and is available locally.Publishing Remotely
To share your plugin beyond your local machine, you publish it to a remote repository. This could be:
- Company Artifactory/Nexus: For internal team use.
- Gradle Plugin Portal: For public plugins, requiring the
com.gradle.plugin-publishplugin. - Maven Central: The largest public repository.
The configuration in the publishing.repositories block specifies the URL and credentials for these remote destinations.
plugins {
id 'maven-publish'
// id 'com.gradle.plugin-publish' // For Gradle Plugin Portal
}
publishing {
publications {
maven(MavenPublication) { /* ... */ }
}
repositories {
maven {
name = "myCompanyRepo"
url = uri("https://your.company.repo/releases")
credentials {
username = project.properties.get("repoUsername")
password = project.properties.get("repoPassword")
}
}
}
}
// This snippet shows how to define a remote repository.
// Credentials should be managed securely, not hardcoded.Plugin Application Check
Time to test your understanding of applying and publishing plugins.
Recap: Apply & Publish
You've learned how to make your custom Gradle plugins truly reusable!
- You can apply simple script plugins using
apply from:. - Plugins in
buildSrcare automatically available by ID. - The
maven-publishplugin allows you to define and publish your plugin artifacts. - Publishing to
mavenLocal()is great for local testing. - To use a published plugin, configure
mavenLocal()insettings.gradleand apply it by ID and version. - Remote repositories enable sharing plugins across teams or publicly.
Now you can build powerful, shareable build logic!
Frequently asked questions
Is the “Applying & Publishing Plugins” lesson free?
Yes — the full text of “Applying & Publishing Plugins” is free to read here on the web, and the Groovy & Gradle: JVM Automation and Build Engineering course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Groovy & Gradle: JVM Automation and Build Engineering course, upgrade to CoddyKit PRO.
What will I learn in “Applying & Publishing Plugins”?
Learn how to apply custom plugins to projects and publish them to a local or remote repository. You practise Groovy & Gradle: JVM Automation and Build Engineering with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Groovy & Gradle: JVM Automation and Build Engineering?
No prior experience is required. Groovy & Gradle: JVM Automation and Build Engineering on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Applying & Publishing Plugins” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Groovy & Gradle: JVM Automation and Build Engineering lesson?
Yes. Every Groovy & Gradle: JVM Automation and Build Engineering lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Understanding Gradle Plugins
- Building Binary Plugins
- Applying & Publishing Plugins
- Plugin Extensions & Configurable DSLs