我可以在MSBuild文件中执行循环吗? [英] Can I do a loop in an MSBuild file?

查看:241
本文介绍了我可以在MSBuild文件中执行循环吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我已经在 MSBuild proj文件中跟踪代码了。这很简单。定义4个变量并按变量调用我的MSBuild任务:

Currently, I've got he following code in an MSBuild proj file. It's really simple. Define 4 variables and call my MSBuild Task once-per-variable :

<ItemGroup><JS_File1 Include="file1.js"/></ItemGroup>
<ItemGroup><JS_File1 Include="file2.js"/></ItemGroup>
<ItemGroup><JS_File1 Include="file3.js"/></ItemGroup>
<ItemGroup><JS_File1 Include="file4.js"/></ItemGroup>

<JavaScriptCompressorTask SourceFiles="@(JS_File1)" OutputFile="@(JS_File1).min"/>
<JavaScriptCompressorTask SourceFiles="@(JS_File2)" OutputFile="@(JS_File2).min"/>
<JavaScriptCompressorTask SourceFiles="@(JS_File3)" OutputFile="@(JS_File3).min"/>
<JavaScriptCompressorTask SourceFiles="@(JS_File4)" OutputFile="@(JS_File4).min"/>

没有什么令人兴奋的。

I我想知道这是否可以重构为这样的事情。

I was wondering if this could be refactored to something like this.

<ItemGroup>
    <JS_File1 Include="file1.js"/>
    <JS_File1 Include="file2.js"/>
    <JS_File1 Include="file3.js"/>
    <JS_File1 Include="file4.js"/>
</ItemGroup>

<!-- now this is the shiz i have no idea about -->
foreach(@(JS_Files))
    <JavaScriptCompressorTask SourceFiles="@(theFile)" OutputFile="@(theFile).min"/>

是否可以在MSBuild中执行此操作?

Is it possible to do this, in MSBuild?

这样就可以每个文件调用一次..或者更多点到每个项目一次的项目?

推荐答案

您可以使用项目元数据批处理任务(请参阅 http://msdn.microsoft.com/en-us/library/ms171474.aspx )。

You can use item metadata to batch the task (see http://msdn.microsoft.com/en-us/library/ms171474.aspx).

所有项目都有称为元数据身份,其中包含包含属性的值。如果使用元数据引用语法%(Identity),则会指示MSBuild为每个唯一的Include值执行任务。

All items have metadata called 'Identity,' which contains the value of the Include attribute. If you use metadata reference syntax %(Identity), that will instruct MSBuild to execute your task for each unique Include value.

<ItemGroup>
    <JS_File1 Include="file1.js"/>
    <JS_File1 Include="file2.js"/>
    <JS_File1 Include="file3.js"/>
    <JS_File1 Include="file4.js"/>
</ItemGroup>

<JavaScriptCompressorTask SourceFiles="@(JS_File1)" OutputFile="%(Identity).min"/>

请注意,MSBuild知道您正在引用 JS_File1 的标识元数据项目组,因为您在任务中引用它。否则,您需要使用语法%(JS_File1.Identity)

Note that MSBuild knows that you are referencing the Identity metadata of the JS_File1 item group because you reference it in the task. Otherwise you would need to use the syntax %(JS_File1.Identity).

这篇关于我可以在MSBuild文件中执行循环吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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