Visual Studio 2022 – Configuring custom project build with version control for easily configuring output application

Issue

I’m facing a problem, where I want to easily switch between how I want to build a project. The entire project is a single .sln with many .csproj files. I want to change output directory and some additional settings with as few steps as possible. I am using version control.

Lets say my project is:

  • MainSolution.sln
    • Project1.csproj
      • Settings1.settings
    • Project2.csproj
      • Settings2.settings
    • Project3.csproj
      • Settings3.settings

Project for customer1 should be built in folder /CustomerAlpha. A value in Settings1 should be set to True
Project for customer2 should be built in folder /CustomerBeta. A value in Settings1 should be set to False
Project for customer3 should be built in folder /CustomerGamma. Project3 is not needed and should not be included

I know this can be achieved by having different branches, but I don’t want to constantly worry about cherry-picking commits, rebasing or merging… Which is basically what I do now.

I am using Directory.Build.props file to set output directories depending on customer so that I can use the output directory as my debug location (for relative paths within my code).

I know about Project configurations, but currently I only have Debug/Release for conditional compilation. Besides, I have read somewhere that adding build configurations is VS specific and it would not be stored on git and shared between developers?

I heard about making custom controls for visual studio toolbar, but I haven’t gotten around to test
this out.. I am assuming this is also VS specific thing and as that and would again introduce problems for multiple developers.

This is as far as I have gotten by searching online, but I ran out of ideas for what to search for…

What would be the best way to achieve this? And how could I make sure that the solution would work regardless of which PC the solution is built on or by which developer?

Thanks for any replies!

(EDIT: fixed formatting)

Solution

After some further testing I have found a solution that fits me best.

I am relying on Directory.build.props and Configuration manager (Like TTT suggested). This way, I only have to define the Configuration for the main project within the solution and not for every single project.

After creating a new Build Configuration (DebugCustomer1), I define a new macro in Directory.build.props:

<PropertyGroup Condition="$(Configuration) == DebugCustomer1">
    <ProjectCustomer>Customer1</ProjectCustomer>
</PropertyGroup>

In the same file, I have defined settings for each customer:

<!-- Settings for Customer1 project -->
<PropertyGroup Condition="$(ProjectCustomer) == Customer1">
    <ProjectTitle>abcdef</ProjectTitle>
    <ClientIP>192.168.1.10</ClientIP>
    <OfflineMode>True</OfflineMode>
</PropertyGroup>

<!-- Settings for Customer2 project -->
<PropertyGroup Condition="$(ProjectCustomer) == Customer2">
    <ProjectTitle>abcdefghij</ProjectTitle>
    <ClientIP>192.1.1.10</ClientIP>
    <OfflineMode>False</OfflineMode>
</PropertyGroup>

I can then pass the macro to any project where I want to use preprocessor directives to control my build by editing the .csproj file like:

<PropertyGroup Condition=" '$(OS)' == 'Windows_NT' ">
  <DefineConstants>WINDOWS;$(ProjectCustomer)</DefineConstants>
</PropertyGroup>

End then I can simply use the directive as (example: I want to have different database seed based on customer):

#if Customer1
  <code>
#endif

Then for the various Settings.settings and app.config files throughout the entire solution I use PostBuild events to replace strings in output files.
For example, in Settings.settings I have a setting named ClientIPAddress with value of ClientIP. In PostBuild event I replace the ClientIP string in the generated file with the value of $(ClientIP) defined in the Directory.build.props.

UPDATE:
Using MSBuild I can build the project from the command line and also change any property defined in the Directory.Build.props:

msbuild .csproj /p:Configuration=Debug /p:ProjectCustomer=Customer1

Answered By – Goliatr

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published