Compile local JAR files with Gradle

Pete Houston
Dec 3, 2015 · 1 min read

Sometimes, you don’t want to build Java projects with dependencies that need to be downloaded from central repositories, instead having them as JAR files; and many Java libraries really don’t exist in repos. In this case, we need to config Gradle to include all those required JARs during the build, and it is easy.


1. Specifying each JAR filenames

dependencies {
compile files('libs/deps-01.jar', 'libs/deps-02.jar')
}

This will tell Gradle to include deps-01.jar and deps-02.jar during compilation.

2. Specifying a directory

dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}

Everything in libs/ directory with .jar extensions will be included for compiling.


To confirm that all required JARs included in compiling configuration, you can create this helper task,

task listJars << {
configurations.compile.each { File file -> println file.name }
}

Then run it,

$ gradle -q listJars
hibernate-core-3.6.7.Final.jar
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-3.2.0.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
jta-1.1.jar
slf4j-api-1.6.1.jar

That’s it, if you want to learn more about Gradle dependency management, visit Chapter 52.

Have fun :)

Pete Houston

Written by

Code / Design / Love @ https://petehouston.com

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade