Reputation: 48923
I would like to apply configuration in Gradle script only when condition met:
useRepo = System.getenv()["IGNORE_REPO_CFG"] == null
buildscript {
useRepo && repositories { // <== HERE
maven.url 'http://localhost:8081/artifactory/release'
}
}
subprojects {
configurations {
all {
useRepo && resolutionStrategy { // <== HERE
cacheChangingModulesFor 0, 'seconds'
cacheDynamicVersionsFor 0, 'seconds'
}
}
}
}
Because of Groovy/Gradle scope magic I can't pass useRepo
to buildscript
and subprojects.configurations.all
scope.
I read about wrapping in class:
class Cfg {
static final useRepo = System.getenv()["SA_IGNORE_REPO_CFG"] == null
}
but on Cfg.useRepo
I got:
java.lang.ClassNotFoundException: Cfg
UPDATE On:
project.ext.useRepo = System.getenv()["SA_IGNORE_REPO_CFG"] == null
buildscript {
project.ext.useRepo && repositories {
maven.url 'http://localhost:8081/artifactory/lognet-release'
}
}
I got:
Caused by: groovy.lang.MissingPropertyException: Cannot get property 'useRepo' on extra properties extension as it does not exist
Upvotes: 0
Views: 682
Reputation: 11135
Like you tried, you should use project.ext
:
project.ext.useRepo = System.getenv()["SA_IGNORE_REPO_CFG"] == null
but when you now try to use project.ext
inside subprojects
it is empty, because it is not defined in this project. so you need to access it with ``rootProject.ext`, cause you defined it there
subprojects {
configurations {
all {
rootProject.ext.useRepo && resolutionStrategy { // <== HERE
cacheChangingModulesFor 0, 'seconds'
cacheDynamicVersionsFor 0, 'seconds'
}
}
}
}
Your try the use the ext
inside the buildscript
, but that doesn't work, because the buildscript
-closure gets executed first, and then the other script. See this Gradle Discussion
If you want to do this all, you can specify this variable on the gradle.ext
inside the settings.gradle
.
gradle.ext.useRepo = System.getenv()["SA_IGNORE_REPO_CFG"] == null
and then use it.
buildscript {
gradle.ext.useRepo && repositories {
maven.url 'http://localhost:8081/artifactory/lognet-release'
}
}
Upvotes: 1