54

我最近不得不将一些代码从Visual Studio 中的 PreBuildEvent 移动到 BeforeBuild 目标中,以使其在 AppHarbor 上工作。这样做时,我还注意到了一个 BeforeCompile 目标。

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

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

4

1 回答 1

98

可以在以下Microsoft.Common.targets文件中找到此问题的答案(取决于您使用的是 64 位还是 32 位框架):C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Microsoft.Common.targetfor 64-bit and C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targetsfor the 32-bit runtime。该文件定义了项目构建所经历的所有步骤。引用来源:

<!--
============================================================
                                    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-, PostBuildEventBuild可以使用Before-, AfterBuild.

选择Target要覆盖哪个场景取决于构建期间您希望执行给定任务的时刻。目标对他们可以完成的事情没有具体的限制和/或好处。除了他们可以调整ItemGroup' 或 由先前步骤定义/填充的属性这一事实之外。

使用 nuget 引入包可能最好在构建尝试解决项目依赖关系之前执行。所以BeforeCompile不是这种行动的好人选。

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

于 2011-05-08T12:47:36.103 回答