如何两次调用同一msbuild目标? [英] How to invoke the same msbuild target twice?
问题描述
我有以下msbuild脚本:
I have the following msbuild script:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
DefaultTargets="All">
<PropertyGroup>
...
</PropertyGroup>
<UsingTask AssemblyFile="$(GallioTaskPath)" TaskName="Gallio.MSBuildTasks.Gallio" />
<Import Project="$(MSBuildCommunityTasksPath)\MSBuild.Community.Tasks.Targets" />
<ItemGroup>
...
</ItemGroup>
<Target Name="CheckServerHostsItemGroup"
Condition="'$(NoServerHosts)' != True">
...
</Target>
<Target Name="RunServerHosts" DependsOnTargets="CheckServerHostsItemGroup"
Condition="'$(NoServerHosts)' != True">
...
</Target>
<Target Name="KillServerHosts" DependsOnTargets="CheckServerHostsItemGroup"
Condition="'$(NoServerHosts)' != True">
...
</Target>
<Target Name="KillServerHosts2"
Condition="'$(NoServerHosts)' != True">
<MSBuild Projects="$(MSBuildProjectFile)" Targets="KillServerHosts" Properties="Configuration=$(Configuration)" />
</Target>
<Target Name="All" DependsOnTargets="Build;RunServerHosts;Test;KillServerHosts2">
<OnError ExecuteTargets="KillServerHosts2" />
</Target>
<Target Name="Test">
...
<Error Text="Tests execution failed with exit code $(ExitCode)" Condition="'$(ExitCode)' != 0" />
</Target>
<Target Name="CheckSolutionsItemGroup">
...
</Target>
<Target Name="Build" DependsOnTargets="CheckSolutionsItemGroup;KillServerHosts">
...
</Target>
<Target Name="Clean" DependsOnTargets="CheckSolutionsItemGroup;KillServerHosts">
...
</Target>
</Project>
我删除了目标的主体以节省空间,因为它们无关紧要.但是,如果有人认为自己是,那么我会把它们包括在内.
I have removed the bodies of the targets to save space and because they are irrelevant. However, if someone thinks they are, I will include them.
无论如何,正如人们所看到的,有一个目标-KillServerHosts
,我希望有两次调用,这是有充分理由的.在构建之前,一次是为了确保没有程序集被锁定,另一次是在测试完成后才能清理表".
Anyway, as one can see, there is one target - KillServerHosts
, which I wish to invoke twice and for a good reason. Once before the build, in order to ensure no assemblies are locked and the other time after the tests complete to "cleanup the table".
自然,尝试两次调用目标KillServerHosts
无效,MSBuild拒绝两次构建同一目标.因此,我试图通过调用目标KillServerHosts2
作弊.问题在于有时它可以工作,有时却不能.
Naturally, trying to invoke the target KillServerHosts
twice does not work, MSBuild refuses to build the same target twice. So, I am trying to cheat by invoking the target KillServerHosts2
. The problem is that sometimes it works and sometimes it does not.
如果我从命令行运行KillServerHosts
目标,它总是会杀死相关的服务器主机,因此从这一方面来看,它可以正常工作.
If I run the KillServerHosts
target from the command line it always kills the relevant server hosts, so from this respect it works fine.
有什么想法吗?
推荐答案
根据设计,MSBuild目标不会多次执行.您不应将MSBuild目标视为方法或函数,因为MSBuild不是一种功能编程语言.
By design MSBuild targets are not executed more than once. You should not think of an MSBuild target as a method or function, bucause MSBuild is not a functional programming language.
如果您想多次执行目标,则可以使用MSBuild任务执行此操作,只需传入一组不同的属性即可.例如这样的东西
If you want to execute a target more than once you can use the MSBuild task to do so, just pass in a different set of properties. For example something like this
<Target Name="SomeTarget">
<MSBuild Projects="$(MSBuildProjectFile)" Targets="YourTarget" Properties="FakeProperty=one" />
<MSBuild Projects="$(MSBuildProjectFile)" Targets="YourTarget" Properties="FakeProperty=two" />
</Target>
注意:我实际上并没有尝试过,所以如果我拼写错误或记错了,可能会出现语法问题,但是您应该能够使它正常工作.
Note: I didn't actually try this out so there might be syntax issues if I mis-spelled or remembered incorrectly but you should be able to get it working.
这篇关于如何两次调用同一msbuild目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!