简体   繁体   English

如何以编程方式将依赖项添加到Gradle配置?

[英]How to add programmatically dependencies to Gradle configuration?

I have the following code: 我有以下代码:

static def getFamilyDependencies(ConfigurationContainer configurations) {
    def result = configurations.collect { configuration ->
        configuration.allDependencies.findAll { dependency ->
            dependency instanceof DefaultProjectDependency
        } collect { projectDependency ->
            projectDependency.dependencyProject.name
        }
    } flatten()

    result as Set
}

and I would like to test it. 我想测试一下。 So far, I have: 到目前为止,我有:

@Test
void shouldGetFamilyDependencies() {
    final Project project = ProjectBuilder.builder().build()

    final configurations = project.getConfigurations()

    configurations.create('configuration0')
    configurations.create('configuration1')

    configurations.each { configuration ->
        println "***************** ${configuration}"

        configuration.allDependencies.each {
            println "@@@@@@@@@@@@@@@@@ ${it}"
        }
    }
}

How do I add dependencies to the configurations? 如何向配置添加依赖项? The following doesn't work: 以下不起作用:

    final Project subproject = ProjectBuilder.builder().build()
    configurations.configuration0 {
        subproject
    }
    configurations.configuration1 {
        allDependencies {
            subproject
        }
    }

这应该做的伎俩:

configuration.getDependencies().add(dependenyMock);
@Test
void shouldGetFamilyDependenciesAcrossAllConfigurations() {
    final expected = ['subproject-0', 'subproject-1']

    final Project project = ProjectBuilder.builder().build()
    final configurations = project.getConfigurations()

    configurations.create('configuration-0')
    final Project subproject0 = ProjectBuilder.builder().withName(expected[0]).build()
    project.dependencies {
        delegate.'configuration-0'(subproject0)
    }

    configurations.create('configuration-1')
    final Project subproject1 = ProjectBuilder.builder().withName(expected[1]).build()
    project.dependencies {
        delegate.'configuration-1'(subproject1)
    }

    final actual = RestorePublishedArtifactTask.getFamilyDependencies(configurations)

    assertThat(actual, hasItems(expected.toArray(new String[expected.size()])))
}

Try to do it in this way: 尝试以这种方式做到:

project.getDependencies().add('compile', project(':common-configuration'))

compile - a name of the configuration compile - 配置的名称
:common-configuration - a name of the project to add (or any other dependencies) :common-configuration - 要添加的项目的名称(或任何其他依赖项)

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

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