【问题标题】:Gradle 'Implementation' Dependencies Are Runtime-Scoped When Project Has No Classes Of Its Own当项目没有自己的类时,Gradle“实现”依赖项是运行时范围的
【发布时间】:2019-10-20 07:53:46
【问题描述】:

我有一个用于自定义 Spring Boot Starter 的 Gradle 多项目构建。遵循 Spring Boot Starter 约定,我的类都在一个“自动配置”项目中,并且我有一个单独的“启动”项目,它只引入使用自动配置所需的依赖项。

多项目构建会生成 2 个不可运行的 jar,1 个用于自动配置子项目,1 个用于启动子项目。我使用这个启动器的新项目拉入了启动器 jar,但是当我使用来自传递依赖项的类时,我的项目在类路径的任何地方都找不到它们。

当我深入研究 starter jar 时,我发现它定义的所有依赖项都是 RUNTIME 范围的,这可以解释问题。我可以通过将启动器中的所有依赖项设置为“编译”而不是“实现”来“解决”问题,但我的理解是“编译”即将退出,并且“实现”依赖项应该是编译范围的反正。有人可以告诉我,将启动器依赖项定义为“实现”而不将它们限定为生成的 jar 中的“运行时”可能需要哪些额外的配置?

我的启动器/自动配置多项目 root gradle 文件:

plugins {
    id 'org.springframework.boot' version '2.1.4.RELEASE' apply false
    id 'io.spring.dependency-management' version '1.0.7.RELEASE' apply false
}

wrapper {
    gradleVersion = '5.2.1'
}

repositories {
    mavenLocal()
    // private repo info omitted
    mavenCentral()
    jcenter()
}

subprojects {
    apply plugin: 'java'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'
    apply plugin: 'maven-publish'
    apply plugin: 'java-library'

    group = 'com.mystarter'

    repositories {
        mavenLocal()
        // private repo info omitted
        mavenCentral()
        jcenter()
    }

    dependencies {
        annotationProcessor "org.springframework.boot:spring-boot-autoconfigure-processor"
        annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
    }

    bootJar {
        enabled = false
    }

    jar {
        enabled = true
    }

    javadoc {
        failOnError = false
        options.addStringOption('Xdoclint:none', '-quiet')
    }

    task sourcesJar(type: Jar) {
        from sourceSets.main.allJava
        classifier = 'sources'
    }
    task javadocJar(type: Jar) {
        from javadoc
        classifier = 'javadoc'
    }

    publishing {
        publications {
            myProjStarterArtifacts(MavenPublication) {
                from components.java
                artifact sourcesJar
                artifact javadocJar
            }
        }
        repositories {
            // private repo info omitted
        }
    }

    tasks.build.finalizedBy tasks.publishToMavenLocal
}

我的 starter 子项目的构建文件:

dependencies {
    compile project(':myproj-spring-boot-autoconfig')

    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.security:spring-security-cas'
    implementation 'org.springframework.security:spring-security-ldap'
}

如果我将上面的“实现”行更改为“编译”行,那么生成的 pom 文件就会停止将这 4 个依赖项设置为“运行时”范围,而是将它们正确地定义为“编译”范围。附带说明一下,“编译项目”行工作得很好,只是当项目没有自己的类时,“实现”行似乎不像我期望的那样工作。

我的新项目对我的初学者的依赖:

dependencies {
    implementation('com.myproj:myproj-spring-boot-starter:1.0.0')
    // other dependencies
}

【问题讨论】:

    标签: spring-boot gradle dependency-management spring-boot-starter


    【解决方案1】:

    在 Gradle 项目中定义的implementation 依赖项仅对所述项目的消费者的runtimeClasspath 具有传递性,这是设计使然。

    如果您有一个没有代码但只定义依赖项的项目,请考虑为其使用 java-platform 插件,它允许您指定约束和可选的依赖项。

    否则,如果您希望项目在编译时向使用者公开其依赖项,则应使用 api 配置来声明它们,而不是使用确实即将退出的 compile。

    更多详情请见documentation。

    【讨论】:

      猜你喜欢
      • 2015-10-14
      • 1970-01-01
      • 2016-05-28
      • 2015-05-07
      • 1970-01-01
      • 2013-03-25
      • 1970-01-01
      • 2016-05-02
      • 1970-01-01
      相关资源
      最近更新 更多