Pages

Monday, June 7, 2010

Maven Property References

Share it Please
 
In a daily application, properties play key roles in providing data at required time. In maven also we have a facility to set properties and access them at required places. We can access system, operating system specific properties also.

Maven properties are like place holders, they can be accessed anywhere in the POM file and can be accessed using ${x}, where x is a property to be accessed.

So in this article we will see how to access system and also user defined properties in Maven by creating a sample application,

First create a sample application

mvn archetype: generate -DgroupId=com.myCompany.VerySample -DartifactId= VerySample -Dpackage=com.myCompany.VerySample -Dversion=1.0-SNAPSHOT

And here are the contents of the POM file,

<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>com.myCompany.VerySample</groupId>
  <artifactId>VerySample</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>VerySample</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

So now we will create some properties in the POM file and access them. In order to set a property in maven we will use the property element as
I just added the myName property as

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   <myName>jagadesh</myName>
  </properties>


So now we want to see the value defined for the myName property. How are we going to do this, for this we will use the maven-antrun plugin.we can use the echo of Ant to display the value of the property.

Maven AntRun Plug-in
Maven AntRun plug-in allows us to run ant tasks in Maven. This plug-in was introduced mainly to provide some help in migrating from Ant based projects to Maven.

So we will add the plug-in to the POM file like

<build>
            <plugins>
            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.1</version>
            <executions>
                <execution>
                        <phase>validate</phase>
                            <goals>
                                    <goal>run</goal>
                             </goals>
                             <configuration>
                                    <tasks>
                                    <echo>Displaying value of 'myName' property</echo>
                                    <echo>[myName] ${myName}</echo>
                                    </tasks>
                            </configuration>
               </execution>
            </executions>
            </plugin>
            </plugins>
</build>


So we added a plug-in to the POM file and now we will now run a life cycle phase to test the property of the myName.so now we will execute the validate phase

“mvn validate” – since we are telling the antRun plug-in to execute at validate phase [see phase in execution]

We will see the following output

[INFO] [antrun:run {execution: default}]
[INFO] Executing tasks
     [echo] Displaying value of 'myName' property
     [echo] [myName] jagadesh
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL


We can the value of the myName displayed as “jagadesh”.

If we execute the command “mvn help:effective-pom” also we can see the value of the property defined .

Maven provides 3 variables by which we can access the maven properties

env – env variable is used to access the properties defined by the operating system or by shell like env.PATH .

project – this variable is used to access various information regarding the project like
project-artifactId , project-groupId.

Settings – this variable is used to access maven setting information like setting. activeProfile.

In addition to the above we can also access the system properties like java.HOME to access java home, os.name.

We have a properties plug-in for maven which reads the properties defined in the text file, refer - http://mojo.codehaus.org/properties-maven-plugin/

Here are some of the links about maven properties,

Some more articles coming, So Happy Coding…..










No comments :

Post a Comment