ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Ant Wiki] Update of "AntNewbies" by JanMatèrne
Date Wed, 11 Jul 2007 07:13:20 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Ant Wiki" for change notification.

The following page has been changed by JanMatèrne:
http://wiki.apache.org/ant/AntNewbies

The comment on the change is:
Answer question #17

------------------------------------------------------------------------------
  '''Question#17'''
  I have a bunch of files in a folder, that I want to import into my build.xml. Can anybody
give me a hint (or a solution), how to import all files from a directory, e.g. using a fileset?
Otherwise, I'd have to change my code anytime, a new file is added to the directory...
  
+ 
+ '''Answer#17'''
+ The <import> task does not support <fileset>s or <resourcecollection>.
Not sure about the problem with <fileset> but with <rc>s we dont know the basedir
of the imported file, because URL or just String could be a resource, too.
+ 
+ What you can do is import a single (optional) file - a generated one, which imports all
your files. If I have to do a kind of file listing, I use <pathconvert>. So this works
for me:
+ 
+ {{{
+ <project default="generateImport">
+ 
+     <property name="antfile.dir" value="buildfiles"/>
+     <property name="antfile.gen" value="_generated_.xml"/>
+ 
+     <import file="${antfile.gen}" optional="true"/>
+     
+     <target name="generateImport">
+         <property name="import.start" value="&lt;import file=&quot;"/>
+         <property name="import.end"   value="&quot;/&gt;"/>
+         <property name="br"  value="${line.separator}"/>
+         <property name="tab" value="    "/>
+         <pathconvert property="antfiles.import" 
+                      dirsep="/" 
+                      pathsep="${import.end}${br}${tab}${import.start}">
+             <map from="${basedir}/" to=""/>
+             <fileset dir="${antfile.dir}" includes="*.xml" excludes="${antfile.gen}"/>
+         </pathconvert>
+         <echo file="${antfile.gen}">
+             &lt;project&gt;
+                 ${import.start}${antfiles.import}${import.end}
+             &lt;/project&gt;
+         </echo>
+     </target>
+     
+     <target name="test"/>
+ 
+ </project>
+ }}}
+ 
+ The first two properties define where your buildfiles (for import) are and where to store
the generated buildfile. You could just change the names, but dont change the directory layout,
because the file-locations of the imported files must be relative to the generated file. And
the generator does not know any different. (Maybe you could play with the <map> on line
16...).
+ 
+ Line 6 is importing the generated buildfile. With optional=true, so that if you dont have
generated the file, the build wont fail.
+ 
+ The target in line 26 "test" is just for (you guess) test.
+ 
+ The interisting stuff is in the target "generateImport" starting in line 8:
+ first I define four properties. The syntax for import is: <import file="FILENAME"/>.
So I define (xml-escaped) the part before and after the FILENAME (the filename will come from
<pathconvert>). br and tab are for layout only.
+ 
+ The <pathconvert> collects all files and stores the list in the property ''antfiles.import''.
For platform purposes I use the / as directory separator. The nested <map> elements
deletes the first part of the (absolute) paths so that you'll get relative ones (to the actual
buildfile). The <fileset> defines which (your manually written) files to import.
+ 
+ The final buildfile is generated with the <echo> in combination with <pathconvert
pathsep>. The ''pathsep'' (on Windows a semicolon) is between the individual filenames.
So I need to put the import-end, a line break (layout only) and the import-start in between.
Finally I need the project-start and an import-start before and an import-end and project-end
after the whole list.
+ 
+ If you start a ''ant test'' you'll get no error. An ''ant -p'' just prints the ''generateImport''
and ''test'' target. After running a simple ''ant'' or ''ant generateImport'' the final ''ant
-p'' will print all your written targets.
+ 

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


Mime
View raw message