gradle中的条件依赖 [英] conditional dependencies in gradle

查看:518
本文介绍了gradle中的条件依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用gradle进行多项目构建. 我有一个要求选择依赖项,取决于在命令行中注入的属性的条件.

I am working on multi-project build using gradle. I have an requirement pick dependencies on condition of property injected at commandline .

场景1:

        dependencies {

            if( ! project.hasProperty("withsources")){


             compile 'com.xx.yy:x-u:1.0.2'

            }else{
              println " with sources"
              compile project (':x-u')
            }

        }

1.无论何时执行 gradle run -Pwithsources

    it is printing "withsources" 

2.但是对于渐变运行

    it is printing "withsources" 

场景2:

        dependencies {

            if(  project.hasProperty("withsources")){


             compile 'com.xx.yy:x-u:1.0.2'

            }else{
              println " with sources"
              compile project (':x-u')
            }

        }

1.无论何时执行 gradle run -Pwithsources

    it is not printing "withsources" 

2.但是对于渐变运行

    it is not printing "withsources" 

我不知道它总是进入else循环.任何人都可以在这里提供帮助.

I don't know it always goes to else loop. Anybody can help here.

推荐答案

在没有看到完整的build.gradle的情况下,我无法真正地说出您的问题是什么,但是您的一般方法是正确的.

I can't really say what your issue is without seeing the full build.gradle but your general approach is correct.

这是一个对我有用的小例子:

Here's a tiny example that works for me:

plugins {
    id "java"
}

repositories {
    jcenter()
}

dependencies {
    if (project.hasProperty("gson")) {
        implementation "com.google.gson:gson:2.8.5"
    } else {
        implementation "org.fasterxml.jackson.core:jackson-core:2.9.0"
    }
}

没有财产:

$ gradle dependencies --configuration implementation
:dependencies

------------------------------------------------------------
Root project
------------------------------------------------------------

implementation - Implementation only dependencies for source set 'main'. (n)
\--- org.fasterxml.jackson.core:jackson-core:2.9.0 (n)

(n) - Not resolved (configuration is not meant to be resolved)

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed

具有属性:

$ gradle -Pgson dependencies --configuration implementation
:dependencies

------------------------------------------------------------
Root project
------------------------------------------------------------

implementation - Implementation only dependencies for source set 'main'. (n)
\--- com.google.gson:gson:2.8.5 (n)

(n) - Not resolved (configuration is not meant to be resolved)

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed

是否可能在其他地方定义了 withsources ?就像 gradle.properties ?

Is it possible that you have defined withsources somewhere else? Like in gradle.properties?

这篇关于gradle中的条件依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