如何在“循环"中执行 EXEC 任务?与 MSBuild 项目组? [英] How do I perform the EXEC task in a "loop" with MSBuild ItemGroups?

查看:14
本文介绍了如何在“循环"中执行 EXEC 任务?与 MSBuild 项目组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 MSBuild ItemGroups 在循环"中执行 EXEC 任务?

How do I perform the EXEC task in a "loop" with MSBuild ItemGroups?

而不是像这样一遍又一遍地重复这个命令:

Instead of repeating this command over and over, like so:

    <Exec ContinueOnError="false" Command="sqlcmd -S $(ServerName) $(SqlServerUser) -d $(DbName) -v TableName=%22account%22 -i dataadd_sql_cache.sql -b" />
    <Exec ContinueOnError="false" Command="sqlcmd -S $(ServerName) $(SqlServerUser) -d $(DbName) -v TableName=%22services%22 -i dataadd_sql_cache.sql -b" />
    <Exec ContinueOnError="false" Command="sqlcmd -S $(ServerName) $(SqlServerUser) -d $(DbName) -v TableName=%22servicesGroup%22 -i dataadd_sql_cache.sql -b" />
    <Exec ContinueOnError="false" Command="sqlcmd -S $(ServerName) $(SqlServerUser) -d $(DbName) -v TableName=%22servicesCategory%22 -i dataadd_sql_cache.sql -b" />

我宁愿定义一个 ItemGroup 并执行一个循环".我已经关闭了 ItemGroup:

I'd rather define an ItemGroup and just execute a "loop". I've got the ItemGroup down:

<ItemGroup>
    <CachedTables Include="account" />
    <CachedTables Include="services" />
    <CachedTables Include="servicesGroup" />
    <CachedTables Include="servicesCategory" />

但由于 MSBuild 的语法非常不直观,我不知道如何以上面的 ItemGroup 作为输入在循环中执行 Exec 任务.

But due to MSBuild's amazingly unintuitive syntax, I have no idea how to perform the Exec task in a loop with the ItemGroup above as an input.

推荐答案

有两种方法,都是批处理"的形式

There are two ways to do this, both are forms of "batching"

您可以批处理一个目标并执行 Exec 和其他操作,

You can batch a target and perform the Exec and other operations,

<Target Name="ExecMany"
  Outputs="%(CachedTables.Identity)">
  <Exec
    Command="sqlcmd -S ... TableName=%22%(CachedTables.Identity)%22 -i ..."
    />
  <SomeOtherTask ThatUses="%(CachedTables.Identity)" />
</Target>

另一种是使用任务批处理,就在Exec任务上.很相似,

The other is to use task batching, just on the Exec task. It is similar,

<Target Name="ExecMany">
  <Exec
    Command="sqlcmd -S ... TableName=%22%(CachedTables.Identity)%22 -i ..."
    />
  <SomeOtherTask ThatUses="%(CachedTables.Identity)" />
</Target>

不同之处在于它们的运作方式.第一个,因为批处理是针对整个目标(通过 Outputs 属性实现的),Exec 任务,然后 SomeOtherTask 将为组中的每个项目执行.换句话说,

The difference is how these will operate. With the first, since the batching is for the whole target (achieved with the Outputs attribute), the Exec task, then the SomeOtherTask will execute for each item in the group. In other words,

Exec with "account"
SomeOtherTask with "account"
Exec with "services"
SomeOtherTask with "services"
...

第二个选项,分别批处理每个任务,将产生以下序列,

The second options, batching each task separately, would produce the following sequence,

Exec with "account"
Exec with "services"
...
SomeOtherTask with "account"
SomeOtherTask with "services"
...

这篇关于如何在“循环"中执行 EXEC 任务?与 MSBuild 项目组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