»MSBuildはてなブックマークに追加はてなブックマークを見る

.NET用ビルドエンジン「MSBuild」の使い方と、簡単なサンプル。

»MSBuildを使うための準備、パスを通す

「システムのプロパティ」→「詳細設定」→「環境変数」でPathに下記のパスを追加する。

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727

または、コマンドライン上で下記のコマンドを入力する。

@Set Path=C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727;%PATH%

パスを通したら、MSBuildが起動するか確かめる。

G:\msbuild>msbuild /version
Microsoft(R) Build Engine Version 2.0.50727.42
[Microsoft .NET Framework, Version 2.0.50727.42]
Copyright (C) Microsoft Corporation 2005. All rights reserved.

2.0.50727.42

上記のようにバージョンが表示されれば問題なし。

»まずはHello, world! (Messageタスク)

HelloWorld.msbuild.xmlというファイルを作成し、下記の内容を書き込む。

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
    DefaultTargets="Build">
    <Target Name="Build">
        <Message Text="Hello, world!"/>
    </Target>
</Project>

ファイルが作成できたら、msbuildに作成したファイルを渡して「ビルド」を実行させる。

G:\msbuild>msbuild HelloWorld.msbuild.xml

すると、下記のように表示される。

Microsoft(R) Build Engine Version 2.0.50727.42
[Microsoft .NET Framework, Version 2.0.50727.42]
Copyright (C) Microsoft Corporation 2005. All rights reserved.

ビルドが 2006/12/06 23:43:33 を開始しました。
__________________________________________________
プロジェクト "G:\msbuild\HelloWorld.msbuild.xml" (既定のターゲット):

ターゲット Build:
    Hello, world!

ビルドに成功しました。
    0 警告
    0 エラー

経過時間 00:00:00.35

上記のように「ビルド」結果が表示される。 ここでの要点は下記の通り。

»プロパティ(PropertyGroup)

プロパティ(PropertyGroup)を使った例(test1.msbuild.xml)

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
    DefaultTargets="Build">

    <PropertyGroup>
        <AssemblyName>Test1</AssemblyName>
        <Configuration>Debug</Configuration>
        <OutputType>WinExe</OutputType>
        <OutputPath>bin\Debug</OutputPath>
        <Hoge>Hage</Hoge>
    </PropertyGroup>

    <Target Name="Build">
        <Message Text="AssemblyName : $(AssemblyName)"/>
        <Message Text="Configuration : $(Configuration)"/>
        <Message Text="OutputType : $(OutputType)"/>
        <Message Text="OutputPath : $(OutputPath)"/>
        <Message Text="Hoge : $(Hoge)"/>
    </Target>
</Project>

実行結果は次の通り。

G:\msbuild>msbuild test1.msbuild.xml
Microsoft(R) Build Engine Version 2.0.50727.42
[Microsoft .NET Framework, Version 2.0.50727.42]
Copyright (C) Microsoft Corporation 2005. All rights reserved.

ビルドが 2006/12/06 23:54:29 を開始しました。
__________________________________________________
プロジェクト "G:\msbuild\test1.msbuild.xml" (既定のターゲット):

ターゲット Build:
    AssemblyName : Test1
    Configuration : Debug
    OutputType : WinExe
    OutputPath : bin\Debug
    Hoge : Hage

ビルドに成功しました。
    0 警告
    0 エラー

経過時間 00:00:00.04

PropertyGroup要素を用いるとプロパティを宣言出来る。 プロパティは一種の変数のようなものであり、PropertyGroupの子要素として任意の名前で定義することができる。 また、Messageタスク等でプロパティの値を参照する場合は、「$(プロパティ名)」とすることでその値を参照できる。

ここでの要点は下記の通り。

»ターゲット(Target)

ターゲット(Target)を使った例(test2.msbuild.xml)

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
    DefaultTargets="Clean;Build">

    <Target Name="Clean">
        <Message Text="Clean"/>
    </Target>
    
    <Target Name="BeforeBuild">
        <Message Text="BeforeBuild"/>
    </Target>

    <Target Name="Build">
        <CallTarget Targets="BeforeBuild"/>
        <Message Text="Build"/>
        <CallTarget Targets="AfterBuild"/>
    </Target>

    <Target Name="AfterBuild">
        <Message Text="AfterBuild"/>
    </Target>
</Project>

このファイルを用いて、オプションとして「/t:Clean」を付けて実行する。 実行結果は次の通り。

