目前,我让他遵循以下代码MSBuild
项目文件。这真的很简单。定义4变量并为每个变量调用一次我的 MSBuild 任务:
求代码~~
<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"/>
一点也不令人兴奋。
我想知道是否可以将其重构为这样的东西。
失败-伪代码~~
<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 中可以做到这一点吗?
因此,该任务被称为“每个文件一次”。或者更确切地说,“项目组中的每个项目一次”?
您可以使用项目元数据来批处理任务(请参阅http://msdn.microsoft.com/en-us/library/ms171474.aspx http://msdn.microsoft.com/en-us/library/ms171474.aspx).
所有项目都有称为“身份”的元数据,其中包含Include属性。如果您使用元数据引用语法%(Identity)
,这将指示 MSBuild 对每个唯一的 Include 值执行任务。
<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)
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)