问题
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.
回答1:
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.
回答2:
I have implemented the solution suggested by @fatcat111 a number of times now. This time I have automated this process by using NuGet - Common.Props. The package will add a common props file at a solution level and by installing the package in multiple projects of the same solution the properties can be shared.
The code is on GitHub.
回答3:
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.
来源:https://stackoverflow.com/questions/2293249/msbuild-set-properties-for-solution