MSBuild中的PreBuildEvent,BeforeBuild目标和BeforeCompile目标之间有什么区别? [英] What is the difference between a PreBuildEvent, BeforeBuild target and BeforeCompile target in MSBuild?

查看:270
本文介绍了MSBuild中的PreBuildEvent,BeforeBuild目标和BeforeCompile目标之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近不得不从解决方案

可以在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,而可以使用Before-, AfterBuild修改Build.

选择要在哪种情况下覆盖的Target取决于构建期间要执行给定任务的时间.这些目标对于可以实现的目标没有特定的限制和/或好处.除了它们可以适应由上一步定义/填充的ItemGroup或属性这一事实之外.

在构建尝试解决项目依赖关系之前,最好使用nuget引入软件包.因此,BeforeCompile并不是此类操作的理想选择.

我希望这可以阐明问题.在 MSDN

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?

解决方案

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

这篇关于MSBuild中的PreBuildEvent,BeforeBuild目标和BeforeCompile目标之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