Search code examples
msbuildmsbuild-task

msbuild publish with different app.config


I'm currently writing an msbuild script to publish a solution, which is all fine but i want to use a different app.config depending on what configuration is selected.

currently my build script looks like:

<PropertyGroup>
    <ProjectFile>.\BarcodeScannerApp\BarcodeScannerApp.csproj</ProjectFile>
    <SolutionFile>.\BarcodeScannerApp.sln</SolutionFile>        
    <PublishLoc>http://publishlocation.com</PublishLoc>
    <Configuration>release</Configuration>
    <GenerateManifests>false</GenerateManifests>
    <BootstrapperEnabled>true</BootstrapperEnabled>
    <ApplicationVersion>1.0.0.*</ApplicationVersion>
    <UpdateEnabled>true</UpdateEnabled>
    <UpdateMode>Foreground</UpdateMode>
    <UpdateUrl>http://backoffice-dev/</UpdateUrl>
</PropertyGroup>

<Target Name="PublishApp">      

    <MSBuild Projects="$(SolutionFile)" 
             Targets="Publish"               
             Properties="PublishUrl=$(PublishLoc);
             Configuration=$(Configuration);
             GenerateManifests=$(GenerateManifests);
             BootstrapperEnabled=$(BootstrapperEnabled);
             ApplicationVersion=$(ApplicationVersion);
             UpdateEnabled=$(UpdateEnabled);
             UpdateMode=$(UpdateMode);
             UpdateUrl=$(UpdateUrl)"
    />

</Target>

currently when this script is run, it generates a file BarcodeScannerApp.exe.config which is a copy of the app.config i have in the solution. i want to use a different config file depending on what different configuration i set up (Debug / Release).


Solution

  • As first you need to define properties which references an app.config path for all config files like:

    <DebugConfig>...</DebugConfig>
    <ReleaseConfig>...</ReleaseConfig>
    <TargetConfigPath>...</TargetConfigPath>
    

    And then using WHEN select an appropriate one and rewrite into the target directory

    <When Condition="'$(Configuration)'=='DEBUG'">
         ...
    </When>
    
    <When Condition="'$(Configuration)'=='RELEASE'">
         ...
    </When>
    

    You can rewrite files before executing PublishApp targetby introducing new target and create target dependency.