MSBuild
.SUFFIXES: .cpp .vcproj .obj .exe
.PHONY: all test mmm clean
TARGET=TaskSchedulerExplorer
all: $(TARGET).exe
$(TARGET).exe: $(TARGET).vcproj
msbuild.exe $<
test:
./$(TARGET).exe
clean:
msbuild.exe /t:Clean
~
-
Build a makefile by typing nmake.
-
Build a .csproj file by typing msbuild mysample.csproj.
-
Build a .vbproj file by typing msbuild mysample.vbproj.
-
Build a .vcproj by typing vcbuild mysample.vcproj.
note
MSDN:MSBuild タスク リファレンス
- msbuild.exeのコマンドライン引数には「*.*proj」という名前のMSBuildファイル(例えば「ConsoleApplication1.csproj」や「ConsoleApplication1.vbproj」などのVS 2005のプロジェクト・ファイルや、独自に命名した「ConsoleApplication1.myproj」など)を指定する
- VS 2005のソリューション・ファイル(=「*.sln」ファイル)もMSBuildからビルドできる(ただし、ソリューション・ファイルはMSBuildファイルではない)
- ソリューション・ファイルから変換されたMSBuildファイルを出力する方法
Set MSBuildEmitSolution=1
msbuild ConsoleApplication1\ConsoleApplication1.sln
- Releaseモードのビルド
msbuild ConsoleApplication1.csproj
/property:Configuration=Release
- プロジェクトをリビルド
msbuild ConsoleApplication1.csproj
/target:Rebuild
msbuild ConsoleApplication1.csproj /target:Clean;Build
- 最終的な出力ファイルを実行
msbuild ConsoleApplication1.csproj /target:Run
- プロジェクトのClickOnce発行
msbuild ConsoleApplication1.csproj /target:Publish
- モジュール(=ビルドにより出力するプログラム・ファイル)ごとに1つの<Project>タグが必要
- MSBuildファイル1つにつき、<Project>タグは1つしか記述できない
myproj
<?xml version="1.0" encoding="utf-8" ?>
<Project
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Build">
……タスクの記述……
</Target>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<PropertyGroup>
<AssemblyName>ConsoleApplication1</AssemblyName>
<RootNamespace>ConsoleApplication1</RootNamespace>
<OutputType>Exe</OutputType>
<Configuration>Debug</Configuration>
<Platform>AnyCPU</Platform>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<OutputPath>bin\Debug\</OutputPath>
</PropertyGroup>
<PropertyGroup>
<Configuration
Condition="
'$(Configuration)' == '' ">Debug</Configuration>
<AssemblyName>ConsoleApplication1</AssemblyName>
<RootNamespace>ConsoleApplication1</RootNamespace>
<OutputType>Exe</OutputType>
<Platform>AnyCPU</Platform>
<DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>
<PropertyGroup
Condition=" '$(Configuration)' == 'Debug' ">
<OutputPath>bin\Debug\</OutputPath>
</PropertyGroup>
<PropertyGroup
Condition=" '$(Configuration)' == 'Release' ">
<OutputPath>bin\Release\</OutputPath>
</PropertyGroup>
<Import
Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
reference
- おまじない
<Project
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
</Project>
|
- Target を定義する
<Project
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="brabra">
:
</Target>
</Project>
|
- 規定のC#ターゲットのインポートで済ませる場合
<Project
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
|
- デフォルト・ターゲットの指定
<Project
DefaultTargets="Build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
|
- モジュールを使うターゲットの例
<Project DefaultTargets="ModuleCompilation" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration>Debug</Configuration>
<Platform>AnyCPU</Platform>
<OutputPath>bin\</OutputPath>
</PropertyGroup>
<Target Name="ModuleCompilation">
<MakeDir Directories= "$(OutputPath)"/>
<Csc Sources="MyModule.cs" TargetType="Module"
OutputAssembly="$(OutputPath)\MyModule.netmodule" />
<Csc Sources="ConsoleApplication1.cs" TargetType="Exe"
AddModules="$(OutputPath)\MyModule.netmodule"
OutputAssembly="$(OutputPath)\ConsoleApplication1.exe" />
</Target>
|
- ソースの指定
<Project
DefaultTargets="Build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
|
- リファレンスの指定
<Project
DefaultTargets="Build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
</Project>
|
- プロパティの設定
<Project
DefaultTargets="Build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<!-- アセンブリ名 -->
<AssemblyName>ConsoleApplication1</AssemblyName>
<!-- 既定の名前空間 -->
<RootNamespace>ConsoleApplication1</RootNamespace>
<!-- 出力の種類 -->
<OutputType>Exe</OutputType>
<!-- 構成 -->
<Configuration>Debug</Configuration>
<!-- プラットフォーム -->
<Platform>AnyCPU</Platform>
<!-- 条件付きコンパイル・シンボル -->
<DefineConstants>DEBUG;TRACE</DefineConstants>
<!-- 出力パス -->
<OutputPath>bin\Debug\</OutputPath>
</PropertyGroup>
</Project>
|
MSBuildのプロパティ
MSBuildフォルダ内Microsoft.Build.Commontypes.xsdファイルのコメントPROPERTIESを見る
|
予約済みプロパティ |
説明 |
例 |
MSBuildProjectDirectory |
プロジェクトファイルの絶対パス |
C:\MyFolder |
MSBuildProjectFile |
プロジェクトファイル名 |
My.proj |
MSBuildProjectExtension |
プロジェクトファイルの拡張子 |
.proj |
MSBuildProjectFullPath |
プロジェクトファイルの絶対パスとファイル名 |
C:\MyFolder\My.proj |
MSBuildProjectName |
プロジェクトファイル名(拡張子なし) |
My |
MSBuildBinPath |
「MSBuild.exe」の絶対パス |
C:\WINDOWS\Microsoft.NET\ Framework\v2.0.50727 |
MSBuildProjectDefaultTargets |
DefaultTargets属性で指定されたターゲット |
<ProjectDefaultTargets="A;B">の場合、「A;B」 |
MSBuildExtensionsPath |
「Program Files」下の「MSBuild」フォルダ |
<ImportProject= "$(MSBuildExtensionsPath) \MyFiles\OtherCustom.targets"/> |
<Target Name="build"
>
<CSC
Sources="@(CSFile)"
References="@(Reference)"
OutputAssembly="$(ProjectName).exe"
TargetType="winexe" />
</Target>
<Target Name="run"
DependsOnTargets="build">
<Exec
Command="$(ProjectName).exe"/>
<Message
Text="アプリケーションを実行しました。" />
</Target>
Include File パス追加
<Tool
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories=";"
PreprocessorDefinitions="WIN32;_DEBUG;_LIB"
MinimalRebuild="TRUE"
BasicRuntimeChecks="0"
RuntimeLibrary="1"
RuntimeTypeInfo="FALSE"
WarningLevel="3"
DebugInformationFormat="4"/>