我有这个在我的gradle:

sourceSets {
    main {
        compileClasspath += configurations.provided
        runtimeClasspath += configurations.provided
    }
    test {
        compileClasspath += configurations.provided
        runtimeClasspath += configurations.provided
    }
}

当我在此代码中打印runtimeClasspath时:
task runTopology(type: JavaExec) {
    classpath = sourceSets.main.runtimeClasspath

    sourceSets.main.runtimeClasspath.each { println it }

我得到类似的东西:
...
/home/steven/.gradle/caches/modules-2/files-2.1/com.lmax/disruptor/3.3.2/8db3df28d7e4ad2526be59b54a1cbd9c9e982a7a/disruptor-3.3.2.jar
/home/steven/.gradle/caches/modules-2/files-2.1/org.apache.logging.log4j/log4j-api/2.1/588c32c91544d80cc706447aa2b8037230114931/log4j-api-2.1.jar
/home/steven/.gradle/caches/modules-2/files-2.1/org.apache.logging.log4j/log4j-core/2.1/31823dcde108f2ea4a5801d1acc77869d7696533/log4j-core-2.1.jar
/home/steven/.gradle/caches/modules-2/files-2.1/org.apache.logging.log4j/log4j-slf4j-impl/2.1/fe9d0925aeee68b743e9ea4b68ca9190a2a411a/log4j-slf4j-impl-2.1.jar
/home/steven/.gradle/caches/modules-2/files-2.1/org.slf4j/log4j-over-slf4j/1.6.6/170e8f7395753ebbe6710bb862a84689db1f128b/log4j-over-slf4j-1.6.6.jar
...

是否可以排除sourceSets.main.runtimeClasspath中的以下行?
/home/steven/.gradle/caches/modules-2/files-2.1/org.apache.logging.log4j/log4j-slf4j-impl/2.1/fe9d0925aeee68b743e9ea4b68ca9190a2a411a/log4j-slf4j-impl-2.1.jar

我不想将它包含在runtimeClasspath中,因为它会与类路径中的另一个类一起崩溃。

最佳答案

您可以使用FileCollection.filter()按文件名过滤。
以下内容将从log4j-slf4j-impl-2.1.jar中排除runtimeClasspath:

sourceSets {
    main {
        compileClasspath += configurations.provided
        runtimeClasspath += configurations.provided
        runtimeClasspath = runtimeClasspath.filter { File file ->
            file.name ==~ "log4j-slf4j-impl-2.1.jar" ? null : file
        }
    }
    test {
        compileClasspath += configurations.provided
        runtimeClasspath += configurations.provided
        runtimeClasspath = runtimeClasspath.filter { File file ->
            file.name ==~ "log4j-slf4j-impl-2.1.jar" ? null : file
        }
    }
}

关于java - 在sourceSets.main.runtimeClasspath中删除一个jar,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43512061/

10-10 13:14