2016-09-27 34 views
1

使用的MSBuild包括我得到一個項目文件的屬性值的解決方案>如何使用的MSBuild

<ItemGroup> 
    <ProjectToBuild Include="$(SVNLocalPath)\$(SolutionName)"> </ProjectToBuild> 
</ItemGroup> 

我需要包括所有從與凸出文件的條件解決方案的* .csproj的文件包含或定義一個屬性;例如,如果x.csproj包含定義的屬性「TestProjectType」希望包括項目到我的ItemGroup

像這樣

<Target Name = "TestProperties"> 
    <Message Text="TestProperties"/> 
    <ItemGroup> 
     <AllProj Include="$(SVNLocalPath)\*.csproj"/> 
     <AllTestProj Include="%(AllProj.Identity)" Condition="%(AllProj.ProjectTypeGuids)=={3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"/> 
    </ItemGroup> 
    <Message Text="@(AllTestProj)"/> 
</Target> 

感謝

+0

所以,如果我正確地理解它,你實際上有兩個問題a)如何從解決方案中獲得所有項目和b)在某些屬性上過濾這些項目?你是否考慮過爲這些GUID手動刷新文件? – stijn

回答

2

可以實現通過自定義任務。

一個簡單的示例來檢查所有項目的屬性(測試)排除當前解決方案的當前項目:

<UsingTask TaskName="GetPropertyTask" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v12.0.dll"> 
    <ParameterGroup> 
     <ProjectFile ParameterType="System.String" Required="true" /> 
     <BuildOutput ParameterType="System.String[]" Output="true" /> 
    </ParameterGroup> 
    <Task> 
     <Reference Include="System.Xml"/> 
     <Reference Include="Microsoft.Build"/> 
     <Using Namespace="Microsoft.Build" /> 
     <Using Namespace="Microsoft.Build.Evaluation" /> 
     <Using Namespace="Microsoft.Build.Utilities" /> 
     <Code Type="Fragment" Language="cs"> 
      <![CDATA[ 

     var properties = new Dictionary<string, string> 
     { 
      { "Configuration", "$(Configuration)" }, 
      { "Platform", "$(Platform)" } 
     }; 
     //Log.LogMessage(MessageImportance.High, "customLog"); 
     // Log.LogMessage(MessageImportance.High, ProjectFile); 

     var collection = new ProjectCollection(properties); 
     var project = collection.LoadProject(ProjectFile); 
     ProjectProperty pp = project.Properties.Where(p => p.Name == "MyCustomProperty").FirstOrDefault(); 
      string customValue = pp==null?"empty":pp.EvaluatedValue; 
     BuildOutput = new String[] { customValue }; 
     ]]></Code> 
    </Task> 
    </UsingTask> 
    <Target Name="AfterBuild"> 
    <GetPropertyTask ProjectFile="%(ProjectToScan.FullPath)"> 
     <Output ItemName="ProjectToScanOutput" TaskParameter="BuildOutput"/> 
    </GetPropertyTask> 
    <Message Text="ClassLibrary1" Importance="high" Condition="'%(ProjectToScanOutput.Identity)' == 'test'" /> 
    </Target> 

的更多信息,請參考文章this

+0

完美,非常感謝 –