How can I include a local jar dependency and its corresponding sources jar (for my IDE, Intellij) in Gradle?
I have tried adding a flatDir to lib (a directory in the same parent directory as all the Gradle stuff) under repositories, where lib contains mylib-1.0.jar and mylib-1.0-sources.jar. I then put implementation name: "mylib-1.0" under dependencies. The jar with compiled classes was included, but not the sources.
I also tried creating a local maven repository. In this case, lib contained
lib/xxx/yyy/mylib/1.0/mylib-1.0.jar
and
lib/xxx/yyy/mylib/1.0/mylib-1.0-sources.jar
where xxx.yyy is the group ID. I added
maven {
url uri("lib")
}
under repositories and
implementation group: "xxx.yyy", name: "mylib", version: "1.0"
under dependencies. Still did not work--neither jars were included this time.
I also tried adding a minimal POM in the same directory as both the jars, but that did not change anything.
Any idea as to where I could be going wrong?
Note: I am no expert at using Gradle or Maven.
Edit: Rationale: in case somebody suggests it, I am aware I can just include as a dependency the jar with the compiled class and "link" the sources jar to it in Intellij, but then every time I refresh gradle I have to re-link them.
I provide below the following approaches.
Prior to gradle 5
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
}
Here libs is the directory which contains the list of jar files and it should be location inside your project base directory.
You can also use in the following manner if you have only one jar file. Here libs refer to the directory which contains only one jar file and libs is available in the project base directory.
dependencies {
compile files('libs/your jar file name.jar')
}
If you want to specify a list of jar files, you can use in the following manner.
dependencies {
compile files(‘libs/a.jar’,
‘libs/b.jar’,
‘libs/c.jar’
)
}
In Gradle 5
You have to use in the follwong manner.
dependencies {
externalLibs files('libs/a.jar', 'libs/b.jar')
}
I have a Gradle project which depends on sub-projects. I would like to create a "fat jar" containing all my sub-projects, and externel dependencies as external jars.
build.gradle:
dependencies {
compile project(':MyDep1')
compile project(':MyDep2')
compile 'com.google.guava:guava:18.0'
}
I would like to be able to generate the following output:
MyProject.jar -> Includes MyDep1 & MyDep2
libs/guavaXXX.jar -> Guava as external lib
I don't know how I could do this.
Use different configurations to hold your internal and external dependencies and package only one of those configurations into your project artifact.
configurations{
internalCompile
externalCompile
}
//add both int and ext to compile
configurations.compile.extendsFrom(internalCompile)
configurations.compile.extendsFrom(externalCompile)
dependencies{
internalCompile project(':MyDep1')
internalCompile project(':MyDep2')
externalCompile 'com.google.guava:guava:18.0'
}
in your fat jar task, include only from internalCompile
I finally made it work with this solution:
jar {
subprojects.each {
from files(it.sourceSets.main.output)
}
}
distributions {
main {
contents {
exclude subprojects.jar.archivePath.name
}
}
}
In my project's jar, I include the content of all subprojects ouputs. In the distribution, I exclude the jar from subprojects (so it only contains dependencies). This is probably not the best way, but it's simple and it works.
dependencies {
compile 'org.slf4j:slf4j-api:1.7.13'
compile group: 'org.apache.commons', name: 'commons-math3' , version: '+'
testCompile 'junit:junit:4.12'
}
Even if I add this, when I run gradle build, it works, and codes with commons-math3 can be compiled. But when I run a jar file in build/,
it says Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/math3/complex/Complex
But the official Gradle site says, the resource in 'compile' will also be included in 'runtime' dependency. and I also tried adding the commons-math to runtime. but it does not work.
Maybe this is my misunderstood of the dependency system.
How can I include external library from maven repository into a jar file made by the Gradle.
What you are looking for is either the distribution zips produced by the application plugin or the shadow jar (also called fat jar) produced by the shadowJar plugin:
The distribution zip (application plugin)
About the distribution zip
The distribution zips look like this:
my-app-0.2.0.zip
├──bin
│ ├──my-app
│ └──my-app.bat
└──lib
├──my-app-0.2.0.jar
├──slf4j-api.1.7.13.jar
└──commons-math3-3.6.jar
You can then run your application with its dependencies by unzipping what has been produced in build/distributions/ and running either my-app.bat (on windows) or ./my-app (on linux or OS X)
Building a distribution zip
Here is a sample gradle build file for making a distribution zip:
build.gradle
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'org.myapp.Main'
repositories { jcenter() }
dependencies {
compile 'org.slf4j:slf4j-api:1.7.13'
compile 'org.apache.commons:commons-math3:3.6'
testCompile 'junit:junit:4.12'
}
Can be run with gradle distributionZip or gradle distributionTar. To just run the application, use gradle run.
The shadow jar
About the shadow jar
The shadow jar is one giant jar file that is a combination of your program and its libraries, packed together into one file. You will get a file that is self-contained and can be run by a double-click on most systems (e.g. on Windows that works, on Xubuntu it can be run by right-clicking and selecting 'Run with Oracle Java 8 Runtime', etc...).
Building a distribution zip
Here is, again, a sample build.gradle file:
apply plugin: 'java'
apply plugin: 'com.github.johnrengelman.shadow'
mainClassName = 'org.myapp.Main'
jar {
manifest {
attributes('Main-Class': mainClassName)
}
}
buildscript {
repositories { jcenter() }
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.2'
}
}
repositories { jcenter() }
dependencies {
compile 'org.slf4j:slf4j-api:1.7.13'
compile 'org.apache.commons:commons-math3:3.6'
testCompile 'junit:junit:4.12'
}
Run it with gradle shadowJar - Your jar with packed dependencies will be in build/libs and it will be named my-app-x.x.x-all.jar.
Gradle is first of all a build tool (just like maven, btw).
Its "responisiblity" starts when you feed it a source file and ends when you get your artifact (in your case its a jar).
Now when you're going to actually run your application there is a plethora of different options here.
If you just run java -jar <your_jar> you are responsible by yourself to construct the classpath.
If you run it with some kind of external runner, you should read the documentation of it and supply it a classpath.
Hope this helps
I have tried to add my local .jar file dependency to my build.gradle file:
apply plugin: 'java'
sourceSets {
main {
java {
srcDir 'src/model'
}
}
}
dependencies {
runtime files('libs/mnist-tools.jar', 'libs/gson-2.2.4.jar')
runtime fileTree(dir: 'libs', include: '*.jar')
}
And you can see that I added the .jar files into the referencedLibraries folder here: https://github.com/WalnutiQ/wAlnut/tree/version-2.3.1/referencedLibraries
But the problem is that when I run the command: gradle build on the command line I get the following error:
error: package com.google.gson does not exist
import com.google.gson.Gson;
Here is my entire repo: https://github.com/WalnutiQ/wAlnut/tree/version-2.3.1
According to the documentation, use a relative path for a local jar dependency as follows.
Groovy syntax:
dependencies {
implementation files('libs/something_local.jar')
}
Kotlin syntax:
dependencies {
implementation(files("libs/something_local.jar"))
}
If you really need to take that .jar from a local directory,
Add next to your module gradle (Not the app gradle file):
repositories {
flatDir {
dirs("libs")
}
}
dependencies {
implementation("gson-2.2.4")
}
However, being a standard .jar in an actual maven repository, why don't you try this?
repositories {
mavenCentral()
}
dependencies {
implementation("com.google.code.gson:gson:2.2.4")
}
You could also do this which would include all JARs in the local repository. This way you wouldn't have to specify it every time.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
The following works for me:
compile fileTree(dir: 'libs', include: '*.jar')
Refer to the Gradle Documentation.
You can try reusing your local Maven repository for Gradle:
Install the jar into your local Maven repository:
mvn install:install-file -Dfile=utility.jar -DgroupId=com.company -DartifactId=utility -Dversion=0.0.1 -Dpackaging=jar
Check that you have the jar installed into your ~/.m2/ local Maven repository
Enable your local Maven repository in your build.gradle file:
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
implementation ("com.company:utility:0.0.1")
}
Now you should have the jar enabled for implementation in your project
A solution for those using Kotlin DSL
The solutions added so far are great for the OP, but can't be used with Kotlin DSL without first translating them. Here's an example of how I added a local .JAR to my build using Kotlin DSL:
dependencies {
compile(files("/path/to/file.jar"))
testCompile(files("/path/to/file.jar"))
testCompile("junit", "junit", "4.12")
}
Remember that if you're using Windows, your backslashes will have to be escaped:
...
compile(files("C:\\path\\to\\file.jar"))
...
And also remember that quotation marks have to be double quotes, not single quotes.
Edit for 2020:
Gradle updates have deprecated compile and testCompile in favor of implementation and testImplementation. So the above dependency block would look like this for current Gradle versions:
dependencies {
implementation(files("/path/to/file.jar"))
testImplementation(files("/path/to/file.jar"))
testImplementation("junit", "junit", "4.12")
}
The accepted answer is good, however, I would have needed various library configurations within my multi-project Gradle build to use the same 3rd-party Java library.
Adding '$rootProject.projectDir' to the 'dir' path element within my 'allprojects' closure meant each sub-project referenced the same 'libs' directory, and not a version local to that sub-project:
//gradle.build snippet
allprojects {
...
repositories {
//All sub-projects will now refer to the same 'libs' directory
flatDir {
dirs "$rootProject.projectDir/libs"
}
mavenCentral()
}
...
}
EDIT by Quizzie: changed "${rootProject.projectDir}" to "$rootProject.projectDir" (works in the newest Gradle version).
Shorter version:
dependencies {
implementation fileTree('lib')
}
The Question already has been answered in detail. I still want to add something that seems very surprising to me:
The "gradle dependencies" task does not list any file dependencies. Even though you might think so, as they have been specified in the "dependencies" block after all..
So don't rely on the output of this to check whether your referenced local lib files are working correctly.
A simple way to do this is
compile fileTree(include: ['*.jar'], dir: 'libs')
it will compile all the .jar files in your libs directory in App.
Some more ways to add local library files using Kotlin DSL (build.gradle.kts):
implementation(
files(
"libs/library-1.jar",
"libs/library-2.jar",
"$rootDir/foo/my-other-library.jar"
)
)
implementation(
fileTree("libs/") {
// You can add as many include or exclude calls as you want
include("*.jar")
include("another-library.aar") // Some Android libraries are in AAR format
exclude("bad-library.jar")
}
)
implementation(
fileTree(
"dir" to "libs/",
// Here, instead of repeating include or exclude, assign a list of paths
"include" to "*.jar",
"exclude" to listOf("bad-library-1.jar", "bad-library-2.jar")
)
)
The above code assumes that the library files are in libs/ directory of the module (by module I mean the directory where this build.gradle.kts is located).
You can use Ant patterns in includes and excludes as shown above.
See Gradle documentations for more information about file dependencies.
Thanks to this post for providing a helpful answer.
I couldn't get the suggestion above at https://stackoverflow.com/a/20956456/1019307 to work. This worked for me though. For a file secondstring-20030401.jar that I stored in a libs/ directory in the root of the project:
repositories {
mavenCentral()
// Not everything is available in a Maven/Gradle repository. Use a local 'libs/' directory for these.
flatDir {
dirs 'libs'
}
}
...
compile name: 'secondstring-20030401'
The best way to do it is to add this in your build.gradle file and hit the sync option
dependency{
compile files('path.jar')
}
The solution which worked for me is the usage of fileTree in build.gradle file.
Keep the .jar which need to add as dependency in libs folder. The give the below code in dependenices block in build.gradle:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
You can add jar doing:
For gradle just put following code in build.gradle:
dependencies {
...
compile fileTree(dir: 'lib', includes: ['suitetalk-*0.jar'])
...
}
and for maven just follow steps:
For Intellij:
File->project structure->modules->dependency tab-> click on + sign-> jar and dependency->select jars you want to import-> ok-> apply(if visible)->ok
Remember that if you got any java.lang.NoClassDefFoundError: Could not initialize class exception at runtime this means that dependencies in jar not installed for that you have to add all dependecies in parent project.
For Gradle version 7.4 with Groovy build file
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
implementation ':gson-2.2.4'
}
If you are on gradle 4.10 or newer:
implementation fileTree(dir: 'libs', includes: ['*.jar'])
Goto File -> Project Structure -> Modules -> app -> Dependencies Tab -> Click on +(button) -> Select File Dependency - > Select jar file in the lib folder
This steps will automatically add your dependency to gralde
Very Simple
Be careful if you are using continuous integration, you must add your libraries in the same path on your build server.
For this reason, I'd rather add jar to the local repository and, of course, do the same on the build server.
An other way:
Add library in the tree view. Right click on this one. Select menu "Add As Library".
A dialog appear, let you select module. OK and it's done.
I'm trying to add org.apache.commons.lang3 to my build. I've downloaded the library which is directory containing jar files.
My group is using gradle to build the project, and I know just enough to maybe ask the right question. So what I think the build is doing is
copying a bunch of .bnds to the build directory
compiles the java we have in src/main/java (via source sourceSets.main.java.srcDirs?)
I would like to add the lang3 library, but I'm not sure how to go about doing that. Can I just dump it into src/main/java? Or do I have to tell gradle about it?
This is what I think is relevant from the current build.gradle
ext.releaseDir = "${buildDir}/release/${tpVersion.getProgramName()}"
ext.bundlesDir = "${releaseDir}/nucleus/bin/nucleus_java/bundles/"
dependencies {
compile fileTree(dir: bundlesDir, include: '*.jar')
bnd {
source sourceSets.main.java.srcDirs
include '**/*.bnd'
You could declare it as a dependency, if it exists in any remote repository. That's the way I would do it.
But if you want to use the local file, do not put it in src/main. Use an extra folder called lib or similar on the same directory level as src or you build script.
Then you can add the local dependency to the build.gradle as in this sample:
repositories {
//central maven repo
mavenCentral()
}
dependencies {
//local file
compile files('libs/toxiclibscore.jar')
//dependencies from a remote repository
compile 'java3d:vecmath:1.3.1', 'commons-lang:commons-lang:2.6'
}
The simplest way is to use maven repository for accessing dependencies.
You can also access this jar directly from filesystem with file dependencies.
dependencies {
compile files('libs/a.jar', 'libs/b.jar')
compile fileTree(dir: 'libs', include: '*.jar')
}