msbuild是微軟提供的一個用於生成應用程序的平台,你可以通過一個xml配置文件來控制和處理你的軟件工程。它也集成到了vs裡面,它不依賴於vs。
xml配置(架構)的組成元素:
屬性
項
任務
目標
<PropertyGroup> <AssemblyName>MSBuildSample</AssemblyName> <OutputPath>Bin\</OutputPath> </PropertyGroup>
<ItemGroup> <Compile Include="helloworld.cs" /> </ItemGroup>
<MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')" /> <Csc Sources="@(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe" />
<Target Name="Build"> <MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')" /> <Csc Sources="@(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe" /> </Target>
下面是msdn上面一個最簡單的xml構架文件:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ItemGroup> <Compile Include="helloworld.cs" /> </ItemGroup> <Target Name="Build"> <Csc Sources="@(Compile)"/> </Target> </Project>
該文件通過一個任務編譯helloworld.cs文件。
下面是一個完整的構建一個c/s架構應用程序的簡單示例:
<PropertyGroup> <OutDir>output</OutDir> <OutServerDir>$(OutDir)server</OutServerDir> <OutClientDir>$(OutDir)client</OutClientDir> <SolutionFile>..\xx\你的工程文件.sln</SolutionFile> <ServerDir>xx\xx\工程編譯後bin\server目錄</ServerDir> <ClientDir>xx\xx\工程編譯後bin\client目錄</ClientDir> </PropertyGroup>
<ItemGroup> <ServerDirFiles Include="$(ServerDir)\**\*.*" Exclude="你要排除的文件定義"/> <ClientDirFiles Include="$(ClientDir)\**\*.*" Exclude="你要排除的文件定義"/> </ItemGroup>
<Target Name="Clean"> <RemoveDir Directories="$(OutDir)"/> </Target> <Target Name="Init" DependsOnTargets="Clean"> <MakeDir Directories="$(OutDir)"/> <MakeDir Directories="$(OutServerDir)"/> <MakeDir Directories="$(OutClientDir)"/> </Target> <Target Name="Build" DependsOnTargets="Init"> <MSBuild Projects="$(SolutionFile)" Targets="Rebuild" Properties="Configuration=Release"/> </Target> <Target Name="CopyFiles" DependsOnTargets="Build"> <Copy SourceFiles="@(ServerDirFiles)" DestinationFiles="@(ServerDirFiles->'$(OutServerDir)\%(RecursiveDir)%(Filename)%(Extension)')"/> <Copy SourceFiles="@(ClientDirFiles)" DestinationFiles="@(ClientDirFiles->'$(OutClientDir)\%(RecursiveDir)%(Filename)%(Extension)')"/> </Target>
在使用這個msbuild xml文件的時候你可以寫一個批處理命令:
@echo off %SystemRoot%\Microsoft.NET\Framework\v2.0.50727\msbuild build.xml /nologo /v:m pause