Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I include a single dependency in my JAR with Gradle?

I'm starting with Gradle and I was wondering how do I include a single dependency (TeamSpeak API in my case) into my JAR so that it could be available at the runtime.

Here is a part of my build.gradle :

apply plugin: 'java'

compileJava {
    sourceCompatibility = '1.8'
    options.encoding = 'UTF-8'
}

jar {
    manifest {
        attributes 'Class-Path': '.......'
    }

    from {
        * What should I put here ? *
    }
}

dependencies {
    compile group: 'org.hibernate', name: 'hibernate-core', version: '4.3.7.Final'
    compile group: 'org.spigotmc', name: 'spigot', version: '1.8-R0.1-RELEASE'
    // Many other dependencies, all available at runtime...

    // This one isn't. So I need to include it into my JAR :
    compile group: 'com.github.theholywaffle', name: 'teamspeak3-api', version: '+'

}

Thanks for your help :)

like image 326
Tlokuus Avatar asked Aug 21 '15 17:08

Tlokuus


1 Answers

The easiest way is to start with a separate configuration for the dependencies you want to include. I know you only asked about a single jar but this solution will work if you add more dependencies to your new configuration. Maven has a well known name for this sort of thing called provided, so that is what we will use.

   configurations {
      provided
      // Make compile extend from our provided configuration so that things added to bundled end up on the compile classpath
      compile.extendsFrom(provided)
   }

   dependencies {
      provided group: 'org.spigotmc', name: 'spigot', version: '1.8-R0.1-RELEASE'
   }

   jar {
       // Include all of the jars from the bundled configuration in our jar
       from configurations.provided.asFileTree.files.collect { zipTree(it) }
   }

Using provided as the name of the configuration is also important because when the jar gets published, any dependencies you have in the providedconfiguration will show up as provided in the POM.xml that gets published with the JAR. Maven dependency resolvers will not pull down provided dependencies and users of your jar will not end up with duplicate copies of classes on the classpath. See Maven Dependency Scopes

like image 52
CaTalyst.X Avatar answered Nov 07 '22 13:11

CaTalyst.X