G:\msbuild>msbuild test2.msbuild.xml /t:Clean
Microsoft(R) Build Engine Version 2.0.50727.42
[Microsoft .NET Framework, Version 2.0.50727.42]
Copyright (C) Microsoft Corporation 2005. All rights reserved.

ビルドが 2006/12/07 0:01:46 を開始しました。
__________________________________________________
プロジェクト "G:\msbuild\test2.msbuild.xml" (Clean ターゲット):

ターゲット Clean:
    Clean

ビルドに成功しました。
    0 警告
    0 エラー

経過時間 00:00:00.01

続いて、同じファイルを用いて、今度はオプション無しで実行する。 実行結果は次の通り。

G:\msbuild>msbuild test2.msbuild.xml
Microsoft(R) Build Engine Version 2.0.50727.42
[Microsoft .NET Framework, Version 2.0.50727.42]
Copyright (C) Microsoft Corporation 2005. All rights reserved.

ビルドが 2006/12/07 0:02:02 を開始しました。
__________________________________________________
プロジェクト "G:\msbuild\test2.msbuild.xml" (既定のターゲット):

ターゲット Clean:
    Clean
ターゲット Build:
    ターゲット BeforeBuild:
        BeforeBuild
    Build
    ターゲット AfterBuild:
        AfterBuild

ビルドに成功しました。
    0 警告
    0 エラー

経過時間 00:00:00.04

/tオプションを使用すると、ビルドするターゲットを変更することが出来る。 最初の例では「Clean」をビルドのターゲットとしたため、「<Target Name="Clean">」で定義した動作、つまり「Clean」の出力が行われた。

続く二番目での例では、ビルドのターゲットを指定しなかった。 この場合、ProjectのDefaultTargetsに指定されているターゲットが使用される。 ここでは「Clean;Build」が設定されているが、これは「Cleanの後にBuildを実行する」という意味になる。

そのため、まず「Clean」で定義された動作が先に動き、続いて「Build」で定義された動作が動く。 さらに二番目の例では、BuildターゲットでCallTargetタスクを使用している。 これは他のターゲットを呼び出すものなので、Buildターゲットを動作させると1.BeforeBuildターゲットが動作する、2.「Build」を出力する、3.AfterBuildターゲットが動作する、という動きになる。

ここでの要点は下記の通り。

»依存関係の定義(DependsOnTargets)

TargetのDependsOnTargets属性を使用してターゲット同士の依存関係を定義した例(test3.msbuild.xml)

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
    DefaultTargets="Build">

    <Target Name="Clean">
        <Message Text="Clean"/>
    </Target>
    
    <Target Name="BeforeBuild">
        <Message Text="BeforeBuild"/>
    </Target>

    <Target Name="Build" DependsOnTargets="Clean;BeforeBuild">
        <Message Text="Build"/>
        <CallTarget Targets="AfterBuild"/>
    </Target>

    <Target Name="AfterBuild">
        <Message Text="AfterBuild"/>
    </Target>
</Project>

実行結果は次の通り。

G:\msbuild>msbuild test3.msbuild.xml
Microsoft(R) Build Engine Version 2.0.50727.42
[Microsoft .NET Framework, Version 2.0.50727.42]
Copyright (C) Microsoft Corporation 2005. All rights reserved.

ビルドが 2006/12/07 0:15:49 を開始しました。
__________________________________________________
プロジェクト "G:\msbuild\test3.msbuild.xml" (既定のターゲット):

ターゲット Clean:
    Clean
ターゲット BeforeBuild:
    BeforeBuild
ターゲット Build:
    Build
    ターゲット AfterBuild:
        AfterBuild

ビルドに成功しました。
    0 警告
    0 エラー

経過時間 00:00:00.04

これはTargetの例に似ているが、CallTargetの代わりにDependsOnTargets属性を指定して依存関係を定義している。 DependsOnTargets属性に任意のターゲットが指定されている場合は、そのターゲットが先に呼び出される。 その後、Target要素の中で定義されている動作が動く。

この例では、Buildターゲットを動作させようとする場合は、依存関係にあるCleanとBeforeBuildを順に呼び出してから、Buildで定義されている動作を順に実行していく。

ここでの要点は下記の通り。

»ターゲット・タスクを使った簡単なサンプル(Target, RemoveDir, MakeDir, Touch, Exec)

