views:

193

answers:

2

We need to test a java build with the languages set to different values. I can manually (i.e. via export LANG=en_DK.UTF-8 and export LANG=en_DK) test that the unit tests run with the ant build script behaves differently, but I need to set the environment variable from ant. I have tried setting it using these methods (with the shell $LANG set to en_DK.UTF-8):

  1. using -D on the command line: ant -DLANG=en_DK
  2. using a build.properties file with the line LANG=en_DK in it
  3. using the following statements in the build.xml file (sorry for the formatting, I can't get SO to display it otherwise):

:

<property environment="ANTENV"/>
<property name="ANTENV.LANG" value="en_DK"/>

Using any of the three possibilities, and when run with -debug, ant reports that:

Override ignored for property "LANG"

What can I do to set the LANG environment variable from within ant?

+1  A: 

ANT Properties are immutable,

<property name="ANTENV.LANG" value="en_DK"/>

may be be interpreted by ant as an attempt to override the LANG value already present when storing all the environment variables in ANTENV (with <property environment="ANTENV"/>).

So you need to store to store that value in a separate property.

<property name="MY.LANG" value="${env.LANG}" />
VonC
But I don't really need to store the value; I need to set it so that the JVM, when executing the tests, can use either the value en_DK or en_DK.UTF-8 as LANG. I acknowledge that the ant properties are immutable, I would just like to meddle with the environment variables from within ant...
Steen
@Steen: "I would just like to meddle with the environment variables from within ant": that is what `<property name="MY.LANG" value="${env.LANG}" />` allows you to do: once seet, you can reuse it within other tasks. What you cannot do is to change something set outside of the Ant session (like an environment value)
VonC
@VonC: OK, that qualifies as an answer to my question. Not the one I really wanted to hear, but that's my problem. I've experimented with wrapping the call to the build-file in a shell-script that sets the LANG environment before calling ant. I works fine, but feels like a hack ;)
Steen
@Steen: Also, you could try to set value to `ANTENV.LANG` before defining environment.
cubanacan
A: 

Assuming that your "test a java build" is done by a call to <java>, you could use the fork-flag and pass properties to the newly created process. Here's an example from the ant documentation:

  <java classname="test.Main"
        fork="yes" >
    <sysproperty key="DEBUG" value="true"/>
    <arg value="-h"/>
    <jvmarg value="-Xrunhprof:cpu=samples,file=log.txt,depth=3"/>
  </java>
pitpod
Thanks for the suggestion. As noted on VonC's answer I reach my purpose through a shell script wrapper. Your solution amounts to the same effect, but it also (like my shell script wrapper solution) feels hacky. Thanks for you answer, though.
Steen