Passing environment variable to ant task, without ANT_OPTS - ant

I'm calling the Jasper ant task, and I want to set the org.apache.jasper.compiler.Parser.STRICT_QUOTE_ESCAPING environment variable. I can set ANT_OPTS to be -Dorg.apache.jasper.compiler.Parser.STRICT_QUOTE_ESCAPING=false and it works correctly. However, I want a setting I can put into the build.xml, so I don't need to tell my teammates that they need to set ANT_OPTS.
I've tried
<property name="env.org.apache.jasper.compiler.Parser.STRICT_QUOTE_ESCAPING"
value="false"/>
but that doesn't seem to work.
How can I pass an environment variable to an ant task?
EDIT: By "doesn't work", I mean I get an error saying an attribute is quoted with " which must be escaped when used within the value
If I set it via ANT_OPTS, I do not get this error.

Use the <property> task to define an environmental prefix:
<property environment="env"/>
Now, you can simply prepend env. to your environment variable and treat it like an already defined Ant property:
<property environment="env"/>
<echo message="My path is "${env.PATH}""/>

Related

Condition based on environment in Ant buildfile

I am new to Ant and any help will be appreciated.
What I want to do is:
<target name="run-tenantManagement" depends="jar"
description="Running TestNG Tests">
<if> ... SIMV3.1 env variable set
<property name="output.dir.name" value="tenantManagementV3"/>
<else>
<property name="output.dir.name" value="tenantManagement"/>
</if>
</target>
Basically I am setting one env variable
setenv SIMV3.1 true
I want to put a check in the Ant target if this variable is set and has value=true, then set output.dir.name to a particular value, else set it to another value.
Kindly help. I have googled through many Ant tutorials but everything seems ambiguous.
There's a few ways to do this, for example using an Ant <condition> task:
<property environment="env" />
<condition property="output.dir.name" value="tenantManagementV3"
else="tenantManagement">
<istrue value="${env.SIMV3.1}" />
</condition>
There are a number of other tests you can use and combine in the task, see Conditions for the list.
Also: I'd avoid using an environment variable name with an embedded dot - it won't work in all shells.

Replacing property dynamically based on environments in Ant scripts

I want to develop ant script which replaces application properties with environment specific properties. My requirement is that I will have all environment properties in single env.properties file. During building the application I need to replace with whatever in env.properties file. Ant replace works well when I have property files for each environment.
Sample : env.properties
dev.AddNETWORK_USER=devUser
dev.ADDPASS=devPass
sit.AddNETWORK_USER=situser
sit.ADDPASS=sitPass
This needs be replaced in mule.properties as
for DEV environment:
dev.AddNETWORK_USER=devUser
dev.ADDPASS=devPass
for SIT environment:
AddNETWORK_USER=sitUser
ADDPASS=sitPass
You can use property ant task on your env.properties file. This allows to access key=value pair based on your need. You can redirect your environment specific properties to file you want or write to one specific file.
<property file="${base.dir}/env.properties"/>
<for list="dev,sit" param="value">
<sequential>
<echo message="#{value}.AddNETWORK_USER=${#{value}.AddNETWORK_USER}" append="true" file="${base.dir}/#{value}Any-File.prop"/>
<echo message="${line.separator}" append="true" file="${para.home}/#{value}Any-File.prop"/>
<echo message="#{value}.ADDPASS=${#{value}.ADDPASS}" append="true" file="${para.home}/#{value}Any-File.prop"/>
</sequential>
</for>

Ant, set environmental variable for <ant> task?

I want to run another ant build file from ant, and the <ant> task looks perfect for this.
The other build file uses environmental variables for a couple things. Is there a way to set environmental variables for the child ant process?
I see that the <exec> task seems to support nested <env>s, but nothing similar for <ant>.
From the documentation of ant task,
By default, all of the properties of the current project will be available
in the new project.
You can also set properties in the new project from the old project by using
nested property tags.
<ant antfile="subproject/property_based_subbuild.xml">
<property environment="env"/>
</ant>
by default the inner ant call inherits the parent properties
<ant inheritAll="true" antfile="subproject/subbuild.xml">
<!--inheritAll="true" is default value, this is unecessary -->
<property name="myprop" value="foo"/>
<antcall target="myTarget"></antcall>
<ant antfile="myAntFile"></ant>
</ant>
In this case, "myTarget" and all targets on "myAntFile" can get "foo" as "myprop" value.

Sourcing a shell profile in an ant build file?

I am using cruisecontrol and ant to build some legacy executables that also depend on a shell profile to setup env vars properly. Is there a way to exec this profile using ant in the current process so the makefiles ant calls get the env vars correctly?
Another solution would be if there is a way to add the profile sourcing to the sub make files I'm calling.
Edit: I guess I wasn't clear in my question. I know what env varibles need to be passed to make using the exec/env tasks. However, I don't know how to have ant grab the values from a shell profile that is usually sourced via: . /usr/local/profile/foo.profile
I figured out how to do it based off of how ant itself sources env variables.
<exec executable="ksh" dir="${foo.dir}"
failonerror="true" output="${foo.dir}/env.properties">
<arg value="-c" />
<arg value=". /usr/local/profiles/profile.foo; set" />
</exec>
<property file="${foo.dir}/env.properties" prefix="env"/>
Further down I can then pass them to sub make calls using the exec tags. For example:
<exec executable="make" dir="${bar.dir}" failonerror="true">
<env key="ORACLE_HOME" value="${env.ORACLE_HOME}" />
</exec>
You will not be able to execute make in the current process.
Take a look at the ant <exec> task, use this to execute your make build. The environment variables will still be available for the make process, in fact you can turn this off explicitly with the newenvironment attribute. The following simple exec should retain all environment variables in make:
<exec executable="make" />
If you need extra environment variables, or want to maintain them through your ant build you can use them in the exec task by adding <env> elements like so:
<exec executable="make" >
<env key="ENV_KEY" value="ENV_VALUE"/>
</exec>

Can Ant expand environment variables from a properties file?

I have a question regarding Ant and its treatment of environment variables.
To illustrate I have a small sample.
Given the Ant build file test.xml:
<project name="myproj" default="testProps">
<property environment="env"/>
<target name="testProps">
<echo message="${env.MyEnvVar}"/>
<echo message="${MY_PROPERTY}"/>
</target>
</project>
And the properties file test.props:
MY_PROPERTY=${env.MyEnvVar}
Now set the environment variable MyEnvVar to some value (foo in my case) and run Ant using this command line:
ant -f test.xml -propertyfile test.props testProps
The output I get is:
[echo] foo
[echo] ${env.MyEnvVar}
What I would like to know is whether there is any way to structure the input properties file such that I get
[echo] foo
[echo] foo
That is, I would like to name an environment variable in the properties file which is replaced within the Ant script. Note - I know how to access environment variables directly (as is done here). What I need to do is make use of a set of Ant scripts that expect one collection of properties in an environment that defines the same properties using different names. Thus the thought of "bridging" them in a properties file.
I am using Ant version 1.6.5.
You need to read the test.props property file after the environment - you could do so using another property task, i.e. add
<property file="test.props" />
after your existing property environment task.
In full:
<property environment="env" />
<property file="test.props" />
<target name="testProps">
<echo message="${env.MyEnvVar}"/>
<echo message="${MY_PROPERTY}"/>
</target>
When you supply the properties file on the command line this gets processed before the content of the build, but at that time ${env.MyEnvVar} is not yet set.

Resources