我最近不得不从PreBuildEvent in Visual Studio into the BeforeBuild target to make it work on AppHarbor移动一些代码。在这样做的同时,我也注意到了一个BeforeCompile目标。
这三个看似相似的事件有什么区别:PreBuildEvent,BeforeBuild Target,BeforeCompileTarget?
每个人可以/不可以做什么,为什么你会选择一个而不是另一个?
答案 0 :(得分:84)
这个问题的答案可以在Microsoft.Common.targets
文件中找到,该文件可以找到(取决于您使用的是64位还是32位框架):C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Microsoft.Common.target
64比特和
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets
表示32位运行时。此文件定义项目构建所经历的所有步骤。引用来源:
<!--
============================================================
Build
The main build entry point.
============================================================
-->
<PropertyGroup>
<BuildDependsOn>
BeforeBuild;
CoreBuild;
AfterBuild
</BuildDependsOn>
</PropertyGroup>
该代码非常适合解释在两个目标的评论中使用BeforeBuild
和AfterBuild
目标。
<!--
============================================================
BeforeBuild
Redefine this target in your project in order to run tasks just before Build
============================================================
-->
<Target Name="BeforeBuild"/>
<!--
============================================================
AfterBuild
Redefine this target in your project in order to run tasks just after Build
============================================================
-->
<Target Name="AfterBuild"/>
接下来是CoreBuild
目标的定义:
<PropertyGroup>
<CoreBuildDependsOn>
BuildOnlySettings;
PrepareForBuild;
PreBuildEvent;
ResolveReferences;
PrepareResources;
ResolveKeySource;
Compile;
UnmanagedUnregistration;
GenerateSerializationAssemblies;
CreateSatelliteAssemblies;
GenerateManifests;
GetTargetPath;
PrepareForRun;
UnmanagedRegistration;
IncrementalClean;
PostBuildEvent
</CoreBuildDependsOn>
</PropertyGroup>
因此Build
目标只是CoreBuild
目标的包装器,使您能够在CoreBuild
目标之前或之后执行自定义步骤。如上所示,PreBuildEvent
和PostBuildEvent
被列为CoreBuild
目标的依赖关系。 Compile
目标的依赖关系定义如下:
<PropertyGroup>
<CompileDependsOn>
ResolveReferences;
ResolveKeySource;
SetWin32ManifestProperties;
_GenerateCompileInputs;
BeforeCompile;
_TimeStampBeforeCompile;
CoreCompile;
_TimeStampAfterCompile;
AfterCompile
</CompileDependsOn>
</PropertyGroup>
再次BeforeCompile
和AfterCompile
在代码中发表了评论:
<!--
============================================================
BeforeCompile
Redefine this target in your project in order to run tasks just before Compile.
============================================================
-->
<Target Name="BeforeCompile"/>
<!--
============================================================
AfterCompile
Redefine this target in your project in order to run tasks just after Compile.
============================================================
-->
<Target Name="AfterCompile"/>
鉴于此信息,我不知道为什么AppHarbor不支持Pre-, PostBuildEvent
,而Build
可以使用Before-, AfterBuild
进行修改。
选择要覆盖哪个Target
哪个场景取决于您希望执行给定任务的构建期间的时刻。目标对于他们可以完成的任务没有特定的限制和/或好处。除了他们可以调整ItemGroup
或由前面步骤定义/填充的属性之外。
使用nuget引入包可能最好在构建尝试解决项目依赖项之前执行。所以BeforeCompile
不适合采取这种行动。
我希望这能够解释这个问题。在MSDN
上找到了另一个很好的解释