tags:

views:

383

answers:

1

Since I started using MSBuild for our projects, I've created several .proj scripts that are shared by several projects in our repository. All these shared scripts reside in a single directory.

So far I've been referring to the shared scripts by using a relative path, something like this:

<MSBuild Projects="..\..\common\build\MyScriptA.proj" Properties="ABC=XYZ"/>

However, every project also imports a common .proj script like so:

<Import Project="..\..\common\build\CommonImports.proj"/>

which <Import>s several other things and defines some properties.

This morning I thought I could replace the relative path with a variable, perhaps $(CommonDir), which would be defined by importing the CommonImports.proj mentioned above. This would enable me to call the common tasks like this:

<MSBuild Projects="$(CommonDir)\MyScriptA.proj" Properties="ABC=XYZ"/>

However, I can't figure out a way to define this $(CommonDir) variable in such a way as to make it work in all other MSBuild scripts that import CommonImports.proj, regardless of their location.

This question offers several ways of creating a property containing an absolute path from a relative path, but none of those seem to work if all I do is <Import> the script defining the property.

Question 1: I'm fairly new to MSBuild; is there a better way of creating a "library" of reusable .proj scripts I could run via the <MSBuild> task? I am aware of $(MSBuildExtensionsPath), however I would like the common tasks to reside in my checkout so that our build machine would automatically get the latest versions of the common tasks whenever it performs a checkout.

Question 2: How do I define $(CommonDir) inside CommonImports.proj so as to make it contain the absolute path to the directory containing CommonImports.proj?

+1  A: 

I wonder if you shouldn't put it in the MSBuild path:

For example, this project is consumed via:

<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>

and installs itself into:

{program files}\MSBuild\MSBuildCommunityTasks

So perhaps define your own specific sub-folder, and use from there?

<Import Project="$(MSBuildExtensionsPath)\romkyns\CommonImports.proj"/>

etc. Because the $MSBuildExtensionsPath variable is defined separately you shouldn't have as much difficulty with it. Maybe.

Marc Gravell
Thanks, indeed I noticed that msbuildtasks installs itself there. However our common tasks change more frequently (for now at least) than msbuildtasks - which is why I'm looking for a way to let the tasks be referenced within the checkout. As a bonus, if I sync back to the past and invoke a build I will be using the same common tasks I did when a release was made, with no extra hassle.
romkyns