4

I have a properties in file dev.properties and they look like this:

test.url=https://www.example.com/
[...]

and in project files there is a token [[test.url]] which I want to replace by https://www.example.com/. I just want to define all tokens in dev.properties and use them in build script, but without modifying build script and I want to replace those tokens in a specified files like *.php, *.html, etc.

Can someone give me a suggestions how to do it? Thanks.

Cezary Tomczyk
  • 584
  • 1
  • 7
  • 14

3 Answers3

4

try this:

<copy file="input.txt" tofile="output.txt">
   <filterchain>
   <replaceregex pattern="\$\{" replace="{" />
   <filterreader classname="org.apache.tools.ant.filters.ReplaceTokens">
        <param type="propertiesfile" value="properties.txt"/>
        <param type="tokenchar" name="begintoken" value="{"/>
        <param type="tokenchar" name="endtoken" value="}"/>
    </filterreader>
    </filterchain>
</copy>

Founded here: Ant replace token from properties file

Community
  • 1
  • 1
ryszardPL
  • 41
  • 3
1

In the following Ant script, replace the src-root property with the root directory containing the tokenized files:

<project name="ant-replace-tokens-with-copy-task" default="run">
    <target name="run">
        <!-- The <copy> task cannot "self-copy" files. So, for each -->
        <!-- matched file we'll have <copy> read the file, replace the -->
        <!-- tokens, and write the result to a temporary file. Then, we'll -->
        <!-- use the <move> task to replace the original files with the -->
        <!-- modified files. -->
        <property name="src-root" location="src"/>
        <property name="filtered-file.extension" value="*.filtered-file"/>

        <copy todir="${src-root}">
            <fileset dir="${src-root}">
                <include name="**/*.html"/>
                <include name="**/*.php"/>
            </fileset>
            <globmapper from="*" to="${filtered-file.extension}"/>
            <filterchain>
                <filterreader classname="org.apache.tools.ant.filters.ReplaceTokens">
                    <param type="propertiesfile" value="dev.properties"/>
                </filterreader>
            </filterchain>
        </copy>

        <move todir="${src-root}">
            <fileset dir="${src-root}" includes="**"/>
            <globmapper from="${filtered-file.extension}" to="*"/>
        </move>
    </target>
</project>
Chad Nouis
  • 6,861
  • 1
  • 27
  • 28
  • needs to specify every token separately. I was thinking about something else. I mean, one task for one file with properties. So, I can easy add any properties without changing ant task. My properties in files are stored as: [[my.property]]. Here what I am doing: http://codepad.org/VCuyWEyn Maybe there is better way. P.S. I can not paste code here due to amount of character limitations in comments. Which is really very weird to limit to very low amount. – Cezary Tomczyk Apr 16 '13 at 17:46
  • Please edit your question and tell us exactly what you're looking for. Give us the smallest possible example that shows what's in dev.properties. Give us the smallest possible example of what a *.php file with tokens looks like. Copy the Ant script from the Codepad site into your question. And then show what you would like the final *.php file to be after the replacements are done. – Chad Nouis Apr 16 '13 at 21:16
  • File dev.properties (or whater name of file) contains some of properties that must be replaced in source code files. Example of dev.properties: project.cdn.url=https://cdn.example.com/ project.cdn.fonts=https://fonts.example.com/ [...] Now, source code (no matters if it is html, php or css; could be any file) can contains "temporary variable" like in example: Logo So, the goal is to replace all "variables" in source code that matching to properties written in specified file (here dev.properties). – Cezary Tomczyk Apr 16 '13 at 21:24
  • First, you should edit your question to make it clear that the example token is like [[project.cdn.url]] and not @test.url@. Are the delimiters "[[" and "]]" required? The ReplaceTokens filter reader used in my answer only works with single character delimiters. "@" is the default, but "[" and "]" would also work. If you're unable to change the delimiters, consider using a that will map property keys. For example, a under a can transform a "project.cdn.url" into a "[[project.cdn.url]]". – Chad Nouis Apr 16 '13 at 22:05
  • 1. Question is already edited. 2. Delimiters "[[" and "]]" can be replaced by "@". 3. Let me check your code and test hw it works. 4. I have a some problems with pasting code here due to the limitation of characters problem. Moreover, I use 4 spaces to indent every line as StackOverflow suggest, but still wasn't look good. I do not know why. – Cezary Tomczyk Apr 17 '13 at 09:27
  • You used "copy & move" solution. What about "replace replacefilterfile" which I used in [link]http://codepad.org/VCuyWEyn[/link]. Of course, everything depends on what is needed to be done. In my case I sometimes minify and concatenate script, CSS files, and sometimes not. That's why I used solution as mentioned above. – Cezary Tomczyk Apr 17 '13 at 10:01
  • It doesn't look like supports nested elements. does, so the "org.apache.tools.ant.filters.ReplaceTokens" can be used. "ReplaceTokens" is nice because it automatically surrounds each property with delimiters. A doesn't. To use , you can first transform the property keys with something like a under a , write the results to a file, and then read that file with . As a side note, are you aware of the filterchain? – Chad Nouis Apr 17 '13 at 14:33
  • "To use , you can first transform the property keys with something like a under a , write the results to a file, and then read that file with ." I do as you described, but little bit in a different way: http://codepad.org/nlRaeL7F I'm sorry, but I can not paste the code here because StackOverflow has a large limit number of characters in a comment. I do not know why. – Cezary Tomczyk Apr 19 '13 at 12:52
1

You specified that you do not want to edit your build script so this answer does not qualify but may still be useful to other readers.

If you were willing to edit your target file to use the format ${test.url} instead of [[test.url]] then ExpandProperites would be an excellent choice.

PeterVermont
  • 1,922
  • 23
  • 18