用msbuild构建应用

  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架构应用程序的简单示例:

xml文件定义的属性:
<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>

 

xml文件定义的项:
<ItemGroup>

    <ServerDirFiles Include="$(ServerDir)\**\*.*" Exclude="你要排除的文件定义"/>

    <ClientDirFiles Include="$(ClientDir)\**\*.*" Exclude="你要排除的文件定义"/>

</ItemGroup>

 

xml文件定义的任务列表:
  1. Clean
  2. Init
  3. Build
  4. Relesase  
  <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>
以上几个片段的汇总:
<Project DefaultTargets="CopyFiles" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <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)\**\*.*"/>

        <ClientDirFiles Include="$(ClientDir)\**\*.*"/>

    </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>

</Project>
View Code

在使用这个msbuild xml文件的时候你可以写一个批处理命令:

@echo off

%SystemRoot%\Microsoft.NET\Framework\v2.0.50727\msbuild build.xml /nologo /v:m

pause

 


  

 

你可能感兴趣的:(Build)