RemoveDir, MakeDir, Touch, Execなどのタスクを使った例(test4.msbuild.xml)

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
    DefaultTargets="Build">

    <PropertyGroup>
        <!-- 出力先 -->
        <OutputPath>bin\</OutputPath>
    </PropertyGroup>

    <Target Name="Clean">
        <RemoveDir Directories="$(OutputPath)" />
    </Target>

    <Target Name="Build">
        <MakeDir Directories="$(OutputPath)" Condition="!Exists('$OutputPath')" />
        <Touch Files="$(OutputPath)Test.txt" AlwaysCreate="True" />
        <Exec Command="date /t > $(OutputPath)Test.txt" />
    </Target>
</Project>

実行結果は次の通り。 まずはターゲットを指定しないでデフォルトであるBuildターゲットを実行させた場合。

G:\msbuild>msbuild test4.msbuild.xml
Microsoft(R) Build Engine Version 2.0.50727.42
[Microsoft .NET Framework, Version 2.0.50727.42]
Copyright (C) Microsoft Corporation 2005. All rights reserved.

ビルドが 2007/02/11 22:16:16 を開始しました。
__________________________________________________
プロジェクト "G:\msbuild\test4.msbuild.xml" (既定のターゲット):

ターゲット Build:
    ディレクトリ "bin\" を作成しています。
    "AlwaysCreate" が指定されたため "bin\Test.txt" を作成しています。
    date /t > bin\Test.txt

ビルドに成功しました。
    0 警告
    0 エラー

経過時間 00:00:03.17

続いてターゲットを指定してCleanターゲットを実行させた場合

G:\msbuild>msbuild test4.msbuild.xml /t:Clean
Microsoft(R) Build Engine Version 2.0.50727.42
[Microsoft .NET Framework, Version 2.0.50727.42]
Copyright (C) Microsoft Corporation 2005. All rights reserved.

ビルドが 2007/02/11 22:16:51 を開始しました。
__________________________________________________
プロジェクト "G:\msbuild\test4.msbuild.xml" (Clean ターゲット):

ターゲット Clean:
    ディレクトリ "bin\" を削除しています。

ビルドに成功しました。
    0 警告
    0 エラー

経過時間 00:00:00.70

この例では、Buildターゲットを動作させた場合は、出力用のディレクトリbinを作成し、bin\Test.txtにターゲットを動作させた時間を記録している。 またCleanターゲットを指定した場合は、出力用のディレクトリbinを削除するようになっている。

ここでの要点は下記の通り。

»アイテム(ItemGroup)

アイテム(ItemGroup)を使った例(test5.msbuild.xml)

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
    DefaultTargets="Build">

    <ItemGroup>
        <!-- 入力ファイルの一覧を作成する -->
        <TextFiles Include="*.txt" Exclude="backup*.txt"/>

        <SourceFiles Include="Main.cs;Form.cs"/>

        <ResourceFiles Include="Button.bmp"/>
        <ResourceFiles Include="Background.jpg"/>
        <ResourceFiles Include="Notice.wav"/>
    </ItemGroup>

    <Target Name="Build">
        <!-- ファイルの一覧を出力する -->
        <Message Text="TextFiles : @(TextFiles)"/>
        <Message Text="SourceFiles : @(SourceFiles)"/>
        <Message Text="ResourceFiles : @(ResourceFiles)"/>

        <!-- 区切り文字を変えて出力する -->
        <Message Text="ResourceFiles : @(ResourceFiles, ', ')"/>
        <Message Text="ResourceFiles : @(ResourceFiles, '/')"/>
    </Target>
</Project>

実行結果は次の通り。

G:\msbuild>msbuild test5.msbuild.xml
Microsoft(R) Build Engine Version 2.0.50727.42
[Microsoft .NET Framework, Version 2.0.50727.42]
Copyright (C) Microsoft Corporation 2005. All rights reserved.

ビルドが 2007/02/11 22:30:19 を開始しました。
__________________________________________________
プロジェクト "G:\msbuild\test5.msbuild.xml" (既定のターゲット):

ターゲット Build:
    TextFiles : test1.txt;test2.txt;test3.txt
    SourceFiles : Main.cs;Form.cs
    ResourceFiles : Button.bmp;Background.jpg;Notice.wav
    ResourceFiles : Button.bmp, Background.jpg, Notice.wav
    ResourceFiles : Button.bmp/Background.jpg/Notice.wav

ビルドに成功しました。
    0 警告
    0 エラー

経過時間 00:00:00.64

ItemGroupはPropertyGroupと似ているが、こちらは主にファイルのリストなどの「アイテム」を定義するために用いる。

