如何从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)
,另一个副本复制到$(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)
?
谢谢.
推荐答案
您应该能够生成包含Dirs,然后包含%的ItemGroup.
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屋!