depask
depask

Reputation: 93

Ant writing property file storing filtered properties

I'm facing a very simple problem with ANT script. I have a script that loads and sets many properties loaded from several property files in file system. This properties are used to preconfigure a new project.

The question is: can I write a new property file persisting all the properties that starts with a given prefix (for example "ref.proj.*")?

The number and the name of the properties is variable and so I cannot use the

    <propertyfile file="my.properties">
      <entry  key="ref.proj.first" value="${ref.first}"/>
       ...
      <entry  key="ref.proj.n" value="${ref.n}"/>
    </propertyfile>

It's possibile to apply a filter to a propertyfile task? Thanks in advance!

Upvotes: 1

Views: 305

Answers (2)

Mark O&#39;Connor
Mark O&#39;Connor

Reputation: 78031

The following example uses the groovy ANT task:

    <path id="build.path">
        <pathelement location="lib/groovy-all-2.1.0.jar"/>
    </path>

    <target name="create-properties">
        <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>

        <groovy>
            new File("my.properties").withWriter { writer ->
                properties.findAll { it.key.startsWith("ref.proj") }.each {
                    writer.println it
                }
            }
        </groovy>
    </target>

Upvotes: 0

David W.
David W.

Reputation: 107090

It's taking too long for me to work out all of the kinks. Sorry...

You should look at the <echoproperties> task. This will let you select the various properties and print them out in property = value format.

You could use that as your properties file itself.

Upvotes: 2

Related Questions