Set system properties in Ant build script : Environment « Ant « Java

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 name="compile">
    <javac srcdir="." destdir=".">
      <classpath path="."/>

  <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}"/>



Related examples in the same category

1.Environment: ant home, OS and Processor
2.Get environment variables