关于gradle:运行时配置中排除的实现依赖性

Implementation Dependency Excluded from Runtime Configuration

在我的gradle文件中将依赖项添加为实现时,作为运行时配置的一部分列出时,它们将不包括在内。例如,尝试将它们放入路径罐中时,将它们排除在外,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
task pathingJar(type: Jar) {
    dependsOn configurations.runtime
    appendix = 'pathing'

    doFirst {
        manifest {
            attributes"Class-Path": configurations.runtime.files.collect {
                it.toURL().toString().replaceFirst(/file:\\/+/, '/')
            }.join(' ')
        }
    }
}

但是,当将它们作为编译依赖项移回时,此方法有效。现在的问题是在编译时,我的类路径要大得多。我的理解是,应将实现视为传递给直接使用者和运行时中的编译时,那么为什么将它们从该配置中排除呢?当将它们指定为" api "时,这也不起作用。这是使用gradle 5.6.1。


不赞成使用runtime配置,并由runtimeOnly代替。此旧配置不了解较新的implementationapi配置,因此这就是为什么在解决它时看不到依赖项的原因。

您想要的不是解析runtimeOnly配置,而是解析运行时使用的类路径。此配置称为runtimeClasspath。示例:

1
2
3
4
5
6
7
8
9
10
11
12
tasks.register("patchingJar", Jar) {
    dependsOn configurations.runtimeClasspath
    appendix = 'patching'

    doFirst {
        manifest {
            attributes"Class-Path": configurations.runtimeClasspath.files.collect {
                it.toURL().toString().replaceFirst(/file:\\/+/, '/')
            }.join(' ')
        }
    }
}