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.
Dependency constraints allow you to define the version or the version range of both dependencies declared in the build script and transitive dependencies. It is the preferred method to express constraints that should be applied to all dependencies of a configuration.
Gradle can model dependencies between modules. Those dependencies are called project dependencies because each module is represented by a Gradle project. At runtime, the build automatically ensures that project dependencies are built in the correct order and added to the classpath for compilation.
Transitive dependencyBy default, Gradle resolves transitive dependencies automatically. The version selection for transitive dependencies can be influenced by declaring dependency constraints.
As part of the metadata, a module can define that other modules are needed for it to work properly. For example, the JUnit 5 platform module also requires the platform commons module. Gradle automatically resolves those additional modules, so called transitive dependencies.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With