Ant setting property by regexp substitution on an existing property

K

kevin cline

I've come across the following Ant task:

<!-- complicated way to set the oracle.app.username from the
version.platform property
by writing a property file, then hacking it with replaceregexp
and then rereading it.
TODO: find simpler way to do this
-->
<target name="create-oracle-username" unless="oracle.app.username">
<mkdir dir="${basedir}/build"/>
<delete file="${oracle.app.username.file}"/>
<propertyfile file="${oracle.app.username.file}">
<entry key="changeme" value="user_${version.platform}"/>
</propertyfile>
<replaceregexp file="${oracle.app.username.file}" match="\."
replace="_" flags="g"/>
<replaceregexp file="${oracle.app.username.file}" match="changeme"
replace="oracle.app.username"/>
<property file="${oracle.app.username.file}"/>
<delete file="${oracle.app.username.file}"/>

Is there some simpler way of setting the oracle.app.username property
from the value of version.platform? This would be one line in a
makefile.
 
K

kevin cline

Can't think of a way to get this to one line, but you might could try  
something like:

<loadresource property="oracle.app.username">
        <propertyresource name="version.platform" />
        <filterchain>
                <concat>
                        <header>_user</header>
                        <replacestring from="." to="_" />
                </concat>
        </filterchain>
</loadresource>

While I haven't actually tried that, loadresource + a filterchain may make  
the logic a bit more clear.

HTH,

-Zig

Thanks. It's still ugly, but definitely an improvement over the
gratuitous introduction of a temporary file just to do a string
replacement.
 
G

Gilbert Rebhan

kevin cline wrote:
[...]
Is there some simpler way of setting the oracle.app.username property
from the value of version.platform? This would be one line in a
makefile.

use a scripting language and make use of the ant api,
method project.setProperty(String name, String value).
something like f.e.

if ${version.platform} is already set before, it's
as easy as =

<script language="ruby">
<![CDATA[
$project.setProperty "oracle.app.username",
"user_"+$project.getProperty('$version.platform').gsub!(/\./,'_')
]]>
</script>


you could use any scripting language that runs via bsf in VM,f.e.
(J)ruby), groovy, beanshell, javascript(rhino from Mozilla) ...

Regards, Gilbert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top