MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

Apache Ant Properties: Access Java System Properties, Environment Variables, and Runtime Inputs

Originally published on 2009-06-20; fully revised and updated.

If you use Apache Ant, property expansion via ${name} is the core mechanism for build-time configuration. This guide shows how to read Java system properties directly, import environment variables, pass values with -D, and use interactive input only where it makes sense.

This revised article expands the original quick tip from DevOpsSchool into a practical reference.

Java system properties

Property sources in Ant at a glance

In Ant build files, you reference a property as ${property.name}. Values can come from multiple sources during a build run.

SourceReference styleHow it is setTypical use
Java system property${user.home}Provided by the JVM running AntRuntime and host metadata
Environment variable${env.PATH}Imported with the Ant property task using an environment prefixOS-level paths and machine config
Command-line property${deploy.env}ant -Ddeploy.env=prodCI/CD and per-run overrides
Interactive input${release.version}Set with Ant input taskLocal/manual runs

Read Java system properties directly

You can use common JVM properties directly in your build file without extra setup.

  • ${user.name}
  • ${user.dir}
  • ${user.home}
  • ${java.version}
<project name='sys-props-demo' default='show-system-props'>
    <target name='show-system-props'>
        <echo message='user.name = ${user.name}'/>
        <echo message='user.dir  = ${user.dir}'/>
        <echo message='user.home = ${user.home}'/>
        <echo message='java.version = ${java.version}'/>
    </target>
</project>Code language: HTML, XML (xml)

Import environment variables with a prefix

To expose OS environment variables in Ant properties, use the property task with an environment prefix (for example, env). Then reference values as ${env.VARIABLE_NAME}.

<project name='env-demo' default='show-env'>
    <target name='show-env'>
        <property environment='env'/>
        <echo message='PATH = ${env.PATH}'/>
        <echo message='HOME = ${env.HOME}'/>
        <echo message='USERPROFILE = ${env.USERPROFILE}'/>
    </target>
</project>Code language: HTML, XML (xml)

Pass properties at runtime with -D

For pipeline-friendly builds, pass values at invocation time so you can reuse the same build file across environments.

ant -Ddeploy.env=staging -Dbuild.number=123 package
ant -Ddeploy.env=prod deploy

Inside build.xml, consume those runtime values with normal property syntax.

<target name='package'>
    <echo message='Deploy environment: ${deploy.env}'/>
    <echo message='Build number: ${build.number}'/>
</target>Code language: HTML, XML (xml)

Prompt for input during local runs

Ant can prompt for ad hoc input. This is useful for local/manual execution, but usually unsuitable for non-interactive CI jobs.

<target name='ask-version'>
    <input addproperty='release.version' message='Enter release version'/>
    <echo message='Release version: ${release.version}'/>
</target>Code language: HTML, XML (xml)

Property resolution workflow

flowchart TD
    A[Task needs a property] --> B{Set by D argument}
    B -- yes --> C[Use command line value]
    B -- no --> D{Set in build or property file}
    D -- yes --> E[Use first assigned value]
    D -- no --> F{Available as Java system property}
    F -- yes --> G[Use system property]
    F -- no --> H{Imported from env prefix}
    H -- yes --> I[Use env prefixed value]
    H -- no --> J[Optional input or fail]

Best practices and common pitfalls

  • Use -D properties for CI/CD and environment-specific overrides.
  • Import environment variables with a clear prefix (for example, env) to avoid naming collisions.
  • Avoid interactive input tasks in automated pipelines.
  • Do not echo sensitive values (tokens, passwords, keys) into logs.
  • Keep default values in the build file and override only what is needed per run.

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

ANT_OPTS in Apache Ant: JVM Memory, Proxy Settings, and Startup Troubleshooting

ANT_OPTS passes JVM options to the Java process that runs Ant. This guide explains how ANT_OPTS differs from ANT_ARGS and JAVACMD, with practical examples for memory tuning, proxy setup, and startup error diagnosis.

Read More

Apache Ant: Set the Current User Home Directory with env.HOME (and Safe Fallbacks)

Learn how to read the logged-in user’s home directory in Ant using env.HOME, then make it portable with a user.home fallback. Includes build.xml patterns you can use in local builds and CI.

Read More

HCP Terraform (Terraform Cloud) — Complete Guide to Variables, Scope & Precedence

Updated: October 2026 | Based on current HashiCorp documentation HCP Terraform provides several ways to pass values to Terraform code. These include variable defaults, .tfvars files, environment…

Read More

How to Configure HTTP/HTTPS Proxy on Linux and Windows (CLI, GUI, Registry, and APT)

Set up proxy access correctly on Windows and Linux for command-line tools, package managers, and system-level use. This guide covers quick commands, persistent configuration, and common troubleshooting steps.

Read More

AWS CLI Credentials Setup: Configure, Verify, and Secure Access

Learn how to configure AWS credentials with the AWS CLI, verify your identity, and understand where credentials are stored. This updated guide also compares profiles, environment variables, and role-based access.

Read More

Jenkins Build and Release Training Blueprint: Git/SVN with Maven and Ant

This revised guide turns a legacy training announcement into a practical learning blueprint for build-and-release engineering. It maps the Jenkins workflow, compares core tools, and includes realistic command and pipeline examples.

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x