-
Notifications
You must be signed in to change notification settings - Fork 0
MS_MSBuild
-
nmake ツールの(機能的な意味での).NET 版
-
Visual Studio プロジェクトを IDE 無しでビルドできる。
-
MSBuild をコマンドラインで呼び出してビルドできる。
- MSBuild コマンド ライン リファレンス
https://learn.microsoft.com/ja-jp/visualstudio/msbuild/msbuild-command-line-reference
MSBuild /build release solution.sln - MSBuild コマンド ライン リファレンス
-
タスクを利用すると種々の作業を自動化できる。
補足(
.csprojは MSBuild のスクリプトである): 最も重要な事実は、
プロジェクト ファイル(.csproj/.vbproj)そのものが
MSBuild の XML スクリプトだということである。Visual Studio ─┐ ├──▶ MSBuild ──▶ csc(Roslyn)──▶ アセンブリ dotnet build ──┤ ↑ CI(GitHub等)─┘ .csproj を解釈したがって「Visual Studio ではビルドできるが CI では落ちる」といった
問題は、同じ MSBuild を使っている以上、環境差(SDK、ワークロード、
NuGet の構成)に原因があると切り分けられる。構成要素は 4 つ。
要素 意味 プロパティ ( <PropertyGroup>)変数( $(Configuration)など)アイテム ( <ItemGroup>)ファイルの集合( @(Compile)など)タスク 1 つの処理( Csc、Copy、Exec)ターゲット ( <Target>)タスクをまとめた単位( Build、Clean)
- タスクを利用すると種々の作業を自動化できる。
- プロジェクトファイルの
<Target>要素にタスクを記載する。
- MSBuild タスク
https://learn.microsoft.com/ja-jp/visualstudio/msbuild/msbuild-tasks - MSBuild タスク リファレンス
https://learn.microsoft.com/ja-jp/visualstudio/msbuild/msbuild-task-reference
- loresoft/msbuildtasks:
The MSBuild Community Tasks Project is an open source project for MSBuild tasks.
https://github.com/loresoft/msbuildtasks
補足(ビルド前後に処理を挟む): 実務で最もよく使うのは、
既定のターゲットの前後にフックする書き方である。<Target Name="MyAfterBuild" AfterTargets="Build"> <Copy SourceFiles="@(Content)" DestinationFolder="$(OutDir)assets" /> <Exec Command="npm run build" WorkingDirectory="ClientApp" /> </Target>
BeforeTargets/AfterTargetsを使うと、
既定のターゲットを上書きせずに処理を追加できる
(古いBeforeBuild/AfterBuildのオーバーライドより安全)。
-
Visual Studio 2015 では MSBuild 14.0 がインストール&利用される。
C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\MSBuild.exeC:\Program Files (x86)\MSBuild\14.0
-
「NuGet auto package restore does not work with MSBuild」が発生した。
- .net - NuGet auto package restore does not work with MSBuild - Stack Overflow
https://stackoverflow.com/questions/22300375/nuget-auto-package-restore-does-not-work-with-msbuild
- .net - NuGet auto package restore does not work with MSBuild - Stack Overflow
-
Visual Studio 2017 では MSBuild 15.0 がインストール&利用される。
C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\MSBuild.exeC:\Program Files (x86)\MSBuild\15.0
-
NuGet のバージョンは、MSBuild 14.0 では 3.5 までとなっており、
MSBuild 15.0 では 4.0 以上が必要。- 以下の文字列を使用して検索すると情報が見つかる。
Failed to load msbuild Toolset Could not load file or assembly 'Microsoft.Build, Version=14.0.0.0
補足(MSBuild の在り処が変わった): MSBuild 15.0(VS2017)以降、
MSBuild は Visual Studio に同梱されるようになり、
C:\Windows\Microsoft.NET\Framework\の下から
Visual Studio のインストール先へ移動した。【〜14.0】C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe 【15.0〜】C:\Program Files\Microsoft Visual Studio\2022\<エディション>\MSBuild\Current\Bin\MSBuild.exeこのため **CI サーバで「MSBuild が見つからない」**という問題が起きた。
現在の対処は次のいずれか。
手段 用途 dotnet build.NET SDK 形式のプロジェクト(最も簡単。VS 不要) vswhere.exeVS のインストール先を検出して MSBuild のパスを得る Build Tools for Visual Studio CI 用のIDE を含まないビルド専用インストール .NET Core 系なら
dotnet buildで完結するため、
この問題自体が発生しない。
Visual Studio との差異
Visual Studio は製品だけあって、
MSBuild では自分で頑張らないといけないことも多い。
補足(この差が CI 構築の勘所): Visual Studio が
暗黙にやってくれていることを、MSBuild 単体では明示する必要がある。
項目 Visual Studio MSBuild 単体 NuGet の復元 自動 msbuild /t:Restoreまたはnuget restoreが要るコンパイラ VS 同梱の Roslyn ツールセットの指定が要る場合がある Web/Azure などのターゲット VS のワークロードが提供 .targetsが無くて失敗(後述の MSB4019)
dotnet buildはこれらを暗黙に処理する(復元も自動)ため、
CI の構築難度が大きく下がった。
dotnetコマンド を参照。
devenv でのビルドと比べ、チラホラと、エラーが起きる。
NuGet 使用時(NuGet)
-
ビルド時に「error MSB4019 ...
MSBuild\...\Microsoft.WebApplication.targetswas not found...」が発生最近、Visual Studio 2017 の MSBuild(v15.0)に標準同梱されていなくて、
Web アプリケーションのビルドに失敗するケースが増えているようです。 -
GitHub から「Download ZIP」して、Explorer で解凍した場合など、この問題が起きます。
-
ビルド時に「error CS1519 Unexpected symbol 'xxxxx' in class, struct, or interface member declaration.」が発生
C# のバージョン(コンパイラ)の問題らしいです。
-
ビルド時に「error CS0012 The type 'xxxxx' is defined in an assembly that is not referenced.」が発生
netstandard ライブラリを参照した場合に発生。
補足("mark of the web" について): インターネットから
ダウンロードしたファイルには Zone.Identifier という代替データ ストリームが
付与され、.NET がそれをブロックする。対処は、
- エクスプローラーのプロパティで**[許可する]にチェック**、
- PowerShell で
Unblock-Fileを再帰実行、Get-ChildItem -Recurse | Unblock-FileZIP を展開する前に ZIP 自体をブロック解除しておくと、
展開後のファイルに付かない。
- MSBuild - Wikipedia
https://ja.wikipedia.org/wiki/MSBuild - MSBuild を活用して開発時の作業を自動化する:CodeZine(コードジン)
http://codezine.jp/article/detail/674
- MSBuild のドキュメント
https://learn.microsoft.com/ja-jp/visualstudio/msbuild/msbuild - MSBuild の概念
https://learn.microsoft.com/ja-jp/visualstudio/msbuild/msbuild-concepts
Tags: 移行, .NET開発, テスト, デバッグ, デプロイ, ツール類, CI
このWikiは「Open棟梁Project」,「OSSコンソーシアム 開発基盤部会」によって運営されています。