Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you add local .jar file dependency to build.gradle.kt file?

I have gone through similar questions regarding build.gradle and I have looked through the Gradle Kotlin Primer and I don't see how to add a .jar file to a build.gradle.kt file. I am trying to avoid using mavenLocal()

like image 223
Naruto Sempai Avatar asked Jan 13 '19 04:01

Naruto Sempai


People also ask

How do I add a dependency in Gradle?

To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your module's build.gradle file.

Where is the jar file for Gradle build?

The Jar is created under the $project/build/libs/ folder.

How can I create an executable jar with dependencies using Gradle?

Right click the module > Open module settings > Artifacts > + > JAR > from modules with dependencies. Set the main class.


3 Answers

If you are looking for the equivalent of

implementation fileTree(dir: 'libs', include: ['*.jar'])

that would be:

implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
like image 147
david.mihola Avatar answered Oct 28 '22 06:10

david.mihola


For Kotlin dsl in gradle 5.4.1 with build.gradle.kts

use

implementation(files("/commonjar/3rdparty/gson-2.8.5.jar"))

I suggest add single file at once because it is easier to keep track of dependencies.

full build.gradle.kts look like this

plugins {
    // Apply the java-library plugin to add support for Java Library
    `java-library`
}

repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

configurations { create("externalLibs") }



dependencies {
    // This dependency is exported to consumers, that is to say found on their compile classpath.
    api("org.apache.commons:commons-math3:3.6.1")

    // This dependency is used internally, and not exposed to consumers on their own compile classpath.
    implementation("com.google.guava:guava:27.0.1-jre")

    implementation(files("/commonjar/3rdparty/gson-2.8.5.jar"))


    // Use JUnit test framework
    testImplementation("junit:junit:4.12")
}
like image 41
Haha TTpro Avatar answered Oct 28 '22 05:10

Haha TTpro


Another answer suggests using map keys and values like we usually do in Groovy. Instead of using that dynamic approach, a more idiomatic and type-safe equivalent would be to use the closure to filter which files to include in the file tree:

api(fileTree("src/main/libs") { include("*.jar") })
like image 8
Nicolas Avatar answered Oct 28 '22 07:10

Nicolas