MSBuild中的PreBuildEvent,BeforeBuild目标和BeforeCompile目标之间有什么区别?

时间:2011-05-08 07:58:59

标签: visual-studio visual-studio-2010 msbuild msbuild-task appharbor

我最近不得不从PreBuildEvent in Visual Studio into the BeforeBuild target to make it work on AppHarbor移动一些代码。在这样做的同时,我也注意到了一个BeforeCompile目标。

这三个看似相似的事件有什么区别:PreBuildEvent,BeforeBuild Target,BeforeCompileTarget?

每个人可以/不可以做什么,为什么你会选择一个而不是另一个?

1 个答案:

答案 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>

该代码非常适合解释在两个目标的评论中使用BeforeBuildAfterBuild目标。

<!--
============================================================
                                    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目标之前或之后执行自定义步骤。如上所示,PreBuildEventPostBuildEvent被列为CoreBuild目标的依赖关系。 Compile目标的依赖关系定义如下:

<PropertyGroup>
    <CompileDependsOn>
        ResolveReferences;
        ResolveKeySource;
        SetWin32ManifestProperties;
        _GenerateCompileInputs;
        BeforeCompile;
        _TimeStampBeforeCompile;
        CoreCompile;
        _TimeStampAfterCompile;
        AfterCompile
    </CompileDependsOn>
</PropertyGroup>

再次BeforeCompileAfterCompile在代码中发表了评论:

<!--
============================================================
                                    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

上找到了另一个很好的解释