You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Will Holcomb <wi...@odin.himinbi.org> on 2001/03/18 00:56:31 UTC

changing directories

Ok, I am getting into ant and trying to figure out how to work out a
couple of problems that I am having and I think that their basis is the
same.

Ok, I am generating a set of documents using ant. There is a pdf using fop
and some html and svgs and stuff. It is a bit unusual in that there are no
<javac> tags at all; it is all <mkdir>'s, <copy>'s and <style>'s. It is a
complicated enough set of documents though that it is worthwhile to set a
system like this up (because builds are conditional and happening on both
NT and Linux).

The problem I am having is that ant doesn't actually change directories
when you call another buildfile using an <ant> directive. Right?

Here is the scenario:

1. /build.xml creates the directories /output, /output/pdf, 
    /output/html, and /output/images
2. /build.xml runs:
    <style in="doc.xml" destdir="/output/html"
     out="/output/html/index.html" style="doc-html.xsl"/>
3. /build.xml copies build-pdf.xml to /output/pdf/build.xml and then runs:
    <ant dir="/output/pdf"/>

Things are a little more complex than that, but that catches all the
important points. The problems I am having come up with #2 and #3.

With #2 doc-html.xsl creates an index as its output but it uses xalan
<redirect>'s to generate a set of files being browsed by the index. With
the current <style> tag the destdir tag is ignored when the in and out
tags are being used and all of my redirects and being put in / rather than
/output/html. Would it perhaps make more sense to have the tag for what I
want to look like:
  <style in="doc.xml" destdir="/output/html" out="index.html"
   style="doc-html.xsl"/>
If the destdir is going to be required then it seems like it ought to be
used in some way instead of silently ignored.

That is not really my problem though; what I am wanting is for all of my
<redirect>ed files to end up in /output/html and they are not. Currently
my kludge is to pass in an output_dir param which is prepended to the
output filenames. (Which isn't actually working yet, but I am pretty sure
it will soon enough.) =)

The problem with #3 is what has me stumped. doc.xml contains a set of
relative pathes to ../images. In theory this will allow the same images
pathes to be used in for the fo output (I am using fop to generate the
pdf) and the html output. Because these documents are later to be trundled
off to other places I can't use absolute paths. I went in and fixed the
fop taskdef so it uses ant to get it's filenames for input and output,
so it is able to find the inputs now. When it is running it is using File
objects and their paths do not come through Ant, so it is trying to open
/../images (as opposed to /output/pdf/../images) and crapping out.

Both of these problems have several possible solutions involving varing
amounts of work and changes to the various libraries, but the simplest
solution would be if ant was actually changing directories.

For #2 it could open the input and xsl relative to the basedir and then
change directories to the destdir and do whatever output. This would put
the out file in the right place as well as putting any redirects from that
file there and also any other files generated by the <style> batch
processing.

For #3 when ant recurses to a subbuild it could actually change to that
directory and run. This would mean that the programs running within ant
would not need to use ant to get their relative files and also files being
output would be placed correctly.

The problem is how toactually change directories, does anyone know how to
do this? Java.sun.com didn't help much and neither did google. I think
that it is something funny in the user.dir property. I wrote a little test
program (attached) which checks for a file, changes user.dir, and checks
again. It just prints the value of the filename, File().getAbsulotePath(), 
and File().exists()

Running in a directory with subdirectory test.dir containing file
test.file the output looks like this:

Checking with user.dir = /home/will/doc_generation
  File(".").getAbsolutePath(): /home/will/doc_generation/. : exists : true
  File("test.dir/test.file").getAbsolutePath():
   /home/will/doc_generation/test.dir/test.file : exists : true
  File("test.file").getAbsolutePath():
   /home/will/doc_generation/test.file : exists : false
Checking with user.dir = /home/will/doc_generation/test.dir
  File(".").getAbsolutePath():
   /home/will/doc_generation/test.dir/. : exists : true
  File("test.dir/test.file").getAbsolutePath():
   /home/will/doc_generation/test.dir/test.dir/test.file : exists : true
  File("test.file").getAbsolutePath():
   /home/will/doc_generation/test.dir/test.file : exists : false

