如何将本地 .jar 文件依赖项添加到 build.gradle.kt 文件?

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

我已经解决了关于 build.gradle 的类似问题,我已经查看了 Gradle Kotlin Primer,但我不知道如何将 .jar 文件添加到 build.gradle.kt文件。我试图避免使用 mavenLocal()

如果您正在寻找

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

那就是:

implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))

对于 gradle 5.4.1 中的 Kotlin dsl build.gradle.kts

使用

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

我建议一次添加单个文件,因为这样更容易跟踪依赖项。

完整的 build.gradle.kts 看起来像这样

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")
}

另一个答案建议像我们通常在 Groovy 中那样使用映射键和值。不使用这种动态方法,更惯用和类型安全的等效方法是使用闭包来过滤要包含在文件树中的文件:

api(fileTree("src/main/libs") { include("*.jar") })