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.
MSBuild lets you set properties on the command line by using the -property (or -p) switch. These global property values override property values that are set in the project file. This includes environment properties, but does not include reserved properties, which cannot be changed.
Click on System and Security and then on System. In the left pane, click on Advanced system settings. At the very bottom of the pop up, click on Environment Variables. Edit the Path variable and append the folder's path that contains the MSBuild.exe to it (e.g., ;C:\Windows\Microsoft.NET\Framework64\v4.
To build a specific target of a specific project in a solution. At the command line, type MSBuild.exe <SolutionName>. sln , where <SolutionName> corresponds to the file name of the solution that contains the target that you want to execute.
The CopyLocalLockFileAssemblies property is useful for plugin projects that have dependencies on other libraries. If you set this property to true , any NuGet package dependencies are copied to the output directory. That means you can use the output of dotnet build to run your plugin on any machine.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With