2015-06-15 22 views
0

如何在解決方案資源管理器中的文件列表中包含文件,而不將其包含爲編譯的依賴項?在解決方案資源管理器中包含文件,而不會構建依賴關係

我有一個生成.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文件在解決方案資源管理器中不可見。生成的代碼也不會被intellisense找到。

如果我在.csproj中添加了ItemGroup的文件,生成的文件在解決方案資源管理器中可見,但隨後的生成會導致不必要的重建項目。智能代碼仍未找到生成的代碼。

<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該項目是無關緊要的構建,因此不應觸發重建整個項目?

回答

0

將代碼生成移動到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智能感知會工作。

<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> 
相關問題