ここでの要点は下記の通り。

»Cscタスクを使ってC#ファイルをコンパイルする

Cscタスクを用いると、cscコンパイラを使ってC#ソースファイルをコンパイルすることが出来る。 ここではいくつかのソースファイル、リソースファイルからWindows実行可能ファイルを生成するための例を示す。

Sponsored Link

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
    DefaultTargets="Build">

    <!-- プロパティを設定する -->
    <PropertyGroup>
        <!-- アセンブリ名 -->
        <AssemblyName>ScreenCapturer2</AssemblyName>

        <!-- 出力ファイルの種類 -->
        <TargetType>WinExe</TargetType>

        <!-- 構成の種類(指定されていない場合はデフォルトとしてDebugを設定する) -->
        <Configuration Condition="'$(Configuration)'==''">Debug</Configuration>
    </PropertyGroup>

    <!-- Debug構成 -->
    <PropertyGroup Condition="'$(Configuration)'=='Debug'">
        <!-- 出力先ディレクトリ -->
        <OutputPath>bin\Debug\</OutputPath>

        <!-- デバッグ情報出力あり -->
        <EmitDebugInformation>true</EmitDebugInformation>

        <!-- 条件付きコンパイルに適用するシンボル -->
        <DefineConstants>DEBUG;TRACE</DefineConstants>
    </PropertyGroup>

    <!-- Release構成 -->
    <PropertyGroup Condition="'$(Configuration)'=='Release'">
        <!-- 出力先ディレクトリ -->
        <OutputPath>bin\Release\</OutputPath>

        <!-- デバッグ情報出力なし -->
        <EmitDebugInformation>false</EmitDebugInformation>
    </PropertyGroup>

    <!-- 入力ファイルの一覧を作成する -->
    <ItemGroup>
        <!-- C#ソースファイル -->
        <CSCodeFile Include="*.cs"/>

        <!-- resxファイル -->
        <ResXFile Include="*.resx"/>

        <!-- 参照に追加するアセンブリ -->
        <References Include="System.dll"/>
        <References Include="System.Drawing.dll"/>
        <References Include="System.Windows.Forms.dll"/>
    </ItemGroup>

    <!-- クリーンを行うターゲット -->
    <Target Name="Clean">
        <RemoveDir Directories="$(OutputPath)"/>
    </Target>

    <!-- リソースの変換を行うターゲット -->
    <Target Name="Resources">
        <!-- 出力ディレクトリが無ければ、作成する -->
        <MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')"/>

        <!-- GenerateResourceタスクを使用してリソースを変換する -->
        <!-- http://msdn2.microsoft.com/ja-jp/library/ms164295(VS.80).aspx -->
        <GenerateResource
            Sources="@(ResXFile)"
            OutputResources="@(ResXFile->'$(OutputPath)%(Filename).resources')">
            <Output TaskParameter="OutputResources" ItemName="Resources"/>
        </GenerateResource>

        <!-- 出力ファイル名をログに出力する -->
        <Message Text="Output file(s): @(Resources)"/>
    </Target>

    <!-- ビルドを行う -->
    <Target Name="Build" DependsOnTargets="Resources">
        <!-- 構成名をログに出力する -->
        <Message Text="Configuration: $(Configuration)"/>

        <!-- 出力ディレクトリが無ければ、作成する -->
        <MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')"/>

        <!-- Cscタスクを使用してCSCodeFileで定義されているファイルをソースとして -->
        <!-- コンパイルする -->
        <!-- http://msdn2.microsoft.com/ja-jp/library/s5c8athz(VS.80).aspx -->
        <Csc
            Sources="@(CSCodeFile)"
            Resources="@(Resources)"
            References="@(References)"
            TargetType="$(TargetType)"
            EmitDebugInformation="$(EmitDebugInformation)"
            OutputAssembly="$(OutputPath)$(AssemblyName).exe">

            <!-- 出力ファイルをOutputFileという名前で格納する -->
            <Output TaskParameter="OutputAssembly" ItemName="OutputFile"/>
        </Csc>

        <!-- 出力ファイル名をログに出力する-->
        <Message Text="Output file(s): @(OutputFile)"/>
    </Target>

    <!-- ビルド後、実行する -->
    <Target Name="Run" DependsOnTargets="Build">
        <Exec Command="$(OutputPath)$(AssemblyName).exe"/>
    </Target>
</Project>

実行結果は次の通り。

