Getting null value while accessing enviornment variable using build.gradle

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

You are confusing system properties with environment variables.
Those two are totally different things that have nothing to do with each other.
You are setting system properties for you test task in your build script but try to read them as environment variable in your test code, that cannot work.
Either set it as environment variable for the test task or read it as system property.

What more help do you need?
I already told you what to do, didn’t I?

If your question is how to set environment variable, then a simple look in the documentation would have revealed that it is environment 'projectTitle', projectTitle.

Btw. I highly recommend using Kotlin DSL instead of Groovy DSL, as you then right away have statically typed type-safe build scripts and amazing IDE support and code completion, at least if you use IntelliJ, not sure about support level in other IDEs as I would never use them.

Well, I already told you how to set an environment variable for the test task.
Actually while looking at your test code I’m not sure you are really trying to read an environment variable, as I have no idea what your SystemEnvironmentVariables class does.
But that is not a Gradle topic at all tbh. :slight_smile: