【问题标题】:gradle - Add custom configuration to installDist for a specific taskgradle - 为特定任务添加自定义配置到 installDist
【发布时间】:2021-05-03 23:12:37
【问题描述】:

我正在尝试进行自定义运行时依赖项配置,以便仅为特定任务安装指定的依赖项。使用installDist 任务安装依赖项。因此,似乎我需要将配置添加到 runtimeClasspath 以完成一项任务而不是另一项任务。我想我需要一个自定义分发,但我不确定如何设置它以拥有不同的runtimeClasspath

在下面的示例中,我希望 run2 任务安装 myRuntimeDep 依赖项,但对于 run1 任务我没有。

我整天都在努力解决这个问题,有人知道我错过了什么吗?

例如build.gradle:

configurations {
    myRuntimeDep.extendsFrom runtimeOnly
}

dependencies {
    ...
    myRuntimeDep 'the:dependency:1.0'
}

task run1(type: JavaExec, dependsOn: installDist) {
   // does not need myRuntimeDep dependencies
}

task run2(type: JavaExec, dependsOn: installDist) {
    // needs myRuntimeDep dependencies
}

【问题讨论】:

    标签: gradle groovy dependencies classpath


    【解决方案1】:

    所以在一个长周末之后,我有点工作了。也许有人可以告诉我是否有更好的方法?此外,它不能完全工作,因为它不遵循配置的传递依赖(这有点痛苦,因为所有子依赖都需要手动添加)。

    解决方案

    顶级build.gradle

    ...
    subprojects {
        configurations {
            fooRuntime.extendsFrom runtimeOnly
            fooClasspath.extendsFrom runtimeClasspath, fooRuntime
        }
        
        distributions {
            foo {
                contents {
                    from installDist.destinationDir
                    from(configurations.fooClasspath) {
                        into 'lib'
                    }
                }
            }
        }
        installFooDist.dependsOn installDist
    }
    

    项目 A build.gradle

    dependencies {
        fooRuntime project(':projectB')
        fooRuntime project(':projectC') // only need this because transitive dependencies won't work
    }
    
    task run(type: JavaExec, dependsOn: installFooDist) {
       classpath = fileTree("$installFooDist.destinationDir/lib")
    }
    

    项目Bbuild.gradle

    dependencies {
        fooRuntime project(':projectC')
    }
    
    task run(type: JavaExec, dependsOn: installFooDist) {
       classpath = fileTree("$installFooDist.destinationDir/lib")
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-01
      • 2015-06-14
      相关资源
      最近更新 更多