Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSBuild - Set Properties For Solution

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.

like image 875
Mafuba Avatar asked Feb 19 '10 00:02

Mafuba


People also ask

How do I set properties in MSBuild?

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.

How do I set environment variables in MSBuild?

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.

How do you build specific targets in solutions using MSBuild exe?

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.

What is CopyLocalLockFileAssemblies?

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.


2 Answers

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.

like image 173
Justin R. Avatar answered Oct 02 '22 18:10

Justin R.


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.

like image 20
Taras Alenin Avatar answered Oct 02 '22 17:10

Taras Alenin