spring引导自动配置是否应该使用运行时依赖项?

spring引导自动配置是否应该使用运行时依赖项?,spring,spring-boot,gradle,Spring,Spring Boot,Gradle,各位, 我对SpringBoot项目中的一件事很好奇 spring boot autoconfigure只是包含在其他启动器中,在我看来是一个子模块,为什么它使用buildSrc中定义的可选插件来声明编译和运行时将在它的依赖项中使用 为什么不在spring引导自动配置模块中使用compileOnly依赖项 spring boot autoconfigure真的需要运行时依赖项吗 Bellow是可选插件: @Override public void apply(Project project) {

各位,

我对SpringBoot项目中的一件事很好奇

spring boot autoconfigure只是包含在其他启动器中,在我看来是一个子模块,为什么它使用buildSrc中定义的可选插件来声明编译和运行时将在它的依赖项中使用

为什么不在spring引导自动配置模块中使用compileOnly依赖项

spring boot autoconfigure真的需要运行时依赖项吗

Bellow是可选插件:

@Override
public void apply(Project project) {
    Configuration optional = project.getConfigurations().create(OPTIONAL_CONFIGURATION_NAME);
    optional.attributes((attributes) -> attributes.attribute(Usage.USAGE_ATTRIBUTE,
            project.getObjects().named(Usage.class, Usage.JAVA_RUNTIME)));
    project.getPlugins().withType(JavaPlugin.class, (javaPlugin) -> {
        SourceSetContainer sourceSets = project.getConvention().getPlugin(JavaPluginConvention.class)
                .getSourceSets();
        sourceSets.all((sourceSet) -> {
            sourceSet.setCompileClasspath(sourceSet.getCompileClasspath().plus(optional));
            sourceSet.setRuntimeClasspath(sourceSet.getRuntimeClasspath().plus(optional));
        });
        project.getTasks().withType(Javadoc.class)
                .all((javadoc) -> javadoc.setClasspath(javadoc.getClasspath().plus(optional)));
    });
    project.getPlugins().withType(EclipsePlugin.class,
            (eclipsePlugin) -> project.getExtensions().getByType(EclipseModel.class)
                    .classpath((classpath) -> classpath.getPlusConfigurations().add(optional)));
}
下面是spring boot自动配置中build.gradle中代码的一部分:

optional("com.atomikos:transactions-jdbc")
optional("com.atomikos:transactions-jta")
optional("com.fasterxml.jackson.core:jackson-databind")
optional("com.fasterxml.jackson.dataformat:jackson-dataformat-cbor")
optional("com.fasterxml.jackson.dataformat:jackson-dataformat-xml")
optional("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")
optional("com.fasterxml.jackson.module:jackson-module-parameter-names")
optional("com.google.code.gson:gson")

我认为compileOnly可以取代optional,对吗?

compileOnly
对于
spring boot auto configure
的依赖项并不起作用。这将导致在发布的pom文件中声明依赖项,并提供
,这是不正确的。这也意味着在运行自己的测试时,依赖项不在项目的类路径上,因此它们也必须再次声明为
testImplementation
依赖项。

谢谢你,Andy。spring boot自动配置需要编译和测试范围,但不需要包括运行时,对吗?不一定。
optional
配置用于Spring Boot构建中的许多模块中,旨在模拟Maven中可选依赖项的行为。Maven中的一个可选依赖项出现在运行时类路径上,因此内部
可选
配置也是这样运行的。感谢您的回复!是的,我知道
optional
是编译和运行时依赖项,它不是可传递的。例如,<代码> Spring Booto自动配置使用<代码> Spring Web作为<代码>可选< /COD>依赖关系,如果不考虑测试范围,在这种情况下,我可以用<代码> > CixLime< /Calp>(<代码>提供/<代码> Maven)替换“代码>可选的<代码>吗?因为在我看来,这个依赖项只在编译时有用,因为
springbootstarterweb
将提供它。您要问的代码是springboot构建的实现细节。它与您在自己的
pom.xml
中能做和不能做的事情之间没有联系。
可选
配置中的依赖项不包括在
spring boot autoconfigure
发布的pom文件中。谢谢你,Andy,帮助很大!