title: GRADLE: TestNg - unable to pass -D parameter to java code
body: <p>I have a Test framework written in Java utilizing TestNg, and built using gradle.  I want to pass an 'environment' argument at the command line, which will be used in an Environment class to define domain and server values for specific environments.  Using the Gradle documentation and several forum discussions on this issue, I am still unable to get the argument passed as a system property to the framework.  I have tried to accomplish this from the 'test' task AND the 'debug' task, to NO avail.  A 'println' statement in the task shows that the -Denv parameter is being picked up by gradle, but my attempt to add it to the System properties for the test framework fails.</p>
  
  <p>Execution results in a null reference, with environmentName being set to null.</p>
  
  <p>Can someone identify my error?</p>
  
  <p>command line</p>
  
  <pre><code>.\gradlew debug -Denv=production
  </code></pre>
  
  <p>build.gradle</p>
  
  <pre><code>test {
      def environment = System.properties["env"]
      println environment
      systemProperties = System.getProperties()
      systemProperties['env'] = environment
      useTestNG()
  }
  
  task debug(type: Test) {
  //    def environment = System.properties["env"]
  //    println environment
  //    systemProperties = System.getProperties()
  //    systemProperties['env'] = environment
      def groupsToInclude = []
      def groupsToExclude = []
  
      groupsToInclude.add('under_development')
      useTestNG() {
          groupsToInclude.each { String group -&gt; includeGroups group }
          groupsToExclude.each { String group -&gt; excludeGroups group }
      }
  }
  </code></pre>
  
  <p>java class</p>
  
  <pre><code>import com.sun.javafx.runtime.SystemProperties;
  
  public class Environment {
  
      private static EnvironmentDefinition environment;
  
      private Environment() {
      }
  
      public static EnvironmentDefinition getInstance() {
          if (environment == null) {
              String environmentName = SystemProperties.getProperty("env");
              // environmentName = environmentName == null ? "production" : environmentName;
              switch (environmentName) {
                  default:
                      Environment.environment = ProductionEnvironment.getInstance();
              }
          }
          return environment;
      }
  }
  </code></pre>