为特定任务向installDist添加自定义配置



我正在尝试进行自定义运行时依赖项配置,以便仅为特定任务安装指定的依赖项。使用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
}

在一个漫长的周末之后,我得到了一些工作。也许有人能告诉我有没有更好的办法?而且,它不能完全工作,因为它没有在配置中遵循传递依赖关系(这有点痛苦,因为所有的子依赖关系都需要手动添加)。

<<p>解决方案/strong>:

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

B项目build.gradle

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

最新更新