54

最近、一部のコードをVisual Studio の PreBuildEvent から BeforeBuild ターゲットに移動して、 AppHarbor で動作させる必要がありました。そうしているうちに、BeforeCompile ターゲットにも気付きました。

PreBuildEvent、BeforeBuild Target、BeforeCompileTarget という、一見似ているように見える 3 つのイベントの違いは何ですか?

それぞれでできることとできないことは何ですか。

4

1 に答える 1

98

この質問への回答は、64ビットおよび 32ビットランタイムのMicrosoft.Common.targets場合に(64ビットまたは32ビットフレームワークを使用しているかどうかに応じて)見つけることができるファイルにあります。このファイルは、プロジェクトのビルドが実行するすべてのステップを定義します。ソースを引用する:C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Microsoft.Common.targetC:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets

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

繰り返しますが、コード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"/>

Pre-, PostBuildEventこの情報を考えると、Buildを使用して変更できるのにAppHarborがサポートされない理由がわかりませんBefore-, AfterBuild

どのシナリオでどちらTargetをオーバーライドするかは、ビルド中の特定のタスクを実行する瞬間によって異なります。ターゲットには、達成できることに関して特定の制限や利点はありません。ItemGroup前のステップで定義/入力されたプロパティを適応させることができるという事実は別として。

nugetを使用してパッケージを取り込むのは、ビルドがプロジェクトの依存関係を解決しようとする前に実行するのがおそらく最適です。したがってBeforeCompile、この種のアクションの適切な候補ではありません。

これが問題にいくらかの光を当てることを願っています。MSDNで別の素晴らしい説明を見つけました

于 2011-05-08T12:47:36.103 に答える