Eric B
Eric B

Reputation: 4437

How do I exclude a folder from a project but not from publish?

I've migrated an asp.net core project to VS2017 RC, which now supports the ability to exclude files from a project. I've excluded two folders, which added these lines to my csproj file:

<ItemGroup>
  <Content Remove="wwwroot\dist\**" />
  <Content Remove="wwwroot\lib\**" />
</ItemGroup>

This works great, except now those files don't get published anymore. How do I exclude these folders from the project but still include them on publish?

Upvotes: 14

Views: 7401

Answers (3)

Gonzalo Lucero
Gonzalo Lucero

Reputation: 1735

The following msbuild items taken from the dotnet new spa templates helped me to achieve what I believe you are after:

<ItemGroup>
    <Content Remove="wwwroot\dist\**" />
    <Content Remove="wwwroot\lib\**" />
</ItemGroup>

<Target Name="GiveAName" AfterTargets="ComputeFilesToPublish">
    <!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
    <Exec Command="npm install" />
    <Exec Command="npm etc etc do your stuff" />
    <Exec Command="or webpack" />

    <!-- Include the newly-built files in the publish output -->
    <ItemGroup>
        <DistFiles Include="wwwroot\dist\**; wwwroot\lib\**" />
        <ResolvedFileToPublish Include="@(DistFiles-&gt;'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
            <RelativePath>%(DistFiles.Identity)</RelativePath>
            <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
        </ResolvedFileToPublish>
    </ItemGroup>
</Target>

Upvotes: 15

Eric B
Eric B

Reputation: 4437

I worked around this by adding the following lines to the PrepublishScript target in the csproj file, since there doesn't seem to be an elegant way to do it yet.

<Target Name="PrepublishScript" BeforeTargets="PrepareForPublish" Condition=" '$(IsCrossTargetingBuild)' != 'true' ">
  <!-- Other things here already -->
  <ItemGroup>
    <Lib Include="wwwroot/lib/**;" />
    <Dist Include="wwwroot/dist/**;" />
  </ItemGroup>
  <Copy SourceFiles="@(Lib)" DestinationFolder="$(PublishDir)\wwwroot\lib\%(RecursiveDir)" SkipUnchangedFiles="true" />
  <Copy SourceFiles="@(Dist)" DestinationFolder="$(PublishDir)\wwwroot\dist\%(RecursiveDir)" SkipUnchangedFiles="true" />
</Target>

Upvotes: 3

weir
weir

Reputation: 4761

You might be able to satisfy your needs with a Condition such as:

<ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
  <Content Remove="wwwroot\dist\**" />
  <Content Remove="wwwroot\lib\**" />
</ItemGroup>

EDIT:

Or perhaps try:

<ItemGroup>
  <Content Condition=" '$(Configuration)' == 'Debug'" Remove="wwwroot\dist\**" />
  <Content Condition=" '$(Configuration)' == 'Debug'" Remove="wwwroot\lib\**" />
</ItemGroup>

EDIT 2:

Since it looks like the IDE is going to respect the Condition, you could instead change items from Content to None and add a BeforePublish target that adds equivalent Content items.

EDIT 3:

So to be specific, keep your original

<ItemGroup>
  <Content Remove="wwwroot\dist\**" />
  <Content Remove="wwwroot\lib\**" />
</ItemGroup>

and add, just before </Project>

<Target Name="BeforePublish">
  <ItemGroup>
    <Content Include="wwwroot\dist\**" />
    <Content Include="wwwroot\lib\**" />
  </ItemGroup>
</Target>

Upvotes: 0

Related Questions