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

发布于 2024-11-05 17:45:36 字数 414 浏览 4 评论 0原文

我最近不得不从 将 Visual Studio 中的 PreBuildEvent 放入 BeforeBuild 目标中,使其在 AppHarbor 上运行。在这样做的同时,我还注意到了一个 BeforeCompile 目标。

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

每种方法可以做什么/不可以做什么?为什么你会选择其中一种而不是另一种?

I recently had to move some code from a PreBuildEvent in Visual Studio into the BeforeBuild target to make it work on AppHarbor. While doing so, I also noticed a BeforeCompile target.

What is the difference between these three seemingly similar events: PreBuildEvent, BeforeBuild Target, BeforeCompileTarget?

What can/can't be done with each, and why would you pick one over another?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

み零 2024-11-12 17:45:36

这个问题的答案可以在 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 进行修改。

选择要覆盖哪个场景的目标取决于构建期间您希望执行给定任务的时刻。这些目标对其能够实现的目标没有具体的限制和/或好处。除此之外,他们还可以调整由前面的步骤定义/填充的 ItemGroup 或属性。

在构建尝试解决项目依赖关系之前,最好使用 nuget 引入包。因此,BeforeCompile 不适合执行此类操作。

我希望这能让我们对此事有所了解。在 MSDN 上找到了另一个很好的解释

The answer to this question can be found in the Microsoft.Common.targets file which can be found (depending on wether you're using the 64-bit or 32-bit framework) at: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Microsoft.Common.target for 64-bit and
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets for the 32-bit runtime. This file defines all the steps a build of your project undergoes. Quoting the source:

<!--
============================================================
                                    Build

The main build entry point.
============================================================
-->
<PropertyGroup>
    <BuildDependsOn>
        BeforeBuild;
        CoreBuild;
        AfterBuild
    </BuildDependsOn>
</PropertyGroup>

The code is nice enough to explain the use of the BeforeBuild and AfterBuild target in the comments for both targets.

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

This is followed by the definition of the CoreBuild target:

<PropertyGroup>
    <CoreBuildDependsOn>
        BuildOnlySettings;
        PrepareForBuild;
        PreBuildEvent;
        ResolveReferences;
        PrepareResources;
        ResolveKeySource;
        Compile;
        UnmanagedUnregistration;
        GenerateSerializationAssemblies;
        CreateSatelliteAssemblies;
        GenerateManifests;
        GetTargetPath;
        PrepareForRun;
        UnmanagedRegistration;
        IncrementalClean;
        PostBuildEvent
    </CoreBuildDependsOn>
</PropertyGroup>

So the Build target is just a wrapper around the CoreBuild target to enable you to perform custom steps just before or after the CoreBuild target. As can be seen above the PreBuildEvent and PostBuildEvent are listed as dependencies of the CoreBuild target. The dependencies of the Compile target are defined as follows:

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

Again BeforeCompile and AfterCompile are commented in the code:

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

Given this information I do not know why AppHarbor does not support Pre-, PostBuildEvent while the Build can be modified using Before-, AfterBuild.

Choosing which Target to override for which scenario depends on the moment during the build at which you wish to perform your given task. The targets do not have specific restrictions and/or benefits as to what they can accomplish. Apart from the fact that they can adapt ItemGroup's or properties that were defined/filled by previous steps.

Using nuget to bring in packages is probably best performed before the build tries to resolve the projects dependencies. So BeforeCompile is not a good candidate for this kind of action.

I hope this sheds some light on the matter. Found another nice explanation on MSDN

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文