views:

158

answers:

2

We are using .NET 3.5 with VS2008. I have a solution with ~20 projects in it, and that number will grow over time as the application grows and adds new modules. Today I needed to set a property on every project. I had to edit each csproj file to add the following:

  <Target Name="BeforeBuild">
    <CreateProperty Condition=" '$(SolutionDir)' != '' And '$(SolutionDir)' != '*Undefined*' " 
                    Value="$(SolutionDir)Tools\MSBuild">
      <Output TaskParameter="Value" PropertyName="AlToolPath" />
    </CreateProperty>
  </Target>

This is to enable the projects to find the assembly linker (AL.exe) that is stored in my source repository so that the build is machine agnostic (can check out and build on any machine with .NET without having to install the SDK). The solution is built on the command line using MSBuild by developers, install batch files, CCNet, etc.

Anyway, I searched for a better way to do this (i.e. something like solution level properties) but could not find one. I know that it is possible to set the property value in the MSBuild command line, but that doesn't scale well for me since all existing and future command lines would have to include it. I also thought about calling a before target in the first built project to use a custom task to set an environment variable, and an after target in the last project to remove it, but that seemed more heavy and brittle than just editing all project files individually.

Please let me know if you have done something like this or know a way to set a property once per solution that can be utilized by multiple projects (including .NET 4/VS2010 which I have no experience with). Remember that we are using MSBuild to build the sln file, not individual projects.

A: 

Since Solution files are not MSBuild files you won't be able to perform such operations. You only choice is to deal with it or to use a master msbuild file in place of building the solution file. One option could be to create a special configuration that is used by your build process. And for solution files you can specify the configuration used.

Sayed Ibrahim Hashimi
+3  A: 

You can specify this property, and all the other common ones, in a single file, and load it by including in your project files an Import statement. This is what we do in our build environment, which has something like 220 built projects and multiple shared project files (e.g., "branch"). It works well.

fatcat1111
I like this solution, but it still means the project files need to be edited at least once.
Mafuba