The second set of results is completely bogus, File is using user.dir to
generate the name returned by getAbsolutePath(), but not to do whatever is
done to return exists().

So, does anyone know how to change directories in java? Or is there some
more optimum solution I am overlooking?

Will

Re: changing directories

Posted by David Rees <d....@usa.net>.
On 21 Mar 2001 12:00:20 +0100, Stefan Bodewig wrote:

>David Rees <d....@usa.net> wrote:
>
>> All Ant tasks _should_ be relative to the build directory, right?
>> Actually, can we add this as an explicit requirement for Ant2?
>
>Do you mean:
>
>All filename like attributes of tasks should be relative to the
>project's basedir. 
>

Yes, and from a code perspective if a Task creates a file it does
File(projectDir, aName) rather than File(aName)...

d

Re: changing directories

Posted by Stefan Bodewig <bo...@apache.org>.
David Rees <d....@usa.net> wrote:

> All Ant tasks _should_ be relative to the build directory, right?
> Actually, can we add this as an explicit requirement for Ant2?

Do you mean:

All filename like attributes of tasks should be relative to the
project's basedir. 

?

Stefan

Re: changing directories

Posted by David Rees <d....@usa.net>.
On 19 Mar 2001 13:01:08 +0100, Stefan Bodewig wrote:

>Will Holcomb <wi...@odin.himinbi.org> wrote:
>
>> The problem I am having is that ant doesn't actually change
>> directories when you call another buildfile using an <ant>
>> directive. Right?
>
>This is, because you cannot do that in Java at all - without forking
>that is. You could invoke Ant with <java>, setting fork to true and
>dir to the value you need.
>

There are two directories here. The build directory and the working
directory. The former could change the later can't and in fact is
sometimes impossible to set when calling from IDEs (VisualAge,
JBuilder).

All Ant tasks _should_ be relative to the build directory, right?
Actually, can we add this as an explicit requirement for Ant2?

d


Re: changing directories

Posted by Stefan Bodewig <bo...@apache.org>.
Stefan Bodewig <bo...@apache.org> wrote:

> Will Holcomb <wi...@odin.himinbi.org> wrote:

>> If the destdir is going to be required then it seems like it ought
>> to be used in some way instead of silently ignored.
> 
> Fair enough - I'll change <style> to make destdir optional if out
> has been specified.

done.

Stefan

Re: changing directories

Posted by Stefan Bodewig <bo...@apache.org>.
Will Holcomb <wi...@odin.himinbi.org> wrote:

> The problem I am having is that ant doesn't actually change
> directories when you call another buildfile using an <ant>
> directive. Right?

This is, because you cannot do that in Java at all - without forking
that is. You could invoke Ant with <java>, setting fork to true and
dir to the value you need.

> 
> Here is the scenario:
> 
> 1. /build.xml creates the directories /output, /output/pdf,
>    /output/html, and /output/images 
> 2. /build.xml runs: <style in="doc.xml" destdir="/output/html"
>    out="/output/html/index.html" style="doc-html.xsl"/>
> 3. /build.xml copies build-pdf.xml to /output/pdf/build.xml and
>    then runs: <ant dir="/output/pdf"/>

Why are you using a separate build.xml for step 3?

> With the current <style> tag the destdir tag is ignored when the in
> and out tags are being used

So use paths relative to your project's basedir when defining them. Is
this an option?

> If the destdir is going to be required then it seems like it ought
> to be used in some way instead of silently ignored.

Fair enough - I'll change <style> to make destdir optional if out has
been specified.

> but the simplest solution would be if ant was actually changing
> directories.

Not possible, unless we fork a new VM.

> I think that it is something funny in the user.dir property. I wrote
> a little test program (attached) which checks for a file, changes
> user.dir, and checks again. It just prints the value of the
> filename, File().getAbsulotePath(), and File().exists()

Well, this may or may not work at all. It will get you into serious
troubles if your CLASSPATH contains relative paths, that's for
sure. The VM for MacOS is reported to change directories correctly,
but ...

See the Execute class and all the OS-specific special cases Ant has to
go through, to invoke an external command in a given directory.

Stefan