有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

gradle中的java条件依赖

我正在使用gradle进行多项目构建。 我有一个在命令行中注入属性的条件的要求

情景1:

        dependencies {

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


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

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

        }

一,。每当我执行渐变运行-Pwithsources

    it is printing "withsources" 

二,。但是对于gradle run

    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')
            }

        }

一,。每当我执行渐变运行-Pwithsources

    it is not printing "withsources" 

二,。但是对于gradle run

    it is not printing "withsources" 

我不知道它总是转到其他循环。这里谁都可以帮忙


共 (1) 个答案

  1. # 1 楼答案

    如果没有看到完整的构建,我真的不能说你的问题是什么。但你的一般方法是正确的

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

    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
    

    您是否可能在其他地方使用资源定义了?像在gradle.properties