开发者

Issue writing out file path properties in ant

开发者 https://www.devze.com 2023-01-09 14:24 出处:网络
I\'m having an issue with writing out a property which holds the开发者_开发知识库 value of a directory path into a property file.

I'm having an issue with writing out a property which holds the开发者_开发知识库 value of a directory path into a property file.

My script originally reads in this particular property, call it 'appserver.home', from a props file using the <property file="source.props"/>. I've echoed the value coming in and it reads correctly as C:\\somedir\\jboss_4_2_3.

What my script needs to do next is provide this value to another properties file (used by another ant script - though that's not important). To create this other file I'm using a kind off template file with place holders surrounded by $....$ to insert the correct values in the correct place, using the following :-

 <copy file="template_file.props" tofile="target.props">
    <filterset begintoken="$" endtoken="$">
        <filter token="appServerDir" value="${appserver.home}"/>
        <filter token="dbusername" value="${database.name}"/>
        ....
    </filterset>
 </copy>                

The problem is that the value now in the target.props is C:\somedir\jboss_4_2_3 ie it's lost the escape characters. When the next ant script uses this file it interprets the property value as C:somedirjboss_4_2_3.

So the question how do I tell ant that the value I'm writing is a file path ? Note I have tried the following, which actually works :-

<propertyfile file="target.props">
    <entry key="appServerDir" value="${appserver.home}"/>
</propertyfile>

.. ie it outputs the name as c\:\\somedir\\jboss4_2_3, but I'd rather not use this technique and rather use the template file technique, as it contains some properties which are always static, as well as comments etc.

Thanks in advance


There's some perhaps confusing differences between tasks in respect of the treatment of escapes going on here.

When you say that 'appserver.home' echoes correctly, I guess you are using the 'echoproperties' task which shows you the value stored without interpolating escapes. And that shows the same number of escapes as in your 'source.props' file.

The problem is that, in general, when Ant interpolates this value in to a string it will consume the escapes, hence they disappear. An exception to this is in the 'propertyfile' task, where you would normally want the escapes retained in the output property file - as you have observed - in order that the file can be read properly later.

So, what to do?

Perhaps the simplest thing is to make sure that the properties read from 'source.props' retain their escapes for use in later filters. So instead of using

<property file="source.props"/>

to load, use

<loadproperties srcfile="source.props">
    <filterchain>
        <replacestring from="\" to="\\" />
    </filterchain>
</loadproperties>

That should ensure that your escape sequences propagate.


I have just tested using Apache Ant version 1.7.1 compiled on May 25 2010 on Ubuntu server 10.10, and using the Apache Ant version 1.7.1 compiled on June 27 2008 in Eclipse 3.6 on Windows XP. now the results (from both the same):

Variable in source.props:
appserver.home=C\:\\somedir\\jboss_4_2_3

Echo on console:
[echo] C:\somedir\jboss_4_2_3

Variable in target.properties:
appserver.home=C:\somedir\jboss_4_2_3

EDIT - the solution below;-D

In my opinion you should use the propertyfile, if you read the manual for this task you can see that it is able to MANIPUlATE existing property files. This one works for me, and all the comments and vars from template are preserved.

<copy file="template_file.props"  tofile="target.props" />
<propertyfile  file="target.props">
    <entry  key="appserver.home" value="${appserver.home}"/>
</propertyfile>


As a workaround, you could also write your initial property using forward slashes as C:/somedir/jboss_4_2_3, which should need no escape characters.


I just tested the following using Eclipse integrated ant support:

<copy file="test.props" tofile="target.props">
    <filterset begintoken="$" endtoken="$">
    <filter token="appServerDir" value="C\:\\somedir\\jboss_4_2_3"/>
    </filterset>
</copy> 

and it generates the following file:

C\:\\\somedir\\\jboss_4_2_3

What ant version are you using ?

0

精彩评论

暂无评论...
验证码 换一张
取 消