C:\Documents and Settings\santa marta\My Documents\sample>msbuild sample.msbuild.xml /t:Run /p:Configuration=Release
Microsoft(R) Build Engine Version 2.0.50727.42
[Microsoft .NET Framework, Version 2.0.50727.42]
Copyright (C) Microsoft Corporation 2005. All rights reserved.

ビルドが 2007/02/11 23:17:28 を開始しました。
__________________________________________________
プロジェクト "C:\Documents and Settings\santa marta\My Documents\sample\sample.msbuild.xml" (Run ターゲット):

ターゲット Resources:
    ディレクトリ "bin\Release\" を作成しています。
    リソース ファイル "MainForm.resx" を "bin\Release\MainForm.resources" に処理しています。
    Output file(s): bin\Release\MainForm.resources
ターゲット Build:
    Configuration: Release
    C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Csc.exe /debug- /out:bin\Release\ScreenCapturer2.exe
/resource:bin\Release\MainForm.resources /target:winexe AssemblyInfo.cs HotKey.cs
MainForm.cs ScreenCapturer.cs ScreenShot.cs
    Output file(s): bin\Release\ScreenCapturer2.exe
ターゲット Run:
    bin\Release\ScreenCapturer2.exe

ビルドに成功しました。
    0 警告
    0 エラー

経過時間 00:00:03.89

ビルドに成功すると、コンパイルしたバイナリが起動し、ウィンドウが現れる。 ウィンドウを閉じてバイナリを終了すると、MSBuildの完了メッセージが出力される。

»プロジェクトファイル(csproj, vbproj)をビルドする

プロジェクトファイル(csproj, vbproj)をmsbuildの引数として渡すことで、 そのプロジェクトをビルドすることが出来る。

E:\msbuild>msbuild Sample.csproj
Microsoft(R) Build Engine Version 2.0.50727.312
[Microsoft .NET Framework, Version 2.0.50727.312]
Copyright (C) Microsoft Corporation 2005. All rights reserved.

ビルドが 2007/11/02 7:01:41 を開始しました。
__________________________________________________
プロジェクト "E:\msbuild\Sample.csproj" (既定のターゲット):

ターゲット PrepareForBuild:
    ディレクトリ "bin\Debug\" を作成しています。
    ディレクトリ "obj\Debug\" を作成しています。
ターゲット CoreCompile:
    C:\Windows\Microsoft.NET\Framework\v2.0.50727\Csc.exe /noconfig /nowarn:1701
,1702 /errorreport:prompt /warn:4 /define:DEBUG;TRACE /reference:C:\Windows\Micr
osoft.NET\Framework\v2.0.50727\System.dll /debug+ /debug:full /optimize- /out:ob
j\Debug\Sample.exe /target:exe Sample.cs
ターゲット CopyFilesToOutputDirectory:
    "obj\Debug\Sample.exe" から "bin\Debug\Sample.exe" へファイルをコピーしてい
ます。
    Sample -> E:\msbuild\bin\Debug\Sample.exe
    "obj\Debug\Sample.pdb" から "bin\Debug\Sample.pdb" へファイルをコピーしてい
ます。

ビルドに成功しました。
    0 警告
    0 エラー

経過時間 00:00:00.25

プロジェクトファイルはデフォルトのターゲットが「Build」になっているので、ターゲットを指定しない場合はビルドが行われる。 また、CleanやRunといったターゲットを指定することも出来る。 たとえば、クリーンアップしてからビルド・実行したい場合は、ターゲットとして「/t:Clean;Build;Run」とすることで一連のタスクを実行できる。

たとえば、

using System;

class Program
{
  static void Main(string[] args)
  {
    Console.WriteLine("run with MSBuild");
  }
}

このようなソースを含むプロジェクトファイルについて、ターゲットに「/t:Clean;Build;Run」を指定すると次のような実行結果となる。

E:\msbuild>msbuild Sample.csproj /t:Clean;Build;Run
Microsoft(R) Build Engine Version 2.0.50727.312
[Microsoft .NET Framework, Version 2.0.50727.312]
Copyright (C) Microsoft Corporation 2005. All rights reserved.

ビルドが 2007/11/02 7:05:29 を開始しました。
__________________________________________________
プロジェクト "E:\msbuild\Sample.csproj" (Clean;Build;Run ターゲット):

ターゲット CoreClean:
    ファイル "bin\Debug\Sample.exe" を削除しています。
    ファイル "bin\Debug\Sample.pdb" を削除しています。
    ファイル "obj\Debug\Sample.exe" を削除しています。
    ファイル "obj\Debug\Sample.pdb" を削除しています。
