Make delicious recipes!

Maven profiles and property substitution example


Maven profiles provide a way to define properties that can be selected from the command-line by just passing in the profile id.
We will see an example for the same to understand it better and also see how the properties can be substituted by maven configuration.

The example has the following files:
./pom.xml
./src/test/java/prismoskills/TestConfiguration.java
./src/test/resources/configuration.properties

  1. src/test/java/prismoskills/TestConfiguration.java
    (This one just loads the configuration.properties file and prints it out).
    package prismoskills;
    
    import java.net.URL;
    
    import org.junit.Test;
    
    import com.google.common.base.Charsets;
    import com.google.common.io.Resources;
    
    public class TestConfiguration {
    
      @Test
      public void test() throws Exception {
          URL url = Resources.getResource("configuration.properties");
          String text = Resources.toString(url, Charsets.UTF_8);
          System.out.println("Properties read: \n" + text);
      }
    }
    



  2. src/test/resources/configuration.properties
    (This is the properties file where we will substitute the properties with values from pom.xml)
    prop1=${prop1.val}
    prop2=${prop2.val}
    


  3. pom.xml
    (This is the place where all the configuration for profiles and property substitution is there. So pay attention to this one.)
    <?xml version="1.0" encoding="UTF-8"?>
    <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">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>prismoskills</groupId>
      <artifactId>property-substitution-example</artifactId>
      <version>1.0.0</version>
    
      <dependencies>
        <dependency>
          <groupId>com.google.guava</groupId>
          <artifactId>guava</artifactId>
          <version>19.0</version>
        </dependency>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.12</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    
      <!-- Declare two maven profiles here -->
      <profiles>
        <profile>
          <id>profile1</id>
          <properties>
            <prop1.val>1</prop1.val>
            <prop2.val>2</prop2.val>
          </properties>
        </profile>
        <profile>
          <id>profile2</id>
          <properties>
            <prop1.val>One</prop1.val>
            <prop2.val>Two</prop2.val>
          </properties>
        </profile>
      </profiles>
    
      <!-- Add configuration for property substitution -->
      <build>
        <testOutputDirectory>${basedir}/target/classes</testOutputDirectory>
        <filters>
          <filter>src/test/resources/configuration.properties</filter>
        </filters>
        <resources>
          <resource>
           <directory>src/test/resources</directory>
           <filtering>true</filtering>
          </resource>
        </resources>
      </build>
    
    </project>
    



The profiles can be activated using the -P flag.
Example:
  1. mvn clean package
    
    -------------------------------------------------------
     T E S T S
    -------------------------------------------------------
    Running prismoskills.TestConfiguration
    Properties read:
    prop1=${prop1.val}
    prop2=${prop2.val}
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.176 sec
    
  2. mvn clean -Pprofile1 package
    
    -------------------------------------------------------
     T E S T S
    -------------------------------------------------------
    Running prismoskills.TestConfiguration
    Properties read:
    prop1=1
    prop2=2
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.175 sec
    
  3. mvn clean -Pprofile2 package
    
    -------------------------------------------------------
     T E S T S
    -------------------------------------------------------
    Running prismoskills.TestConfiguration
    Properties read:
    prop1=One
    prop2=Two
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.169 sec
    










Like us on Facebook to remain in touch
with the latest in technology and tutorials!


Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Name:
Email: (Your email is not shared with anybody)
Comment:

Facebook comments:

Site Owner: Sachin Goyal