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

前端 未结 3 2342
粉色の甜心
粉色の甜心 2021-02-19 17:20

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 cs

相关标签:
3条回答
  • 2021-02-19 17:33

    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>
    
    0 讨论(0)
  • 2021-02-19 17:44

    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>
    
    0 讨论(0)
  • 2021-02-19 17:45

    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>
    
    0 讨论(0)
提交回复
热议问题