ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Peter Reilly <peter.rei...@corvil.com>
Subject Re: Splitting full filename into package and filename
Date Wed, 18 Feb 2004 10:12:24 GMT
Hi,
You have ran into one of the currently weak areas of ant - mapping of 
paths and filenames.
The easiest solution I can think of is to use a script:

  <target name="run">
    <property name="srcdir" location="src"/>
    <ac:for param="dir">
      <ac:path>
        <ac:dirset dir="src"/>
      </ac:path>
      <ac:sequential>
        <echo>dir is @{dir}</echo>
        <script language="beanshell">
          import java.io.File;
          import org.apache.tools.ant.util.FileUtils;
          setProperty(name, value) {
             if (project.getUserProperty(name) == null) {
               project.setProperty(name, value);
             } else {
               project.setUserProperty(name, value);
             }
          }
          fileUtils = FileUtils.newFileUtils();
          relative = fileUtils.removeLeadingPath(
              new File(srcdir), new File("@{dir}"));
          packageName = relative.replace('\\', '/'); // DOS filenames
          packageName = packageName.replace('/', '.');
          setProperty("package", packageName);
          setProperty("relative", relative);
        </script>
        <echo>relative is '${relative}' package is '${package}'</echo>
      </ac:sequential>
    </ac:for>
  </target>
</project>

Generating the output:
run:
     [echo] dir is /home/preilly/learning/ant/src
     [echo] relative is '' package is ''
     [echo] dir is /home/preilly/learning/ant/src/code
     [echo] relative is 'code' package is 'code'
     [echo] dir is /home/preilly/learning/ant/src/parsing
     [echo] relative is 'parsing' package is 'parsing'
     [echo] dir is /home/preilly/learning/ant/src/task
     [echo] relative is 'task' package is 'task'
     [echo] dir is /home/preilly/learning/ant/src/task/subtask
     [echo] relative is 'task/subtask' package is 'task.subtask'

Peter

F. Da Costa wrote:

> Dominique Devienne wrote:
>
>> What you're trying to achieve sounds doable, but with a bit
>> more info we might be able to help more. --DD
>
>
> Fair enough, guess i've been looking at this too long.
> I'l try and explain better.
>
> In essence i require two things:
> 1. a package string (to be inserted at the top of a java file)
> 2. a list of space separated filenames (no dir-prefixes on the 
> filenames).
>
> Basically what i am doing now is:
> Create a dirset (within a path) and loop through that with foreach 
> with the target being responsible for the second phase.
>
> The second target is entered with the full blown dir 
> (c:\deault\path\to\whatever\package\the\fileisin).
> c:\deault\path\to\whatever is the scr.dir and package\the\fileisin is 
> the string that i need to turn into a package (assume ${package}).
> I tried <propertyregex property="package" input="${dir}" 
> regexp="(.*)\.(.*)" replace="(.*)\\(.*)" />
> but i'm going nowhere fast. :(
>
> Now i create a fileset on the java files in the ${dir}
> This is where my second issue surfaces.
> If i use pathconvert i do get the list but the files contain their 
> path prefixes.
> Using pathtofileset does get rid of the path-prefixes but does not 
> allow for removal of the , file-separators. I could probably use 
> propertyregex on the result but here i get back to issue 1.
>
> I hope this is somewhat clearer.
> It is probably too simple for words but its still eluding me.
> Would really appreciate a hand because i've just passed the 2 day mark 
> (though that includes getting to grip with Ant as well).
>
> TIA
> Fermin DCG
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>
>


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


Mime
View raw message