ターゲット CoreCompile:
    C:\Windows\Microsoft.NET\Framework\v2.0.50727\Csc.exe /noconfig /nowarn:1701
,1702 /errorreport:prompt /warn:4 /define:DEBUG;TRACE /reference:C:\Windows\Micr
osoft.NET\Framework\v2.0.50727\System.dll /debug+ /debug:full /optimize- /out:ob
j\Debug\Sample.exe /target:exe Sample.cs
ターゲット CopyFilesToOutputDirectory:
    "obj\Debug\Sample.exe" から "bin\Debug\Sample.exe" へファイルをコピーしてい
ます。
    Sample -> E:\msbuild\bin\Debug\Sample.exe
    "obj\Debug\Sample.pdb" から "bin\Debug\Sample.pdb" へファイルをコピーしてい
ます。
ターゲット Run:
    E:\msbuild\bin\Debug\Sample.exe
    run with MSBuild

ビルドに成功しました。
    0 警告
    0 エラー

経過時間 00:00:00.36

»ビルド構成を指定してプロジェクトファイルをビルドする

/property(または/p)を使用してConfigurationプロパティを指定することで、ビルド構成を指定してビルドすることが出来る。 たとえば、

using System;

class Program
{
  static void Main(string[] args)
  {
#if DEBUG
    Console.WriteLine("Debugビルドです");
#else
    Console.WriteLine("Releaseビルドです");
#endif
  }
}

このようなソースを含むプロジェクトファイルについて、「/p:Configuration=Release」を指定すると次のような実行結果となる。

E:\msbuild>msbuild Sample.csproj /t:Clean;Build;Run /p:Configuration=Release
    . 
    :
(途中省略)
    :
    . 
ターゲット CoreCompile:
    C:\Windows\Microsoft.NET\Framework\v2.0.50727\Csc.exe /noconfig /nowarn:1701
,1702 /errorreport:prompt /warn:4 /define:TRACE /reference:C:\Windows\Microsoft.
NET\Framework\v2.0.50727\System.dll /debug:pdbonly /optimize+ /out:obj\Release\S
ample.exe /target:exe Sample.cs
    . 
    :
(途中省略)
    :
    . 
ターゲット Run:
    E:\msbuild\bin\Release\Sample.exe
    Releaseビルドです

ビルドに成功しました。
    0 警告
    0 エラー

経過時間 00:00:00.36

デフォルトではConfigurationの値はDebugとなっているので、「/p:Configuration=Debug」と明示的に指定しなくてもDebug構成でのビルドが行われる。

»コンパイルスイッチを指定してプロジェクトファイルをビルドする

ビルド構成の場合と同様に、/property(または/p)でDefineConstantsプロパティを指定することで、コンパイルスイッチを指定することも出来る。 たとえば、

using System;

class Program
{
  static void Main(string[] args)
  {
#if DEBUG
    Console.WriteLine("DEBUGは有効です");
#endif

#if PLATFORM_WIN32
    Console.WriteLine("PLATFORM_WIN32は有効です");
#endif

#if PLATFORM_WINNT
    Console.WriteLine("PLATFORM_WINNTは有効です");
#endif
  }
}

このようなソースを含むプロジェクトファイルについて、プロパティに「/p:DefineConstants="DEBUG;PLATFORM_WIN32"」を指定すると次のような実行結果となる。

E:\msbuild>msbuild Sample.csproj /t:Clean;Build;Run /p:DefineConstants="DEBUG;PLATFORM_WIN32"
    . 
    :
(途中省略)
    :
    . 
ターゲット CoreCompile:
    C:\Windows\Microsoft.NET\Framework\v2.0.50727\Csc.exe /noconfig /nowarn:1701
,1702 /errorreport:prompt /warn:4 /define:DEBUG;PLATFORM_WIN32 /reference:C:\Win
dows\Microsoft.NET\Framework\v2.0.50727\System.dll /debug+ /debug:full /optimize
- /out:obj\Debug\Sample.exe /target:exe Sample.cs
    . 
    :
(途中省略)
    :
    . 
ターゲット Run:
    E:\msbuild\bin\Debug\Sample.exe
    DEBUGは有効です
    PLATFORM_WIN32は有効です

ビルドに成功しました。
    0 警告
    0 エラー

経過時間 00:00:00.38

»参考資料

Sponsored Link