Java/Ant/Environment
Environment: ant home, OS and Processor
<?xml version="1.0"?>
<project name="Apache Ant Properties Project" default="properties.environment" basedir=".">
<target name="properties.environment">
<property environment="env"/>
<echo message="Built on: ${env.OS} ${env.PROCESSOR_ARCHITECTURE}"/>
<echo message="ANT_HOME: ${env.ant_home}"/>
</target>
</project>
Get environment variables
<?xml version="1.0"?>
<project name="Template Buildfile" default="compile" basedir=".">
<property name="dir.src" value="src"/>
<property name="dir.build" value="build"/>
<property environment="env"/>
<target name="checkProperties">
<fail unless="env.TOMCAT_HOME">TOMCAT_HOME must be set</fail>
<fail unless="env.JUNIT_HOME">JUNIT_HOME must be set</fail>
<fail unless="env.JBOSS_HOME">JBOSS_HOME must be set</fail>
</target>
<!-- Creates the output directories -->
<target name="prepare" depends="checkProperties">
<mkdir dir="${dir.build}"/>
</target>
<target name="clean"
description="Remove all generated files.">
<delete dir="${dir.build}"/>
</target>
<target name="compile" depends="prepare"
description="Compile all source code.">
<echo>Compile code...</echo>
</target>
</project>
Set system properties in Ant build script
public class ShowProps {
public static void main(String[] args) {
System.out.println("Now in ShowProps class...");
System.out.println("prop1 = " + System.getProperty("prop1"));
System.out.println("prop2 = " + System.getProperty("prop2"));
System.out.println("prop3 = " + System.getProperty("prop3"));
System.out.println("user.home = " + System.getProperty("user.home"));
}
}
-----------------------------------------
File: build.xml
<?xml version="1.0"?>
<project name="sysprops" default="run" basedir=".">
<property name="prop1" value="Property 1 from Buildfile"/>
<property name="prop2" value="Property 2 from Buildfile"/>
<target name="clean">
<delete dir="com"/>
</target>
<target name="compile">
<javac srcdir="." destdir=".">
<classpath path="."/>
</javac>
</target>
<target name="run" depends="compile">
<echo message="Now in buildfile..."/>
<echo message="prop1 = ${prop1}"/>
<echo message="prop2 = ${prop2}"/>
<echo message="user.home = ${user.home}"/>
<!-- execute the main() method in a Java class -->
<java classname="ShowProps">
<classpath path="."/>
<!-- pass one of the properties -->
<sysproperty key="prop1" value="${prop1}"/>
</java>
</target>
</project>