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

问题描述

如何使用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 data\add_sql_cache.sql -b" />
    <Exec ContinueOnError="false" Command="sqlcmd -S $(ServerName) $(SqlServerUser) -d $(DbName) -v TableName=%22services%22 -i data\add_sql_cache.sql -b" />
    <Exec ContinueOnError="false" Command="sqlcmd -S $(ServerName) $(SqlServerUser) -d $(DbName) -v TableName=%22servicesGroup%22 -i data\add_sql_cache.sql -b" />
    <Exec ContinueOnError="false" Command="sqlcmd -S $(ServerName) $(SqlServerUser) -d $(DbName) -v TableName=%22servicesCategory%22 -i data\add_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语法异常直观,我不知道如何在循环中执行Exec任务,并使用上面的ItemGroup作为输入.

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 ItemGroups?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 09:01