Ant Pattern Matching - * vs. **
Asked Answered
D

2

15

We are using TeamCity to produce *.nupkg artifacts which we don't want to be cleaned up. TeamCity provides a field where you can specify an ANT-style pattern for indicating which files you do or don't want to be cleaned up. Let's assume for a second that we have the following files which we do not want to be cleaned up:

/a.nupkg
/dir1/b.nupkg
/dir1/dir2/c.nupkg

Does the *.nupkg pattern match .nupkg files both in the root directory AND all child directories or do need to use **.*nupkg to traverse all directories?

I read the following documentation but this is still ambiguous to me: http://ant.apache.org/manual/dirtasks.html#patterns

If there is an Ant-Pattern tester (similar to http://regexpal.com/) that would be amazing.

Drawer answered 29/10, 2015 at 14:57 Comment(2)
Funny, I read the same article and was confused exactly the way you were. I agree, that article was not clear on the meaning/purpose/use of asterisk.Broadcasting
#28177090Jerome
K
13

To match all files, in all directories (from the base directory and deeper)

**/*.nupkg

Will match

sample.nupkg
sample-2.nupkg
tmp/sample.nupkg
tmp/other.nupkg
other/new/sample.nupkg

** will match any directory (multiple directories deep).

*.nupkg will match any file with the nupkg extension. Or just * will match any file or any directory (but just a single directory deep).

PS: There is no Ant Pattern Tester.

Karlakarlan answered 29/10, 2015 at 15:39 Comment(3)
Your answer adds little value as we were already aware that * is a wildcard. You have failed to explain the difference between the use of double asterisk versus single asterisk. This being the point of confusion.Broadcasting
Is there a way to match all files, in all directories except one ? Like for example one that contains string "foo".Candlemaker
@Candlemaker Mostly there is <include name="some-file-pattern" /> part as well as a <exclude name="some-file-pattern" /> part. Fill the foo in under the exclude part, of the Ant fileset.Karlakarlan
P
0

For testing of your patterns a simple way is to echo your fileset contents to stdout or to file, f.e. :

<project>
  <fileset dir="..." id="foobar">
   <include name="..."/>
   <!-- .. -->
  </fileset>

  <!-- simple echo -->
  <echo>${toString:foobar}</echo>

  <!-- use pathconvert for listing files line by line -->
  <pathconvert property="foo" pathsep="${line.separator}" refid="foobar"/>

  <!-- simple echo -->
  <echo>${foo}</echo>

  <!-- print to file -->
  <echo file="whatever.txt">${foo}</echo>

  <!-- use nested mapper if you need only basename -->
  <pathconvert property="fooflat" pathsep="${line.separator}" refid="foobar">
   <mapper>
    <flattenmapper />
   </mapper>
  </pathconvert>

  <echo>$${fooflat} => ${line.separator}${fooflat}</echo>

  <!-- to combine several filesets use -->
  <path id="fooo">
   <fileset dir="...">
    <include name=".."/>
   </fileset>
   <fileset>
     <!-- ... -->
   </fileset>
   <fileset>
     <!-- ... -->
   </fileset>
    <!-- ... -- >
  </path>

  <echo>$${fooo} => ${fooo}</echo>

</project>
Pintle answered 29/10, 2015 at 18:3 Comment(2)
While your comment is useful, it fails to answer the question. Your response does not explain the difference between * and ** as asked in the question.Broadcasting
@Broadcasting Seems you didn't read the posting carefully enough. My answer is related to his last question = "If there is an Ant-Pattern tester (similar to regexpal.com) that would be amazing." - so your downvote ain't rightPintle

© 2022 - 2024 — McMap. All rights reserved.