2016-04-26 90 views

回答

3

我以前使用MSBuild Community Tasks'sGetSolutionProjects爲此,但不幸的是它依賴於.NET 3.5。

要做到這一點使用CodeTask(因爲.NET 4個可用)執行以下操作:

<UsingTask TaskName="GetProjectsFromSolutionCodeTask" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" > 
    <ParameterGroup> 
     <Solution ParameterType="System.String" Required="true"/> 
     <Output ParameterType="Microsoft.Build.Framework.ITaskItem[]" Output="true"/> 
    </ParameterGroup> 
    <Task> 
     <Reference Include="System.Xml"/> 
     <Reference Include="Microsoft.Build"/> 
     <Using Namespace="Microsoft.Build.Construction"/> 
     <Code Type="Fragment" Language="cs"> 
     <![CDATA[ 
     var _solutionFile = SolutionFile.Parse(Solution); 
     Output = _solutionFile.ProjectsInOrder 
      .Where(proj => proj.ProjectType == SolutionProjectType.KnownToBeMSBuildFormat) 
      .Select(proj => new TaskItem(proj.AbsolutePath)) 
      .ToArray(); 
     ]]> 
     </Code> 
    </Task> 
    </UsingTask> 

並調用它像這樣:

<!-- Gets the projects composing the specified solution --> 
    <Target Name="GetProjectsFromSolution"> 
    <GetProjectsFromSolutionCodeTask Solution="%(Solution.Fullpath)"> 
     <Output ItemName="ProjectFiles" TaskParameter="Output"/> 
    </GetProjectsFromSolutionCodeTask > 
    </Target> 

這將填充ProjectFiles項集合與解決方案中所有項目的絕對路徑。

請注意:路徑爲CodeTaskFactory因MSBuild版本而異。這裏的例子是MSBuild 14.0。

相關問題