Msbuild 如何从项目中排除文件夹,而不从发布中排除文件夹?

Msbuild 如何从项目中排除文件夹,而不从发布中排除文件夹?,msbuild,asp.net-core,visual-studio-2017,Msbuild,Asp.net Core,Visual Studio 2017,我已经将asp.net核心项目迁移到VS2017 RC,它现在支持从项目中排除文件的功能。我排除了两个文件夹,它们将以下行添加到我的csproj文件中: <ItemGroup> <Content Remove="wwwroot\dist\**" /> <Content Remove="wwwroot\lib\**" /> </ItemGroup> 这非常有效,但现在这些文件不再发布了。如何从项目中排除这些文件夹,但在发布时仍包含它们?

我已经将asp.net核心项目迁移到VS2017 RC,它现在支持从项目中排除文件的功能。我排除了两个文件夹,它们将以下行添加到我的csproj文件中:

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


这非常有效,但现在这些文件不再发布了。如何从项目中排除这些文件夹,但在发布时仍包含它们?

您可以使用
条件满足您的需要,例如:

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


我通过在csproj文件中的
预发布脚本
目标中添加以下行来解决这个问题,因为似乎还没有一种优雅的方法来完成它

<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>

以下从dotnet新spa模板中获取的msbuild项帮助我实现了我相信您所追求的目标:

<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>

%(DistFiles.Identity)
保存最新

回避了一个问题,如果您希望发布它们,为什么要将它们从项目中排除?@DavidG这样它们就不会被visual studio在文件中查找、在解决方案资源管理器中显示等所获取。它们是缩小的javascript/css文件和外部库。最好将它们隐藏起来。
DnxInvisibleFolder
是否仍然有效?例如:
。虽然我还是不明白你为什么不把它们放在里面。不,那不行。我很在意,因为Visual Studio在文件中查找可能需要很长时间,并从外部库或缩小的文件中返回数百个搜索字符串的结果。是否可以右键单击VS中的文件夹并将其隐藏?这就是它以前的工作方式。Visual Studio 2017解决方案资源管理器忽略了
条件
?我想我不应该感到惊讶。我添加了一个变体,以防它从IDE中获得所需的行为,但我怀疑它是否会:(尝试过此方法后,visual studio中的发布根本无法复制wwwroot,使用dotnet publish仍然会排除dist和lib,即使使用BeforePublish目标,您是否可以提供另一个?没有其他内容包含或删除linesTry将目标名称从BeforePublish更改为BeforeBuild。代码示例似乎需要一个
预发布csproj中的脚本
target。ASP.NET核心web应用程序模板中没有该脚本。在这种情况下,您是否将其添加到
项目
元素中?我刚刚在VS2017(v15.3.5)上测试了此脚本谢谢!顺便说一句,我删除了npm的东西,因为我不需要它。不用担心。仅供参考,我还删除了npm命令,并且发现在VS之外处理前端任务的效率要高得多。我正在尝试让它工作,但是对于.net项目之外的npm构建目录(后端在一个文件夹中,在部署过程中,从第二个文件夹捆绑UI资源)。例如,我的dist文件看起来像,但我正在努力收集任何输出…我不理解inlcude distfiles->fullpath的语法??还有人尝试过此文件夹结构吗?
<Target Name="BeforePublish">
  <ItemGroup>
    <Content Include="wwwroot\dist\**" />
    <Content Include="wwwroot\lib\**" />
  </ItemGroup>
</Target>
<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>
<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>