I am trying to set the environment variable using gradle but when I am trying to access it I am getting a null value.
I am trying to pass the project name from the command line and iterating through the list.
command :
./gradlew platform
build.gradle
ext {
projectList = [
"platform": "platform",
"legacy":"legacy"
]
}
projectList.each {
projectTitle, tags ->
tasks.create(name: "${projectTitle}", type: Test) {
outputs.upToDateWhen { false }
ignoreFailures = true
finalizedBy(aggregate)
group = "Environments"
doFirst {
project.logger.lifecycle("Target project: ${projectTitle}")
project.setProperty("projectTitle", "${projectTitle}")
project.setProperty("projectTitle", "${projectTitle}")
systemProperties.put("projectTitle", "${projectTitle}")
systemProperties System.properties
if(System.properties.getProperty("${projectTitle}"))
{
project.logger.lifecycle("Running tests against: ${projectTitle}")
}
}
}
}
gradle.properties
projectTitle = false
Code for accessig environment variable:
private static final String project = setTargetProject();
private static String setTargetProject() {
EnvironmentVariables environmentVariables = SystemEnvironmentVariables.createEnvironmentVariables();
Map<String, String> res = environmentVariables.asMap();
String name = environmentVariables.simpleSystemPropertiesAsMap().get("projectTitle");
try {
if (project != null) {
return name;
} else throw new Exception("Project is null, it shouldn't be null");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String getTargetProject() {
return project;
}