Skip to content

Dependency management

srnyx edited this page Jun 25, 2024 · 5 revisions

Similar to repository management, there are a few built-in dependencies you can use. However, it only includes Spigot-API, Spigot-NMS, Paper, Adventure, and Annoying API. There are also some dependency handler methods added to more easily manage implemented dependencies.

Dependencies

  • Project.spigotAPI(version: String, configuration: String = "compileOnly", configurationAction: Action<ExternalModuleDependency> = Action {}): Adds Repository.MAVEN_CENTRAL and Repository.SPIGOT repositories, then adds org.spigotmc:spigot-api to the given configuration. Also sets the correct Java version based on the Minecraft version using getJavaVersionForMC(...)
  • Project.spigotNMS([version](version: String, configuration: String = "compileOnly", configurationAction: Action<ExternalModuleDependency> = Action {}): Same as above except also adds maven local repository and adds org.spigot:spigot instead of org.spigot:spigot-api
  • Project.paper(version: String, configuration: String = "compileOnly", configurationAction: Action<ExternalModuleDependency> = Action {}): Adds Repository.MAVEN_CENTRAL, Repository.SONATYPE_SNAPSHOTS_OLD, and Repository.PAPER repositories, then adds the corresponding Paper group/artificat to the given configuration
  • Project.adventure(vararg dependencies: AdventureDependency, configurationAll: String? = null): Adds the Repository.MAVEN_CENTRAL repository and then adds all of the given dependencies
  • Project.annoyingAPI(version: String, configuration: String = "implementation", configrationAction: ExternalModuleDependency.() -> Unit = {}): Adds the Repository.JITPACK repository and then xyz.srnyx:annoying-api to the given configuration. It will also automatically relocate xyz.srnyx.annoyingapi, org.bstats, javassist., org.reflections, de.tr7zw.changeme.nbtapi, and any other runtime dependencies that Annoying API uses. It'll also exclude net.byteflux:libby-bukkit and xyz.srnyx:java-utilities to avoid them being duplicated.

Usage

Using the built-in dependencies is a bit confusing at first, but it'll make more sense the more you use it, here are some examples:

// This will add Spigot-API to compileOnly dependencies
spigotAPI("1.8.8")

// You can use any sort of dependency configuration desired and even add a configuration for it
spigotNMS("1.19.2", "implementation") {
    exclude("org.bukkit")
}

// Annoying API does a bunch of stuff, see its description above for more info
annoyingAPI("3.0.1")

Helpers

  • getJavaVersionForMC(minecraftVersion: String): Returns the required Java version for the given version of Minecraft
  • getVersionString(version: String): Returns the version string with -R0.1-SNAPSHOT appended to it
  • <T: ModuleDependency> DependencyHandler.implementationRelocate(project: Project, dependency: T, relocateFrom: String, relocateTo: String = "${project.getPackage()}.libs.${relocateFrom.split(".").last()}", configuration: T.() -> Unit = {}): Adds the dependency to the implementation configuration and relocates its classes
  • DependencyHandler.implementationRelocate(project: Project, dependency: String, relocateFrom: dependency.split(":").first(), relocateTo: String = "${project.getPackage()}.libs.${relocateFrom.split(".").last()}", configuration: Action<ExternalModuleDependency> = Action {}): Same as above, just uses different types for parameters

Usage

Making use of the custom implementation methods is also a bit strange at first, here are some examples of that:

dependencies {
    // This will add bStats as an implementation dependency, as well as relocate 'org.bstats' to '{getPackage()}.libs.bstats'.
    implementationRelocate(project, "org.bstats:bstats-bukkit:3.0.0")

    // This is the same as above, except instead of relocating 'de.tr7zw', it'll relocate 'de.tr7zw.changeme.nbtapi' to '{getPackage()}.libs.nbtapi'.
    implementationRelocate(project, "de.tr7zw:nbt-api:2.11.3", "de.tr7zw.changeme.nbtapi")

    // The same as above, except it will relocate 'xyz.srnyx.annoyingapi' to 'libs.annoyingapi' instead of calculating 'from' & 'to' itself
    implementationRelocate(project, "xyz.srnyx:annoying-api:3.0.1", "xyz.srnyx.annoyingapi", "libs.annoyingapi")
}