Johan Polson
Johan Polson

Reputation: 88

MSBuild: Add and remove files to a msbuild 15 publish output @ BeforeTargets="PrepareForPublish"

I want to add and delete files when I publish in msbuild 15.

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Folder Include="wwwroot\" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
  </ItemGroup>

  <!-- My target, which is only executed on publication , Which is desirable in my case -->
  <Target Name="MyPrepareForPublish" BeforeTargets="PrepareForPublish">

    <!-- Delete old files in wwwroot from Content -->
    <ItemGroup>
        <Content Remove="wwwroot\**"/>
    </ItemGroup>

    <!-- I Add and removes files to file system in wwwroot here , Exclude="@(Content)" prevents duplicates from outside wwwroot -->

    <!-- Adding new files to Content form wwwroot -->
    <ItemGroup>
        <Content Include="wwwroot\**" Exclude="@(Content)"/>
    </ItemGroup>

 </Target>

</Project>

But when I remove and add files, I get an error message :

(109,5): Error MSB3030: Could not copy the file "C:\buildTest\WebsSrver\wwwroot\main.d158e9e1259986c4bd76.bundle.js" because it was not found.

My code deletes "main.d158e9e1259986c4bd76.bundle.js" and creates a new one with another name in MyPrepareForPublish.

The files are located in wwwroot im my dotnet core project.

So how do I specify what should be included in publish output ?

Thanks in advance !

Upvotes: 0

Views: 1140

Answers (1)

Martin Ullrich
Martin Ullrich

Reputation: 100581

When adding files during a pre-publish target, the SDK will also add corresponding ContentWithTargetPath items. However, the SDK does not track removals and modifications to Content items are irrelevant here since the publish targets use the ContentWithTargetPath only.

You should be able to work around your problems by removing the item groups removing and re-adding Content items and simply removing leftover ContentWithTargetPath after the custom steps have been run like this:

<ItemGroup>
    <ContentWithTargetPath Remove="@(ContentWithTargetPath)" Condition="!Exists('%(FullPath)')" />
</ItemGroup>

The 2.0.0 SDK will add ContentWithTargetPath for newly generated files automatically.

Upvotes: 1

Related Questions