从 springBoots `bootJar` gradle 任务中排除特定依赖项

Exclude a specific dependency from springBoots `bootJar` gradle task

我需要从 springBoots bootJar gradle 任务中排除特定的依赖项(类似于 maven 中提供的范围)。

我尝试了自定义配置,但 dependency-which-should-not-be-in-bootJar 仍包含在生成的 jar 中。

configurations{
    provided
    implementation.extendsFrom provided
}

dependencies {
    // ...
    provided "dependency-which-should-not-be-in-bootJar"
}

jar {
    from configurations.compile - configurations.provided
    from configurations.runtime
}

bootJar {
    from configurations.compile - configurations.provided
    from configurations.runtime
    launchScript()
}

你实际上可以使用 compileOnly 作为你的依赖 gradle > 2.12

dependencies {
     // ...
     compileOnly "dependency-which-should-not-be-in-bootJar"
}

您仍然可以在测试 + 运行时使用它,但不会在最终构建的 jar 中。

我还在 spring 引导 gitter 频道中从 Andy Wilkinson 那里得到了答案,它的工作方式略有不同,但设法实现了相似。

configurations {
    custom
    runtime.extendsFrom custom
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web'
    custom 'com.h2database:h2'
}

bootJar {
    exclude {
        configurations.custom.resolvedConfiguration.files.contains(it.file)
    }
}

谢谢安迪 =)