Eventually three further solutions based on Flaka:

1. Using Flaka's Expression Language (EL) ..

<project xmlns:f="antlib:it.haefelinger.flaka">
  <f:install-property-handler />
  <property environment="env"/>
  <property name="JOF" value="#{ '${env.BUILD_BOX}'.tolower == 'true'
? 'F:/Inetpub/wwwroot/wo30' : 'C:/Inetpub/wwwroot/wo30' }" />
  <echo>${JOF}</echo>
</project>

$ BUILD_BOX=tRue ant -lib ~/lib/flaka/ant-flaka-1.02.01.jar
     [echo] F:/Inetpub/wwwroot/wo30

$ BUILD_BOX=false ant -lib ~/lib/flaka/ant-flaka-1.02.01.jar
     [echo] C:/Inetpub/wwwroot/wo30


2. Using Flaka's switch task ..

<project xmlns:f="antlib:it.haefelinger.flaka">
  <property environment="env"/>
  <f:switch value="${env.BUILD_BOX}">
    <re expr="true" ignorecase="true">
      <property name="JOF" value="F:/Inetpub/wwwroot/wo30" />
    </re>
    <otherwise>
      <property name="JOF" value="C:/Inetpub/wwwroot/wo30" />
    </otherwise>
  </f:switch>

  <echo>${JOF}</echo>
</project>

Here the value of environment variable BUILD_BOX will be matched using
regular expressions (<re />). This task would be suitable for carrying
out more complex actions than just setting a property.

3. Using Flaka's choose task ..

<project xmlns:f="antlib:it.haefelinger.flaka">
  <f:install-property-handler />
  <property environment="env"/>
  <f:choose>
    <when test=" '${env.BUILD_BOX}'.tolower == 'true'">
      <property name="JOF" value="F:/Inetpub/wwwroot/wo30" />
    </when>
    <otherwise>
      <property name="JOF" value="C:/Inetpub/wwwroot/wo30" />
    </otherwise>
  </f:choose>
  <echo>${JOF}</echo>
</project>

Similar to <switch /> task, just without power of regular (or glob)
pattern matching.


And, of course, there are <when /> and <unless /> ..

// Wolfgang


