代码之家  ›  专栏  ›  技术社区  ›  user1079877

使用“java库”和“api”而不是“编译”获取Gradle中的运行库列表

  •  1
  • user1079877  · 技术社区  · 7 年前

    我使用此代码获取所需的库,并在编译期间复制它们:

    task copyToLib( type: Copy ) {
        into "$buildDir/libs/lib"
        from configurations.runtime
    }
    
    jar {
        dependsOn copyToLib
        ...
    }
    

    当我使用lagecy-Gradle模型添加依赖项时,使用 compile :

    dependencies {
        compile 'net.objecthunter:exp4j:0.4.8'
        compile 'io.undertow:undertow-core:2.0.16.Final'
        compile 'org.postgresql:postgresql:42.2.5'
        ...
    }
    

    但是当我使用 api 关键词:

    dependencies {
        api 'net.objecthunter:exp4j:0.4.8'
        api 'io.undertow:undertow-core:2.0.16.Final'
        api 'org.postgresql:postgresql:42.2.5'
        ...
    }
    

    configurations.runtime

    1 回复  |  直到 7 年前
        1
  •  2
  •   M.Ricciuti    7 年前

    这个 runtime 配置也被弃用,如 compile 配置(请参阅以下文档: https://docs.gradle.org/4.10/userguide/java_plugin.html#tab:configurations

    所以你需要改变你的想法 from 条款 copyToLib compileClasspath ( 使用 runtimeClasspath

    dependencies {
        api 'net.objecthunter:exp4j:0.4.8'
        api 'org.postgresql:postgresql:42.2.5'
        api 'io.undertow:undertow-core:2.0.16.Final'
    }
    
    task copyToLib(type: Copy) {
        into "$buildDir/libs/lib"
        from configurations.runtimeClasspath
    }
    

    https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_configurations_graph