Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have an optional file that gets copied to the output directory in VS 2017?

On a large (team and code) project, we have set up the various App.configs and Web.configs to reference an (optional) local.config file so that developers can override some things when running locally. local.config is listed in .getignore so it's ignored and not included in commits. This works very well to support local configuration overrides.

However, for console apps, local.config isn't copied to the output directory by default. I can set its properties in VS so that it's copied to the output - it gets listed in the .csproj file like this:

<None Include="local.config">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>

This works fine in VS, even when a particular developer does not have a local.config file at all. However, it fails in the VSTS build jobs because apparently msbuild doesn't like it when a listed file is absent.

Is there any way to configure the project or msbuild so that it will tolerate/ignore the missing (optional) file instead of failing the builds?

like image 897
E-Riz Avatar asked Jul 03 '17 17:07

E-Riz


1 Answers

This can be done by using an msbuild condition:

<None Include="local.config" Condition="Exists('local.config')">
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
like image 123
Martin Ullrich Avatar answered Nov 03 '22 13:11

Martin Ullrich