繁体   English   中英

如何以编程方式在 Gradle 中添加传递依赖项?

[英]How can I programmatically add transitive dependencies in Gradle?

我对 Gradle 非常(非常)新,我正在评估从 SBT 切换到 Z7B5CC4FB56E11DC7520F716E11DC7520F716EC1763ABBZ 项目的潜在好处 my164587C 当前雇主。 因此,我不希望立即将整个构建转换为 Gradle,但似乎应该可以动态地基于 Gradle 构建(atm,主要是编译器标志和与全局版本的依赖关系)。 这样我不会不必要地增加同事的认知负担,但同时我不会冒我的构建滞后或与 pom 文件中的“规范”配置冲突的风险。

这是我目前的build.gradle (到目前为止,我才开始解决依赖关系):

def dependencyVersions = [:]
new XmlSlurper().parse('pom.xml').dependencyManagement.dependencies.dependency.each {
    dependencyVersions["${it.groupId}:${it.artifactId}"] = it.version.text()
}

allprojects {
    group = 'my.org'
    version = 'latest-SNAPSHOT'
}

subprojects {
    apply plugin: 'java'
    apply plugin: 'scala'

    repositories {
        mavenLocal()
        maven {
            url = uri('https://repo.maven.apache.org/maven2')
        }
    }

    dependencies {
        implementation 'org.scala-lang:scala-library:2.13.3'

        // ... Global deps ...

        new XmlSlurper().parse("$projectDir/pom.xml").dependencies.dependency.each {
            if(it.groupId.text() == 'my.org') {
                add('implementation', project(":${it.artifactId}"))
            } else {
                def version = it.version.text() ? it.version.text() : dependencyVersions["${it.groupId}:${it.artifactId}"]
                def dep = "${it.groupId}:${it.artifactId}:${version}"
                def scope = it.scope.text() ? it.scope.text() : 'compile'
                if(scope == 'compile')
                    add('implementation', dep)
                else if(scope == 'test') {
                    add('testImplementation', dep)
                } else {
                    throw new Exception("Unrecognized dependency scope: $scope")
                }
            }
        }
    }

    sourceCompatibility = '1.8'

    tasks.withType(JavaCompile) {
        options.encoding = 'UTF-8'
    }
}

以上几乎可以工作。 应该添加直接依赖项,但问题是传递依赖项在编译时不可用。 如何配置子项目,以便解析任何传递依赖项并将其添加到子项目中?

如果您打算将模块/子项目用作另一个子项目/项目的依赖项或库,则应使用Java 库插件而不是Java 插件

使用 Java 库插件,您将可以访问api配置。 您可以在API 和实现分离文档中阅读有关implementationapi的更多信息。

所以你的 Gradle 文件可能是:

dependencies {
    implementation 'org.scala-lang:scala-library:2.13.3'

    // ... Global deps ...

    new XmlSlurper().parse("$projectDir/pom.xml").dependencies.dependency.each {
        if(it.groupId.text() == 'my.org') {
            add('api', project(":${it.artifactId}"))
        } else {
            def version = it.version.text() ? it.version.text() : dependencyVersions["${it.groupId}:${it.artifactId}"]
            def dep = "${it.groupId}:${it.artifactId}:${version}"
            def scope = it.scope.text() ? it.scope.text() : 'compile'
            if(scope == 'compile')
                add('api', dep)
            else if(scope == 'test') {
                add('testImplementation', dep)
            } else {
                throw new Exception("Unrecognized dependency scope: $scope")
            }
        }
    }
}

这里唯一不同的是从implementation切换到api

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM