How do you recursively call tasks in MSBuild?

2019-05-10 16:08发布

E.g. I want to call these scripts passing diff params each time:

<ItemGroup> 
        <SqlFiles Include="$(SourceServicesDbSqlRootDir)CreateScripts\$(SourceServiceName)\Tables\*.sql" /> 
        <SqlFiles Include="$(SourceServicesDbSqlRootDir)CreateScripts\$(SourceServiceName)\Functions\*.sql" /> 
        <SqlFiles Include="$(SourceServicesDbSqlRootDir)CreateScripts\$(SourceServiceName)\Views\*.sql" /> 
        <SqlFiles Include="$(SourceServicesDbSqlRootDir)CreateScripts\$(SourceServiceName)\ForeignKeys\*.sql" /> 
        <SqlFiles Include="$(SourceServicesDbSqlRootDir)CreateScripts\$(SourceServiceName)\StoredProcedures\*.sql" /> 
        <SqlFiles Include="$(SourceServicesDbSqlRootDir)CreateScripts\$(SourceServiceName)\Data\*.sql" /> 
</ItemGroup> 

1条回答
地球回转人心会变
2楼-- · 2019-05-10 16:36

You could to that by using MSBuild Task with Properties.

<!-- Dont itemize sql files now, if you want to differenciate the task operations -->
<ItemGroup> 
  <SqlDirs Include="$(SourceServicesDbSqlRootDir)CreateScripts\$(SourceServiceName)\Tables" /> 
  <SqlDirs Include="$(SourceServicesDbSqlRootDir)CreateScripts\$(SourceServiceName)\Functions" /> 
  <SqlDirs Include="$(SourceServicesDbSqlRootDir)CreateScripts\$(SourceServiceName)\Views" /> 
  <SqlDirs Include="$(SourceServicesDbSqlRootDir)CreateScripts\$(SourceServiceName)\ForeignKeys" /> 
  <SqlDirs Include="$(SourceServicesDbSqlRootDir)CreateScripts\$(SourceServiceName)\StoredProcedures" /> 
  <SqlDirs Include="$(SourceServicesDbSqlRootDir)CreateScripts\$(SourceServiceName)\Data" /> 
</ItemGroup>


<Target Name="MainTask">
  <MSBuild Projects="$(MSBuildProjectFile)" 
           Properties="SqlDir=%(SqlDirs.Fullpath)"
           Targets="RecursivelyCalledTask">
    <Output ItemName="ProjSources" TaskParameter="TargetOutputs"/>
  </MSBuild>
</Target>

<Target Name="RecursivelyCalledTask">
  <!-- We create here the SqlFiles items based on SqlDir-->
  <CreateItem Include="$(SqlDir)\*.sql">
    <Output ItemName="SqlFiles" TaskParameter="Include"/>
  </CreateItem>

  <Message Text="SqlFiles -> @(SqlFiles)"/>
</Target>
查看更多
登录 后发表回答