Visual Studio 2017 csproj core file exclusion

2019-02-02 15:24发布

问题:

I've migrated xproj core projects to csproj. All is working well, however I still have issues with publish configuration. Based on documentation: https://docs.microsoft.com/en-us/dotnet/articles/core/tools/project-json-to-csproj I should be able to exclude files during publish.

I've added following lines to the f

<None Include="*.json" CopyToPublishDirectory="Never" />
<None Include="wwwroot\**\*.map;wwwroot\**\*.less;*.pdb" CopyToPublishDirectory="Never" />
<None Include="wwwroot\**\*" CopyToPublishDirectory="PreserveNewest" />

But still *.map, .json and .less files are copied to publish folder. I tried different order no luck.

How to exclude certain files from publishing?

回答1:

Short answer: use the following snippets instead:

<ItemGroup>
  <Content Update="**\*.map;**\*.less;*.json" CopyToPublishDirectory="Never" />
</ItemGroup>

You could also add these patterns to the "DefaultItemExcludes" property.

<PropertyGroup>
  <DefaultItemExcludes>$(DefaultItemExcludes);**\*.map;**\*.less;*.json</DefaultItemExcludes>
</PropertyGroup>

Longer answer:

Microsoft.NET.Sdk and Microsoft.NET.Sdk.Web include settings for default items. These are globs for items in your project folder that should always be compiled, embedded, copied to output, etc. There are some settings to control this, but they are not well documented.

If you want to change a metadata value (such as the CopyToPublishDirectory setting ) for an item already included by a default glob, you have to use "Update" instead of "Include".

To see what is happening under the hood, here are the default item settings for Microsoft.NET.Sdk and Microsoft.NET.Sdk.Web

https://github.com/dotnet/sdk/blob/dev15.1.x/src/Tasks/Microsoft.NET.Build.Tasks/build/Microsoft.NET.Sdk.DefaultItems.props#L19-L27

https://github.com/aspnet/websdk/blob/rel/vs2017rtw/src/Web/Microsoft.NET.Sdk.Web.ProjectSystem.Targets/netstandard1.0/Microsoft.NET.Sdk.Web.ProjectSystem.props#L25-L40