Tuesday, 9 July 2019

Maven: Custom properties in pom.xml file


<properties> element is used to set the custom properties.

Ex
   <properties>
                  <junit.version>4.12</junit.version>
   </properties>
  
As you see above example, "junit.version" is a custom property which has value 4.12.

You can specify any number of custom properties inside <properties> element.

Ex
   <properties>
                  <junit.version>4.12</junit.version>
                  <runningEnvironment>Dev</runningEnvironment>
   </properties>
  
How to access custom properties?
The property 'junit.version' is accessed with ${junit.version}

pom.xml
<project>
   <modelVersion>4.0.0</modelVersion>
   <groupId>selfLearningJava</groupId>
   <artifactId>helloworld</artifactId>
   <packaging>jar</packaging>
   <version>1</version>
   <name>helloworld</name>
   
   <build>
  <finalName>${project.groupId}-${project.artifactId}-${project.version}</finalName>
   </build>
   
   <dependencies>
   
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>${junit.version}</version>
   <scope>test</scope>
  </dependency>
   
   </dependencies>
   
   <properties>
  <junit.version>4.12</junit.version>
   </properties>
   
</project>

Go to the location, where this pom file is located and run “mvn help:effective-pom” command to see how the custom variable “junit.version” is replaced at run time.

When I see the output of “mvn help:effective-pom” command, dependencies are changed like below.


  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
  </dependencies>



Previous                                                    Next                                                    Home

No comments:

Post a Comment