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

Nar*_*pai 3 gradle-kotlin-dsl

我经历了有关build.gradle的类似问题,并且浏览了Gradle Kotlin Primer,但看不到如何在build.gradle.kt文件中添加.jar文件。我正在尝试避免使用mavenLocal()

Hah*_*pro 13

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

implementation(files("/commonjar/3rdparty/gson-2.8.5.jar"))
Run Code Online (Sandbox Code Playgroud)

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

完整的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")
}
Run Code Online (Sandbox Code Playgroud)

  • 最好指定完整路径,即 `implementation(files("$projectDir/commonjar/3rdparty/gson-2.8.5.jar"))` 而不是相对路径。我在使用相对路径的 Travis-CI 上遇到了错误。 (3认同)

Nic*_*las 13

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

api(fileTree("src/main/libs") { include("*.jar") })
Run Code Online (Sandbox Code Playgroud)


dav*_*ola 5

如果您正在寻找相当于

implementation fileTree(dir: 'libs', include: ['*.jar'])
Run Code Online (Sandbox Code Playgroud)

那将是:

implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
Run Code Online (Sandbox Code Playgroud)

  • 请详细了解如何添加单个 jar 文件(带绝对目录) (2认同)
  • 您有关于文档的任何参考资料可以使这一切变得更加明显吗? (2认同)