Build you Spigot plugin with Gradle Groovy - Historical

Applied By Conclure: Apr 20, 2021 at 11:53 AM

Build you Spigot plugin with Gradle Groovy
Introduction
When we develop plugins we often aim to make things in the process more efficient. Gradle is a build automation tool aiming to automate and simplify the process of jar production, dependency management and project management. It is similar to Maven in terms of what their goal is however unlike Maven, Gradle can be written both imperatively and declaratively, and is not in XML but instead Groovy (there exists a Kotlin DSL also).

This wiki page is NOT trying to convince you Gradle is better than Maven.

What You Will Need
In this guide we will also be using IntelliJ IDEA (v2021.1). You should also have fundamental knowledge about how to create a Spigot plugin.

Lets Begin
First we will be creating a new project.
[​IMG]

Then choose Gradle and select Java, we will use SDK 11 but you can of course go with another version, then Next!
[​IMG]

Fill in the name, version and group id (we can change it later), then Finish!
[​IMG]

Now wait for everything to load. Once that's done your project should look something like this (click on the image for full quality):
[​IMG]

The main file we will be working with is build.gradle. It similar to pom.xml if you have worked with Maven. You can change the name of the project in settings.gradle which also will require a project reload and can be done by clicking this button (click on the images for full quality):
[​IMG]
[​IMG]

Whenever you are changing something in build.gradle or settings.gradle like the name, group, version or adding dependencies then reload like the images demonstrate.

To add spigot as a dependency put this in the dependencies block in build.gradle (you can of course choose any version):
Code (Text):

compileOnly 'org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT'
 
And this in the repositories block in build.gradle:
Code (Text):

maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
maven { url 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/' }
 
We also recommend adding this block in build.gradle:
Code (Text):

/* Assume you have built a jar from earlier production.
   This will ensure removal of any resources that was cached in any earlier build,
   so that the new jar you produce will generate the newest version of your resources. */
processResources {
    doFirst {
        file("$buildDir/resources/main").listFiles().each {
            it.delete()
        }
    }
}
 
Your build.gradle should now look something like this:
Code (Text):

plugins {
    id 'java'
}

group 'spigot.gradle.project'
version '1.0.0'

repositories {
    mavenCentral()
    maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
    maven { url 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/' }
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
    compileOnly 'org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT'
}

test {
    useJUnitPlatform()
}

processResources {
    doFirst {
        file("$buildDir/resources/main").listFiles().each {
            it.delete()
        }
    }
}
 
Now reload the project! Once your project is done reloading you may start writing your code and remember plugin.yml! With Gradle we usually have our classes in ./src/main/java/ and our resources in ./src/main/resources/ (click on the image for full quality).
[​IMG]

Once you are done coding and want to test your masterpiece, run the jar task which can be found here (click on the image for full quality):
[​IMG]

Then once that is done your jar will be ready in the folder ./build/libs/. (click on the image for full quality)
[​IMG]

Congratulations! You have now successfully created a plugin using Gradle.