0

I'm trying to build the symettrical difference of two ";" seperated lists in my ant file. But it doesn't work and I think it might be because the result of String.split(";") seems to be a string, not an Array. (As seen in the println(a1). The result is: [Ljava.lang.String;@6395e579).

See the code:

<scriptdef language="javascript" name="diffLists">
    <attribute name="list1" />
    <attribute name="list2" />
    <attribute name="target" />
    <![CDATA[
          function symmetricDifference(str1, str2) {
                var a1 = str1.split(";");
                println(Array.isArray(a1));
                println(a1);
                var a2 = str2.split(";");

              var result = [];
              for (var i = 0; i < a1.length; i++) {
                if (a2.indexOf(a1[i]) == -1) {
                  result.push(a1[i]);
                }
                }
                for (i = 0; i < a2.length; i++) {
                  if (a1.indexOf(a2[i]) == -1) {
                    result.push(a2[i]);
                  }
                }
                return result;
            }

          project.setProperty(attributes.get("target"),symmetricDifference(attributes.get("list1"),attributes.get("list2")).join(";"));
   ]]>
  </scriptdef>

<target name="init">
    <taskdef resource="net/sf/antcontrib/antlib.xml">
      <classpath>
        <pathelement location="${basedir}/lib/ant-contrib-1.0b3.jar"/>
      </classpath>
    </taskdef>
</target>

<target name="checkDiff" depends="init">
 <var name="fileOld.list" value="a.txt;b.txt" />
 <var name="file.list" value="b.txt;c.txt" />
 <diffLists list1="${fileOld.list}" list2="${file.list}" target="diffList" />
 <echo message="DIFF: ${diffList}" />
</target>

It is supposed to return: a.txt;c.txt But it returns: a.txt;b.txt;b.txt;c.txt

I'm using Apache Ant(TM) version 1.9.2 compiled on July 8 2013 if that matters.

Thank you for your help!

  • If you call `println(a1[0]);` what happens? – Zev Spitz Oct 07 '16 at 07:24
  • println(a1[0]) returns "a.txt" – Helge Richter Oct 07 '16 at 09:46
  • Not familiar with Ant, but it looks like `println(a1)` is printing some kind of string representation of the array, instead of the contents. – Zev Spitz Oct 07 '16 at 09:51
  • I copied and pasted this function into the browser's console and it works correctly. – Zev Spitz Oct 07 '16 at 09:53
  • I'm no expert in JS, but in Java when you print something that does not have a toString() method, it prints something like we see here . It makes no sense that String does not have a toString() method, so it's just another thing that baffles me. – Helge Richter Oct 07 '16 at 09:53
  • Yeah, I figured it must be something together with ant. But I couldn't find any other people with problems when using JS in ant. – Helge Richter Oct 07 '16 at 09:55
  • What happens if you call `println(attributes.get("list1"));`? `println(attributes.get("list2"));`? `println(symmetricDifference("a.txt;b.txt;c.txt","b.txt").join(";"));` ? – Zev Spitz Oct 07 '16 at 09:57
  • println(attributes.get("list1")): a.txt;b.txt – Helge Richter Oct 07 '16 at 10:00
  • println(attributes.get("list1")): b.txt;c.txt println(symmetricDifference("a.txt;b.txt;c.txt","b.txt").joi‌​n(";")): a.txt;c.txt – Helge Richter Oct 07 '16 at 10:01
  • Interesting. So I assume attributes.get does not return a "normal" String, but something ant specific which behaves weird... – Helge Richter Oct 07 '16 at 10:02
  • Tested with ant 1.9.4, JDK 1.8 and using JDK scripting engine (using `manager="javax"` scriptdef's attribute to enforce it. Works perfectly. – P.A. Cros Oct 07 '16 at 10:12
  • Doesn't `Ljava.lang.String;@6395e579` mean it's [a Java array of strings](http://stackoverflow.com/questions/9868482/what-is-ljava-lang-string)? – Zev Spitz Oct 07 '16 at 10:18
  • I've now given up and fixed it by writing a Task in Java instead of a taskdef in JS. – Helge Richter Oct 11 '16 at 09:04

0 Answers0