【问题标题】:Include file in solution explorer without it being a build dependency在解决方案资源管理器中包含文件,而不是构建依赖项
【发布时间】:2015-06-15 21:11:47
【问题描述】:

如何在解决方案资源管理器的文件列表中包含文件而不将其作为编译依赖项包含在内?

我有一个生成 .cs 文件的 .targets 文件,类似于the examples in this answer

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <CoreCompileDependsOn>$(CoreCompileDependsOn);GenerateCode</CoreCompileDependsOn>
  </PropertyGroup>

  <ItemGroup>
    <Sources Include="..\sources\*.txt" />
  </ItemGroup>

  <Target Name="GenerateCode" Inputs="@(Sources)" Outputs="@(Sources->'generated\%(Filename).cs')">
    <!-- run executable that generates files -->
    <ItemGroup>
      <Compile Include="generated\*.cs" />
    </ItemGroup>   
  </Target>
</Project>

这会正确构建,并且连续构建不会不必要地重新构建项目。生成的 .cs 文件在解决方案资源管理器中不可见。智能感知也找不到生成的代码。

如果我在 .csproj 中添加带有 ItemGroups 的文件,生成的文件在解决方案资源管理器中可见,但后续构建会导致不必要地重新构建项目。智能感知仍然找不到生成的代码。

  <ItemGroup>
    <Sources Include="..\sources\*.txt">
      <Link>sources\%(Filename)%(Extension)</Link>
    </Sources>
    <!-- using None instead of Compile on the next line makes no difference -->
    <Compile Include="@(Sources->'generated\%(Filename).cs')">
      <Generator>MSBuild:Compile</Generator>
      <Link></Link>
    </Compile>
  </ItemGroup>

我如何告诉 msbuild 项目中包含的 .cs 文件对构建无关紧要,因此不应触发重新构建整个项目?

【问题讨论】:

    标签: c# visual-studio msbuild intellisense


    【解决方案1】:

    将代码生成移至BeforeCompile 而不是CoreCompileDependsOn。这将防止文件的生成触发后续构建。

    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <Target Name="BeforeCompile" DependsOnTargets="GenerateCode">        
      </Target>
    
      <Target Name="GenerateCode" Inputs="@(Sources)" Outputs="@(Sources->'generated\%(Filename).cs')">
        <!-- run executable that generates files -->
      </Target>
    </Project>
    

    如果您在 .csproj 中包含所有生成的文件,则 Visual Studio intellisense 将起作用。

      <ItemGroup>
        <Sources Include="..\sources\*.txt">
          <Link>sources\%(Filename)%(Extension)</Link>
          <LastGenOutput>generated\%(Filename).cs</LastGenOutput>
        </Sources >
        <Compile Include="@(Sources->'generated\%(Filename).cs')">
          <Link></Link>
        </Compile>
      </ItemGroup>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-18
      • 1970-01-01
      • 2011-01-28
      • 2018-06-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多