如何从 msbuild 项目文件本身中使用不同的参数调用相同的 msbuild 目标两次 [英] How to invoke the same msbuild target twice with different parameters from within msbuild project file itself
问题描述
我有以下一段 msbuild 代码:
I have the following piece of msbuild code:
<PropertyGroup>
<DirA>C:DirA</DirA>
<DirB>C:DirB</DirB>
</PropertyGroup>
<Target Name="CopyToDirA"
Condition="Exists('$(DirA)') AND '@(FilesToCopy)' != ''"
Inputs="@(FilesToCopy)"
Outputs="@(FilesToCopy -> '$(DirA)%(Filename)%(Extension)')">
<Copy SourceFiles="@(FilesToCopy)" DestinationFolder="$(DirA)" />
</Target>
<Target Name="CopyToDirB"
Condition="Exists('$(DirB)') AND '@(FilesToCopy)' != ''"
Inputs="@(FilesToCopy)"
Outputs="@(FilesToCopy -> '$(DirB)%(Filename)%(Extension)')">
<Copy SourceFiles="@(FilesToCopy)" DestinationFolder="$(DirB)" />
</Target>
<Target Name="CopyFiles" DependsOnTargets="CopyToDirA;CopyToDirB"/>
因此调用目标 CopyFiles
会将相关文件复制到 $(DirA)
和 $(DirB)
,前提是它们不存在并且是最新的.
So invoking the target CopyFiles
copies the relevant files to $(DirA)
and $(DirB)
, provided they are not already there and up-to-date.
但是目标 CopyToDirA
和 CopyToDirB
看起来相同,除了一个复制到 $(DirA)
而另一个复制到 $(目录)
.是否可以将它们统一为一个目标,首先使用 $(DirA)
然后使用 $(DirB)
调用?
But the targets CopyToDirA
and CopyToDirB
look identical except one copies to $(DirA)
and the other - to $(DirB)
. Is it possible to unify them into one target first invoked with $(DirA)
and then with $(DirB)
?
谢谢.
推荐答案
你应该能够生成一个 ItemGroup 包含 Dirs 和 % .
You should be able to generate an ItemGroup containing the Dirs and then % on that.
<ItemGroup>
<Dirs Include="C:DirA;C:DirB">
</ItemGroup>
<Target Name="CopyFiles"
Condition="Exists('%(Dirs)') AND '@(FilesToCopy)' != ''"
Inputs="@(FilesToCopy)"
Outputs="@(FilesToCopy -> '%(Dirs)%(Filename)%(Extension)')">
<Copy SourceFiles="@(FilesToCopy)" DestinationFolder="%(Dirs)" />
</Target>
或者您可以进行 2 次显式调用:
Or you can do 2 explicit calls:
<Target Name="CopyFiles">
<MsBuild Projects="$(MSBuildProjectFullPath)" Targets="CopyASetOfFiles" Properties="FilesToCopy=@(FilesToCopy);DestDir=$(DirA)" />
<MsBuild Projects="$(MSBuildProjectFullPath)" Targets="CopyASetOfFiles" Properties="FilesToCopy=@(FilesToCopy);DestDir=$(DirB)" />
</Target>
<Target Name="CopyASetOfFiles"
Condition="Exists('$(DestDir)') AND '@(FilesToCopy)' != ''"
Inputs="@(FilesToCopy)"
Outputs="@(FilesToCopy -> '$(DestDir)%(Filename)%(Extension)')">
<Copy SourceFiles="@(FilesToCopy)" DestinationFolder="$(DestDir)" />
</Target>
我没有测试过任何一种语法,但对第二种语法相对更有信心.
I haven't tested either syntax, but am relatively more confident of the second.
(答案,如果有的话,在我桌子上的 Sayed Hashimi 书中 - 你必须等到第一个:
(The answer, if there is one, is in my Sayed Hashimi book on my desk - you'll have to wait until the first of:
- 拿书
- 我觉得无聊
- Sayed 找到了这篇文章并提出了一个经过测试的绝妙答案)
这篇关于如何从 msbuild 项目文件本身中使用不同的参数调用相同的 msbuild 目标两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!