19

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

Scenario-1:

        dependencies {

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


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

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

        }

1.Whenever I executes gradle run -Pwithsources

    it is printing "withsources" 

2. But for gradle run

    it is printing "withsources" 

Scenario-2:

        dependencies {

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


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

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

        }

1.Whenever I executes gradle run -Pwithsources

    it is not printing "withsources" 

2. But for gradle run

    it is not printing "withsources" 

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

Slok
  • 576
  • 1
  • 12
  • 27
  • Sounds like you want a [composite build](https://docs.gradle.org/current/userguide/composite_builds.html). As composite builds are declared in groovy this can be conditional – lance-java Jul 03 '18 at 13:11

1 Answers1

29

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"
    }
}

No property:

$ 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

With property:

$ 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

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

Raniz
  • 10,882
  • 1
  • 32
  • 64