开发者

Nant: can't get current directory from .include

开发者 https://www.devze.com 2023-02-20 23:02 出处:网络
It\'s not exactly easy for me to summarize this... I have this structure on disk: [dir] project [dir] foo

It's not exactly easy for me to summarize this... I have this structure on disk:

[dir] project

  • [dir] foo

    • [file] foo.build
  • [dir] bar

    • [file] bar.build
  • [file] default.include

The file default.include contains a couple of properties which are the directories used during the build. E.g: property name="build.dir" value="${directory::get-current-directory}".

The default.include file is included by foo.build and bar.build using its relative path: include buildfile="..\default.include"

Now the problem: when I run foo.build from project\foo dir, I get the wrong value for build.dir. I need "project" and I get "project\foo" instead. Is there a开发者_StackOverflow社区 way to get the directory in which the .include file exists?

I could do this in a batch file using %~dp0


you can specify the base directory of a project by using this

<property name="build.dir" value="${project::get-base-directory()}">

and make sure you set the build output path to be build.dir


You could set the build path property to be the relative difference between the working folder and the project root before including your shared include stuff. So, in \project\foo\foo.build:

<property name="build.dir" value="..\" />
<include buildfile="..\default.include" />

If you want to then refer to things relative to the project root then just build paths from build.dir. Let's say you had a \project\utilities\ folder where you put some custom build tools or something, then you can refer to that folder as:

<property name="utils.dir" value="${build.dir}utilities\" />

utils.dir would be ..\utilities\ which would be the right value from your working folder (\project\foo\ in this case).

Personally I do things the other way around - I have the main build file at the root of the project and it includes a build file for each sub-project. Physically the build is distributed across the projects, but logically it all runs as if it were one big script in the project root. The downside being that global property names and target names can collide so you need a naming convention to prevent that from happening.


Just got exactly the same problem: NAnt - how to get the directory path of the included script

In my include file I need to get the directory of the include file, to refer to something else relative to it. Seems like when NAnt includes another build file, it includes the content of it, without changing anything in its environment (directory::get-current-directory, project::get-buildfile-path, etc).

Another solution in your case might be to use <nant> task instead of <include>. Obviously it works if you don't need anything returned from the included script and just want to run it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号