Solved How to shade APIs with Gradle?

Discussion in 'Spigot Plugin Development' started by ratquaza, Feb 5, 2020.

  1. Currently trying to shade the NBT API for my plugin, and I've never understood how to setup my build.gradle? The NBT API is currently is in my 'libs' folder, but I don't know how to shade it.

    This is my build.gradle:

    Code (Text):
    plugins {
        id 'java'
    }

    group = pluginGroup
    version = pluginVersion

    sourceCompatibility = '1.8'
    targetCompatibility = '1.8'

    repositories {
        mavenCentral()
        maven {
            name = 'spigotmc-repo'
            url = 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/'
        }
        maven {
            name = 'sonatype'
            url = 'https://oss.sonatype.org/content/groups/public/'
        }
        maven { url 'https://jitpack.io' }
    }

    dependencies {
        testCompile group: 'junit', name: 'junit', version: '4.12'
        compileOnly 'org.spigotmc:spigot-api:1.15.2-R0.1-SNAPSHOT'
        compileOnly "com.github.MilkBowl:VaultAPI:1.7"
        compile fileTree(include: ['*.jar'], dir: 'libs')
    }

    import org.apache.tools.ant.filters.ReplaceTokens

    processResources {
        from(sourceSets.main.resources.srcDirs) {
            filter ReplaceTokens, tokens: [version: version]
        }
    }
     
     
    #1 ratquaza, Feb 5, 2020
    Last edited: Feb 5, 2020
  2. md_5

    Administrator Developer

    Quick Google suggests gradle shadowjar plugin
     
    • Like Like x 2
  3. Yes. You can do that without shadowJar but it's the simplest way and this plugin allow multiples transformations like merge jar, rename packages, compile only specified modules etc...
     
  4. Figured it out using shadowjar. This is my build.gradle

    Code (Text):
    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'com.github.jengelman.gradle.plugins:shadow:5.2.0'
        }
    }

    apply plugin: 'java'
    apply plugin: 'com.github.johnrengelman.shadow'

    group = pluginGroup
    version = pluginVersion

    sourceCompatibility = '1.8'
    targetCompatibility = '1.8'

    repositories {
        mavenCentral()
        maven {
            name = 'spigotmc-repo'
            url = 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/'
        }
        maven {
            name = 'sonatype'
            url = 'https://oss.sonatype.org/content/groups/public/'
        }
        maven { url 'https://jitpack.io' }
        maven {
            name = 'codemc-repo'
            url = 'https://repo.codemc.io/repository/maven-public/'
        }
    }

    dependencies {
        testCompile group: 'junit', name: 'junit', version: '4.12'
        compileOnly 'org.spigotmc:spigot-api:1.15.2-R0.1-SNAPSHOT'
        compileOnly "com.github.MilkBowl:VaultAPI:1.7"
        compile group: 'de.tr7zw', name: 'item-nbt-api-plugin', version: '2.2.0'
        compile fileTree(include: ['*.jar'], dir: 'libs')
    }

    shadowJar {
        dependencies {
            include dependency("de.tr7zw:item-nbt-api-plugin:2.2.0")
        }
        relocate("de.tr7zw", "shaded.de.tr7zw")
    }

    import org.apache.tools.ant.filters.ReplaceTokens

    processResources {
        from(sourceSets.main.resources.srcDirs) {
            filter ReplaceTokens, tokens: [version: version]
        }
    }