title: How to read system property from maven in java
body: <p>I am working on a web application which is running on tomcat server.
  I have different properties files for the different environment, So I want to read my environment variable from pom file in java file and set that environment property when running command <code>mvn clean install</code>.</p>
  
  <p>Here is my pom file:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;marketingcenter&lt;/groupId&gt;
      &lt;artifactId&gt;marketingcenter&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;packaging&gt;war&lt;/packaging&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.ws.rs&lt;/groupId&gt;
              &lt;artifactId&gt;jsr311-api&lt;/artifactId&gt;
              &lt;version&gt;1.1.1&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;com.sun.jersey&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-server&lt;/artifactId&gt;
              &lt;version&gt;1.8&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- https://mvnrepository.com/artifact/log4j/log4j --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;log4j&lt;/groupId&gt;
              &lt;artifactId&gt;log4j&lt;/artifactId&gt;
              &lt;version&gt;1.2.17&lt;/version&gt;
          &lt;/dependency&gt;
  
  
      &lt;/dependencies&gt;
  
      &lt;profiles&gt;
          &lt;!-- The configuration of the development profile --&gt;
          &lt;profile&gt;
              &lt;id&gt;dev&lt;/id&gt;
              &lt;!-- The development profile is active by default --&gt;
              &lt;activation&gt;
                  &lt;activeByDefault&gt;true&lt;/activeByDefault&gt;
              &lt;/activation&gt;
              &lt;properties&gt;
                  &lt;environment.id&gt;environment_dev&lt;/environment.id&gt;
              &lt;/properties&gt;
          &lt;/profile&gt;
          &lt;!-- The configuration of the production profile --&gt;
          &lt;profile&gt;
              &lt;id&gt;prod&lt;/id&gt;
              &lt;activation&gt;
                  &lt;activeByDefault&gt;false&lt;/activeByDefault&gt;
              &lt;/activation&gt;
              &lt;properties&gt;
                  &lt;environment.id&gt;environment_prod&lt;/environment.id&gt;
              &lt;/properties&gt;
          &lt;/profile&gt;
          &lt;!-- The configuration of the testing profile --&gt;
          &lt;profile&gt;
              &lt;id&gt;qa&lt;/id&gt;
              &lt;activation&gt;
                  &lt;activeByDefault&gt;false&lt;/activeByDefault&gt;
              &lt;/activation&gt;
              &lt;properties&gt;
                  &lt;environment.id&gt;environment_qa&lt;/environment.id&gt;
              &lt;/properties&gt;
          &lt;/profile&gt;
          &lt;profile&gt;
              &lt;id&gt;reg&lt;/id&gt;
              &lt;activation&gt;
                  &lt;activeByDefault&gt;false&lt;/activeByDefault&gt;
              &lt;/activation&gt;
              &lt;properties&gt;
                  &lt;environment.id&gt;environment_reg&lt;/environment.id&gt;
              &lt;/properties&gt;
          &lt;/profile&gt;
      &lt;/profiles&gt;
      &lt;build&gt;
          &lt;sourceDirectory&gt;src&lt;/sourceDirectory&gt;
          &lt;resources&gt;
              &lt;resource&gt;
                  &lt;directory&gt;src&lt;/directory&gt;
                  &lt;excludes&gt;
                      &lt;exclude&gt;**/*.java&lt;/exclude&gt;
                  &lt;/excludes&gt;
              &lt;/resource&gt;
          &lt;/resources&gt;
          &lt;pluginManagement&gt;
              &lt;plugins&gt;
                  &lt;plugin&gt;
                      &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                      &lt;version&gt;3.7.0&lt;/version&gt;
                      &lt;configuration&gt;
                          &lt;source&gt;1.8&lt;/source&gt;
                          &lt;target&gt;1.8&lt;/target&gt;
                      &lt;/configuration&gt;
                  &lt;/plugin&gt;
                  &lt;plugin&gt;
                      &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
                      &lt;version&gt;3.0.0&lt;/version&gt;
                      &lt;configuration&gt;
                          &lt;warSourceDirectory&gt;WebContent&lt;/warSourceDirectory&gt;
                      &lt;/configuration&gt;
                  &lt;/plugin&gt;
                  &lt;!-- &lt;plugin&gt;
               &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
               &lt;artifactId&gt;properties-maven-plugin&lt;/artifactId&gt;
               &lt;version&gt;1.0-alpha-2&lt;/version&gt;
               &lt;executions&gt;
                   &lt;execution&gt;
                       any phase before your app deploys
                       &lt;phase&gt;prepare-package&lt;/phase&gt;
                       &lt;goals&gt;
                           &lt;goal&gt;set-system-properties&lt;/goal&gt;
                       &lt;/goals&gt;
                       &lt;configuration&gt;
                           &lt;properties&gt;
                               &lt;property&gt;
                                   &lt;name&gt;environment&lt;/name&gt;
                                   &lt;value&gt;environment_dev&lt;/value&gt;
                               &lt;/property&gt;
                           &lt;/properties&gt;
                       &lt;/configuration&gt;
                   &lt;/execution&gt;
               &lt;/executions&gt;
           &lt;/plugin&gt; --&gt;
  
  
                  &lt;!--   &lt;plugin&gt;
                         &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
                         &lt;artifactId&gt;exec-maven-plugin&lt;/artifactId&gt;
                         &lt;version&gt;1.3.2&lt;/version&gt;
                         &lt;executions&gt;
                             &lt;execution&gt;
                                 &lt;phase&gt;package&lt;/phase&gt;
                                 &lt;goals&gt;
                                     &lt;goal&gt;java&lt;/goal&gt;
                                 &lt;/goals&gt;
                             &lt;/execution&gt;
                         &lt;/executions&gt;
                         &lt;configuration&gt;
                             &lt;mainClass&gt;com.keurig.config.DBUtils&lt;/mainClass&gt;
                             &lt;arguments&gt;
                                 &lt;argument&gt;argument1&lt;/argument&gt;
                             &lt;/arguments&gt;
                             &lt;systemProperties&gt;
                                 &lt;systemProperty&gt;
                                     &lt;key&gt;environment&lt;/key&gt;
                                     &lt;value&gt;environment_dev&lt;/value&gt;
                                 &lt;/systemProperty&gt;
                             &lt;/systemProperties&gt;
                         &lt;/configuration&gt;
                         &lt;/plugin&gt;
                          --&gt;
                  &lt;plugin&gt;
                      &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                      &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
                      &lt;version&gt;2.21.0&lt;/version&gt;
                      &lt;configuration&gt;
                          &lt;systemPropertyVariables&gt;
                              &lt;environment&gt;environment_dev&lt;/environment&gt;
                          &lt;/systemPropertyVariables&gt;
                      &lt;/configuration&gt;
                  &lt;/plugin&gt;
  
              &lt;/plugins&gt;
          &lt;/pluginManagement&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>I have tried multiple things which are commented but all are giving same problem null value.
  And I am reading environment property by following code which is in DBUtils class-</p>
  
  <pre class="lang-java prettyprint-override"><code>public class DBUtils {
      private static final Logger LOG=Logger.getLogger(DBUtils.class);
  
      public static Connection getConnection() throws SQLException {
          Connection connection = null;
  
          String str =System.getProperty("environment");
          System.out.println("deven"+str);
  
  
          //static Properties _config = PropertyLoader.loadProperties(System.getProperty("environment"));
          Properties prop = PropertyLoader.loadProperties(str);
          String url = prop.getProperty("databaseUrl");
          String driverName = prop.getProperty("databaseDriver");
      }
  }
  </code></pre>
  
  <p>but I am getting null every time.
  this is the result-</p>
  
  <pre><code>devennull
  devennull
  2018-04-20 12:52:22,424 [http-nio-8080-exec-8] ERROR
  java.lang.IllegalArgumentException: null input: name
          at com.app.data.PropertyLoader.loadProperties(PropertyLoader.java:51)
          at com.app.data.PropertyLoader.loadProperties(PropertyLoader.java:128)
          at com.app.data.DBUtils.getConnection(DBUtils.java:23)
  </code></pre>