Question

For removing duplicates from comma seperate strings stored in ant property I tried below, but is printing as-is. How to get uniq values from the ant's property - filtering dup.s

Whether comman seperated tokens needs to be replaced to look as individual token before applying sort &/ uniq filter?.

<property name="mydup.prop" value="x,Xx,y,y,z,x,a,x"/>

<loadresource property="myout.prop">
    <string value="${mydup.prop}" />
    <filterchain>
        <sortfilter />
        <uniqfilter />
    </filterchain>
</loadresource>
    <echo message="${myout.prop}"/>
Was it helpful?

Solution 2

You could use Ant's script task to access the underlying Java libraries using JavaScript. JavaScript is available in the JRE since 1.6 so no extra dependency is needed.

<property name="mydup.prop" value="x,Xx,y,y,z,x,a,x" />

<target name="main">
    <echo message="${mydup.prop}" />

    <script language="javascript"><![CDATA[
        var unsorted = project.getProperty("mydup.prop"); 
        var unique = new java.util.ArrayList(new java.util.HashSet(java.util.Arrays.asList(unsorted.split(','))));
        java.util.Collections.sort(unique);
        var result = org.apache.tools.ant.util.CollectionUtils.flattenToString(unique); 
        project.setProperty("myout.prop", result);
    ]]></script>

    <echo message="${myout.prop}" />
</target>

If you do not need sorting you can omit the line java.util.Collections.sort(unique);

OTHER TIPS

I realize this is an old question, but I was running into the same problem and found a way to do this in native Ant without any scripting. The gist of it is that filterchains rely heavily on newlines to separate tokens and run mapping operations, so if you start by replacing your delimiter with a line.separator, almost anything is possible:

`

    <loadresource property="myout.prop">
        <propertyresource name="mydup.prop" />
        <filterchain>
            <tokenfilter>
                <filetokenizer />
                <replacestring from="," to="${line.separator}" />
            </tokenfilter>

            <sortfilter />

            <uniqfilter />

            <tokenfilter>
                <filetokenizer />
                <replacestring from="${line.separator}" to="," />
                <replaceregex pattern="^,|,$" replace="" />
            </tokenfilter>
        </filterchain>
    </loadresource>`

The replaceregex at the end is optional depending on your needs. All it does is get rid of delimiters at the beginning or end of your list.

Had no success with loadresource, tokenfilter ..etc., so i used Groovy.
Needs groovy-all-2.x.x.jar, get the actual Version 2.2.1 here

<project>
 <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>

 <property name="mydup.prop" value="x,Xx,y,y,z,x,a,x"/>

 <groovy>
  properties.'myout.prop1' = properties.'mydup.prop'
   .split(',')
   .toList()
   .unique()
   .sort()
   .toString().replaceAll(/\[|\]/, "")

  properties.'myout.prop2' = properties.'mydup.prop'
   .split(',')
   .toList()
   .unique()
   .sort { a, b -> a.compareToIgnoreCase b }
   .toString().replaceAll(/\[|\]/, "")
 </groovy>

 <echo>
   Default sort => ${myout.prop1}
   Case insensitive sort => ${myout.prop2}
 </echo>
</project>

output :

[echo]    Default sort => Xx, a, x, y, z
[echo]    Case insensitive sort => a, x, Xx, y, z
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top