15

How can I add an external library in gradle? My build.gradle contains:

buildscript {
    repositories {
        mavenCentral()
        maven {
            name = "forge"
            url = "http://files.minecraftforge.net/maven"
        }
        maven {
            name = "sonatype"
            url = "https://oss.sonatype.org/content/repositories/snapshots/"
        }
    }
    dependencies {
        classpath 'net.minecraftforge.gradle:ForgeGradle:1.1-SNAPSHOT'
    }
}

My root folder is /FORGE/. I want to add /FORGE/build/libs/spigot.jar as a dependency.

Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
QuadX
  • 661
  • 1
  • 7
  • 10

1 Answers1

21

As explained in the documentation:

dependencies {
    compile files('libs/spigot.jar')
}

The above will add the libs/spigot.jar file to the compile configuration. You can of course add it to any other configuration (runtime, etc.).

Note that using build/libs is an extremely bad idea, since the whole build directory will be deleted as soon as you execute gradle clean. The build directory is used to store artifacts generated by the build.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • I create Folder lib(FORGE/build/lib) and put spigot.jar. My build gardle: dependencies { classpath 'net.minecraftforge.gradle:ForgeGradle:1.1-SNAPSHOT' compile files('lib/spigot.jar') } and have error http://i.gyazo.com/62470133c325b501287feae846adea6e.png Line 15: compile files('lib/spigot.jar') – QuadX Mar 30 '14 at 07:37
  • 1
    Read my answer again. files('lib/spigot.jar') will look for the file in lib/spigot.jar, not in build/lib/spigot.jar. You shouldn't put anything into build, because the build dir is for produced artifact, and will be deleted by gradle clean. – JB Nizet Mar 30 '14 at 07:42
  • Ok, moved spigot.jar to FORGE/lib/ and have same error on line 15: files('lib/spigot.jar') http://i.gyazo.com/62470133c325b501287feae846adea6e.png. – QuadX Mar 30 '14 at 07:52
  • Do you actually have a configuration named "compile"? If you have a configuration named "classpath" and that's where you want to add the jar, then use `classpath files('lib/spigot.jar')`. – JB Nizet Mar 30 '14 at 08:06
  • 1
    dependencies { classpath 'net.minecraftforge.gradle:ForgeGradle:1.1-SNAPSHOT' classpath files('libs/carftbukkit.jar', 'libs/netty-all-4.0.17.Final.jar', 'libs/commons-lang3-3.3.1.jar', 'libs/jinput.jar', 'libs/lwjgl.jar', 'libs/lwjgl_util.jar', 'libs/commons-io-2.4.jar', 'libs/minecraft_server.1.7.2.jar') } Work, thanks – QuadX Apr 01 '14 at 16:09
  • Will this work in production? does the underlying class-loader pick-up and load these external JARs as dependencies? or is this only during development? – Coder Guy Dec 14 '17 at 12:27
  • @JonathanNeufeld gradle is a build tool. It's used to compile, assemble, publish, package the application. It's not used at runtime. I don't know minecraft, but I guess spigot.jar is in the runtime classpath of minecraft. – JB Nizet Dec 14 '17 at 14:31
  • The answer is no, transitive dependencies are not loaded correctly by FML. It appears that Minecraft modding is hostile toward modularity. – Coder Guy Dec 15 '17 at 23:53