How do I display an environment variable?
Author: Deron Eriksson
Description: This tutorial describes how to display an environment variable.
Tutorial created using:
Windows Vista || JDK 1.6.0_04 || Eclipse Web Tools Platform 2.0.1 (Eclipse 3.3.1)
With mavenSW, an environment variable is treated as a property, where the property name begins with "env.". Thus, an environment variable such as %M2_HOME% can be accessed via ${env.M2_HOME}. The maven-antrun-plugin:run goal can be used to run AntSW tasks. We can use the "echo" Ant task to display a value of a property. The following fragment of a pom.xml file binds the antrun:goal to the validate build lifecycle phase. It then uses the "echo" Ant task to display the values of 3 environment variables: COMPUTERNAME, M2_HOME, and Path. Fragment of pom.xml to display values of 3 environment variables<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 3 environment variables</echo> <echo>[COMPUTERNAME] ${env.COMPUTERNAME}</echo> <echo>[M2_HOME] ${env.M2_HOME}</echo> <echo>[Path] ${env.Path}</echo> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> Performing a "mvn validate" on my "mytest" project generates the following console output: Console output of 'mvn validate' on 'mytest' project[INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] Building mytest [INFO] task-segment: [validate] [INFO] ------------------------------------------------------------------------ [INFO] [antrun:run {execution: default}] [INFO] Executing tasks [echo] Displaying 3 environment variables [echo] [COMPUTERNAME] YOSEMITE [echo] [M2_HOME] C:\dev\apache-maven-2.0.8 [echo] [Path] C:\dev\jdk1.6.0_04\bin\..\jre\bin\client;C:\dev\jdk1.6.0_04\bin\..\jre\bin;C:\dev\cygwin\bin;C:\dev\jdk1.6.0_04\bin;C:\dev\apache-maven-2.0.8\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\hp\bin\Python;c:\Program Files\Common Files\Roxio Shared\DLLShared\;c:\Program Files\Common Files\Roxio Shared\9.0\DLLShared\ [INFO] Executed tasks [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: < 1 second [INFO] Finished at: Mon Feb 11 12:38:15 PST 2008 [INFO] Final Memory: 2M/4M [INFO] ------------------------------------------------------------------------ As you can see, the values of the 3 environment variables are displayed in the console output. The "mytest" project's full pom.xml file is shown here. pom.xml<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.maventest</groupId> <artifactId>mytest</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>mytest</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.3</version> <scope>compile</scope> </dependency> </dependencies> <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 3 environment variables</echo> <echo>[COMPUTERNAME] ${env.COMPUTERNAME}</echo> <echo>[M2_HOME] ${env.M2_HOME}</echo> <echo>[Path] ${env.Path}</echo> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </project> |