I had a pressing need to get at TOMCAT_HOME to do a bit of deployment from a
build. So I wrote a task to get Execute to fetch all the environment
variables then turn them into properties like (from the debug trace) the
following -
environment.PROCESSOR_LEVEL -> 6
environment.PROCESSOR_REVISION -> 0502
environment.ProgramFiles -> C:\Program Files
environment.PROMPT -> $P$G
environment.SystemDrive -> C:
environment.SystemRoot -> C:\WINNT
environment.TEMP -> d:\temp
environment.TMP -> d:\temp

You can change the prefix if you want. The test build tries the default and
a specified prefix, prints out the value of ANT_HOME in both cases.

Enjoy,

-Steve
Title: Ant User Manual

Getenv

Description

Turns all system environment variables into ant properties.

This task lets you import new environment variables into your build process, without having to modify the invocation scripts. When the task is called, all system variables are enumerated and saved as ant properties, each one prefixed by a prefix -"environment." is the default but this can be overridden.

It does not work on the Mac, because the task it depends on (Execute) can't extract environment variables from the Mac either.

Note that the case of environment variables is the same as whatever the underlying operating system chooses, so that the system path on unix would be stored as "environment.path" while on Windows NT it will be "environment.Path". Any reliance on such system properties will be inherently unportable.

Parameters

Attribute Description Required
prefix prefix string to use instead of "environment." No

Examples

  <getenv/>
  <echo message="TOMCAT_HOME =${environment.TOMCAT_HOME}"/>
Sets all environment variables to environment.* names, then prints out tomcat.home if it is present
  <getenv prefix="env-" /> 
	

Fetches the variables and prefixes them with env-, such that the value of TOMCAT_HOME would be stored in "env-TOMCAT_HOME"


Copyright © 2001 Apache Software Foundation. All rights Reserved.

Attachment: Getenv.java
Description: Binary data

<?xml version="1.0"?>
<!-- test file for getenv -->


<project name="test-getenv" default="test" basedir=".">
	<target name="init">
		<taskdef
		        name="getenv"
			classname="org.apache.tools.ant.taskdefs.Getenv" />
                <getenv/>
                <getenv prefix="env-vars." />
	</target>


	<target name="test" depends="init">
                <echo message="property environment.ANT_HOME =${environment.ANT_HOME}"/>
                <echo message="property env-vars.ANT_HOME =${env-vars.ANT_HOME}"/>
	</target>

</project>

Reply via email to