On Wed, Jun 8, 2011 at 3:01 PM, wolfgang haefelinger
<whaefelin...@gmail.com> wrote:
> Hi,
>
>>>    <condition property="JOF" value="F:/Inetpub/wwwroot/wo30">
>>>      <isset property="env.BUILD_BOX"/>
>>>    </condition>
>
> This would also enable the "build system settings" if environment
> variable BUILD_BOX is set to "false". Would be better to check
> existence and then variable's content as well - just to avoid any
> confusion.
>
> A second thought on maintaining such a script later on - you would
> move "F:/Inetpub/wwwroot/wo30" into a property cause you don't want to
> have such hard-wired paths in your build scripts, do you?
>
> Perhaps you should drop the idear of depending on environment
> variables and depend on (property) files instead, for example:
>
> <project>
>  <property file="buildsystem.properties" />
>  <property file="common.properties" />
>  <echo>JOF=${JOF}</echo>
> </project>
>
> Property file "buildsystem.properties" would only exist on
> build-servers while "common.properties" is assumed to exist on
> developer and build-server hosts. Then
>
> == buildserver.properties ==
> JOF = F:/Inetpub/wwwroot/wo30
>
> == common.properties ==
> JOF=C:/Inetpub/wwwroot/wo30
>
> Notice that <property /> graciously ignores non-existent files. You
> may also want to put those <property /> tasks  directly below <project
> /> to ensure that your properties are set before *any* target is
> executed. Btw, you should also assert that property JOF is set to
> avoid unexpected script behavior.
>
> // Wolfgang
>
>
>
>
> On Mon, Jun 6, 2011 at 7:33 PM, Brian McCann <bmccan...@comcast.net> wrote:
>> Hi Robert,
>>
>> couple questions to make sure I'm following you right.
>>
>> 1. I'm currently setting the value of JOF in the build.userproperties file
>> to " JOF=C:/Inetpub/wwwroot/wo30"
>> and even if I try to run a conditional in the build.xml file < property
>> name="JOF" value="F:/Inetpub/wwwroot/wo30"/>
>> The value of JOF gets set in stone when build.xml runs? Build.properties
>> files always override build.xml?
>>
>>
>>
>> 2. Since I already have a file build.user.properties I could just set the
>> default value of JOF path in that file like <property name="JOF"
>> value="C:/Inetpub/wwwroot/wo30"/> which would be the state that the build
>> file is not being run on the build box?
>>
>> so your code would look like:
>>
>> <?xml version="1.0" encoding="utf-8"?>
>> <project name="plugin-rules" basedir="." default="setJOF">
>>  <target name="setJOF">
>>    <property environment="env"/>
>>    <echo>BUILD_BOX = ${env.BUILD_BOX}</echo>
>>    <condition property="JOF" value="F:/Inetpub/wwwroot/wo30">
>>      <isset property="env.BUILD_BOX"/>
>>    </condition>
>>    <echo>JOF = ${JOF}</echo>
>>
>>    <property file="build.user.properties" />
>>    <echo>After Property file, JOF = ${JOF}</echo>
>>  </target>
>>
>> </project>
>>
>> Thanks,
>> BPM
>>
>>
>> -----Original Message-----
>> From: Echlin, Robert [mailto:robert.ech...@windriver.com]
>> Sent: Monday, June 06, 2011 12:46 PM
>> To: Ant Users List
>> Subject: RE: How change variable based on environment variable value
>>
>> Hi Brian,
>> Try this code.
>> Note: you can't put the default value of JOF into a file that will be loaded
>> automatically when ant is run, such as (home)/.antrc,
>> as that would set JOF before your code executes. First set works, last set
>> fails.
>>
>> On the other hand, you could set "JOF_default" in a properties file that is
>> loaded before this code is run, then have an "else" clause in your condition
>> to use it when BUILD_BOX is not set in your environment:
>>  <condition property="JOF" value="F:/Inetpub/wwwroot/wo30"
>> else="${JOF_default}">
>>
>> Rob
>>
>> ------------ simplified code ----------
>> <?xml version="1.0" encoding="utf-8"?>
>> <project name="plugin-rules" basedir="." default="setJOF">
>>  <target name="setJOF">
>>    <property environment="env"/>
>>    <echo>BUILD_BOX = ${env.BUILD_BOX}</echo>
>>    <condition property="JOF" value="F:/Inetpub/wwwroot/wo30">
>>      <isset property="env.BUILD_BOX"/>
>>    </condition>
>>    <echo>JOF = ${JOF}</echo>
>>
>>    <property file="jof.properties" />
>>    <echo>After Property file, JOF = ${JOF}</echo>
>>  </target>
>>
>> </project>
>> ---------------- output of test run - no build-run ---------
>> C:\Documents and Settings\rechlin\My Documents\test\TEST-ant>set BUILD_BOX=
>>
>> C:\Documents and Settings\rechlin\My Documents\test\TEST-ant>echo
>> %BUILD_BOX%
>> %BUILD_BOX%
>>
>> C:\Documents and Settings\rechlin\My Documents\test\TEST-ant>ant -f
>> jof-test.xml
>> Buildfile: C:\Documents and Settings\rechlin\My
>> Documents\test\TEST-ant\jof-test.xml
>>
>> setJOF:
>>     [echo] BUILD_BOX = ${env.BUILD_BOX}
>>     [echo] JOF = ${JOF}
>>     [echo] After Property file, JOF = C:/Inetpub/wwwroot/wo30
>>
>> BUILD SUCCESSFUL
>> Total time: 0 seconds
>> ------------------------ output of test run with BUILD_RUN set in the
>> environment ---------
>> C:\Documents and Settings\rechlin\My Documents\test\TEST-ant>set
>> BUILD_BOX=true
>>
>> C:\Documents and Settings\rechlin\My Documents\test\TEST-ant>echo
>> %BUILD_BOX%
>> true
>>
>> C:\Documents and Settings\rechlin\My Documents\test\TEST-ant>ant -f
>> jof-test.xml
>> Buildfile: C:\Documents and Settings\rechlin\My
>> Documents\test\TEST-ant\jof-test.xml
>>
>> setJOF:
>>     [echo] BUILD_BOX = true
>>     [echo] JOF = F:/Inetpub/wwwroot/wo30
>>     [echo] After Property file, JOF = F:/Inetpub/wwwroot/wo30
>>
>> BUILD SUCCESSFUL
>> Total time: 0 seconds
>> --------------------------------------------------
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscr...@ant.apache.org
>> For additional commands, e-mail: user-h...@ant.apache.org
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscr...@ant.apache.org
>> For additional commands, e-mail: user-h...@ant.apache.org
>>
>>
>
>
>
> --
> Wolfgang Häfelinger
> häfelinger IT - Applied Information Technology
> http://www.haefelinger.it
>



-- 
Wolfgang Häfelinger
häfelinger IT - Applied Information Technology
http://www.haefelinger.it

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@ant.apache.org
For additional commands, e-mail: user-h...@ant.apache.org

Reply via email to