ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Ludwig <mil...@gmx.de>
Subject Re: Remove duplicate JAR file names from an XML file
Date Sat, 31 Jul 2010 11:21:05 GMT
Gilbert Rebhan schrieb am 31.07.2010 um 00:47 (+0200):

> Whenever some kind of xml processing occurs within your ant workflow
> i recommend the use of the xmltask[1].

> From your first posting i assume you have some xml like :
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <jars>
> <files>antlr-2.7.6.jar antlr-2.7.6.jar antlr-2.7.6.jar
> aopalliance-1.0.jar aopalliance-1.0.jar aopalliance-1.0.jar
> commons-validator-1.0.2.jar commons-validator-1.0.2.jar
> commons-lang-2.2.jar commons-lang-2.2.jar</files>
> </jars>

If that is the OP's XML, poor design indeed.

> 1) use xmltask/xpath to get a list of filenames and <script ../>
> afterwards to get your distinct list.

>   <script language="beanshell">
>   <![CDATA[
>     String[] jars = project.getProperty("alljars").split(" ");

Should be using a regex here: getProperty("alljars").split("\\s+");

That will take care of linebreaks and tabs, not only spaces.

>      [echo] ${uniquejars} = [antlr-2.7.6.jar, commons-lang-2.2.jar
>      [echo] , aopalliance-1.0.jar, commons-lang-2.2.jar,
> commons-validator-1.0.2.jar]

Duplicates will then be removed.

> 2) take influence on the creation of the xml file if possible,
> and create a structure that is more xpath suitable and simply make use
> of xpath

Much better approach if you want to use XML!

>   [xmltask] Cannot append values to properties
>    ... don' get annoyed from those messages, simply ignore
>    or do a search in the xmlproperty task sources and comment it out

Why is it there in the first place? Appending to a property seems to
work just fine, at least in this case.

While I like XML, I think it's overkill for a list of items. Here's a
plain text example:

          \,,,/
          (o o)
------oOOo-(_)-oOOo------
michael@wladimir:~ :-) cat res.txt 
antlr-2.7.6.jar antlr-2.7.6.jar
antlr-2.7.6.jar aopalliance-1.0.jar
aopalliance-1.0.jar aopalliance-1.0.jar
commons-validator-1.0.2.jar commons-validator-1.0.2.jar
commons-lang-2.2.jar commons-lang-2.2.jar

michael@wladimir:~ :-) expand -t2 res.xml
<project>
  <file file="res.txt" id="input"/>
  <union id="tokens">
    <sort>
      <tokens>
        <resources refid="input"/>
        <stringtokenizer/>
      </tokens>
    </sort>
  </union>
  <pathconvert refid="tokens"
    pathsep="${line.separator}"
    property="tokens2" />
  <echo message="${tokens2}"/>
</project>

michael@wladimir:~ :-) ant -f res.xml 
Buildfile: T:\cygwin\home\michael\res.xml
     [echo] antlr-2.7.6.jar
     [echo] aopalliance-1.0.jar
     [echo] commons-lang-2.2.jar
     [echo] commons-validator-1.0.2.jar

BUILD SUCCESSFUL
Total time: 0 seconds
-------------------------

Can any of it be simplified further?

Maybe it's just me, but I've been struggling with the Ant documentation
to find out how to achieve this. It's so simple in Perl, rather
complicated in Java, and non-obvious in Ant.

When I learnt programming Perl (first language) a couple years ago,
there was a key moment when I understood that you have to grasp each
operation in terms of its input and output, which will allow you to
combine them elegantly and seamlessly:

  print map "$_\n", sort keys %j;

It's about knowing how the different pieces can be combined. I think in
language design this is referred to as composability, usually seen as a
good thing.

With Ant, I'm frequently unsure how to combine things. Well, it's still
not entirely clear, but this doc helped me:

http://ant.apache.org/manual/Types/resources.html

-- 
Michael Ludwig

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message