Introductionโ
Both then and now, many developers in the Bukkit ecosystem use Maven. There are also some developers which are not using a real build tool at all. Please don't use the Eclipse Build Path or IntelliJ Artifacts for dependencies.
So whats wrong with Maven you may now ask?
Don't get me wrong, Maven is a great tool. But for the times we live in, it's not up to date. I don't think it's good to have to write 75 lines of xml for a simple plugin. If you want to do more complex tasks, it gets damn complicated. Gradle is not only faster than maven. Since the paperweight userdev plugin is available, there is a nice way to use NMS code with Mojang Mappings without having to deal with obfuscated code. Even if you don't want to use NMS Code, Gradle has it's advantages.
For Example, you don't write your build logic in XML, but in one (or more) Groovy or Kotlin scripts. With the Kotlin variant, your IDE is even able to provide type checking and auto complete for it. Here is an example of the maven configuration mentioned earlier. Just 25 lines of kotlin code.
IDE Supportโ
IntelliJ IDEAโ
IntelliJ IDEA has great support for Gradle. I'll use IntelliJ IDEA Ultimate in this tutorial, but the community edition has all features we need for this tutorial.
Eclipseโ
Eclipse supports some Gradle features via Eclipse Buildship, but it's not really great. Also eclipse provides no support for the Gradle Kotlin DSL, which we are going to use. I would recommend you to use IntelliJ IDEA instead. There is a migration guide for eclipse users available here.
Getting Startedโ
To create our project, start up IntelliJ IDEA and click on Create Project in the main menu or select File -> New Project if you already have a project opened. Select Gradle in the sidebar on the left. Select a JDK with version 17 in the selection for Project SDK. You can download a JDK via the selection box if you don't have one installed. I recommend to use Eclipse Temurin if you don't have one. Also, make sure to check the Kotlin DSL build script checkbox. Only select Java under Additional Libraries and Frameworks.
Now, click Next.
In the next screen, you can choose the project name and the path on your system. Click on Artifact Coordinates to set your projects Maven Coordinates. (yes gradle uses maven's dependency system).
Click on Finish to create your project and wait until IntelliJ has loaded and indexed your project.
Updating Gradleโ
Because IntelliJ uses the Gradle Tooling API to manage Gradle Projects, the version which IntelliJ uses is often a bit outdated. We'll now upgrade our project to use the latest and greatest Gradle version.
You may have seen that there are several files in your project folder like gradlew, gradlew.bat and a folder structure with a gradle-wrapper.jar and gradle-wrapper.properties. This is because gradle with its wrapper makes sure that the gradle version can be set for each project. Because of that you don't need to install gradle on your system. Everything is handled by the Gradle Wrapper.
As I write this, the latest Gradle version is 7.3.3, but please check here if a newer one is available. To upgrade your gradle version, run the following command in your project. We also want to use the all distribution type which includes sources and docs, because otherwise IntelliJ would download them as well.
- Linux/macOS
- Windows (cmd)
- Windows (PowerShell)
./gradlew wrapper --gradle-version 7.3.3 --distribution-type all
gradlew wrapper --gradle-version 7.3.3 --distribution-type all
.\gradlew wrapper --gradle-version 7.3.3 --distribution-type all
Adding Paper to your projectโ
So now we are using the latest version of gradle. Let's remove some stuff from our build script. IntelliJ adds some defaults which we don't need.
Open the build.gradle.kts and adjust it like this.
plugins {
java
}
// Use your group id here of course
group = "dev.nycode"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
}
Now we have an empty Gradle project. Let's add Paper to the build.
plugins {
java
id("io.papermc.paperweight.userdev") version "1.3.3"
}
Let's also set our Java Language Version to 17.
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
}
We also have to add the PaperMC Maven repository to our gradle plugin sources to be able to resolve the plugin.
Adjust your settings.gradle.kts as the following:
// Use your project name here of course
rootProject.name = "paper-blog-post"
pluginManagement {
repositories {
gradlePluginPortal()
maven("https://papermc.io/repo/repository/maven-public/")
}
}
Now we can add a dependency on the paper dev bundle.
dependencies {
paperDevBundle("1.18.1-R0.1-SNAPSHOT")
}
Let's make sure the reobfJar task is executed when we build our plugin.
tasks {
assemble {
dependsOn(reobfJar)
}
}
Our build.gradle.kts now should like the following:
plugins {
java
id("io.papermc.paperweight.userdev") version "1.3.3"
}
group = "dev.nycode"
version = "1.0-SNAPSHOT"
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
}
repositories {
mavenCentral()
}
dependencies {
paperDevBundle("1.18.1-R0.1-SNAPSHOT")
}
tasks {
assemble {
dependsOn(reobfJar)
}
}
Now sync your changes in IntelliJ and wait until IntelliJ re-synced the project. All errors should be gone after that. Now you can freely work with mojang mapped NMS and deobfuscated names.
You can try out the example shown in Paper's paperweight test repository.
To build your plugin just execute Gradle's build task.
- Linux/macOS
- Windows (cmd)
- Windows (PowerShell)
./gradlew build
gradlew build
.\gradlew build
Your reobfuscated plugin jar will be in build/libs/<project-name>-<version>.jar There will be also a jar with a -dev suffix. This file won't run on normal servers, because it's using the deobfuscated mojang mappings.