你的位置:首页 > ASP.net教程

[ASP.net教程]用msbuild构建应用


  msbuild是微软提供的一个用于生成应用程序的平台,你可以通过一个

  •   项目文件

      属性

      项

      任务

      目标

 

属性: 
<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上面一个最简单的

<Project ="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>

 

  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" ="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

@echo off%SystemRoot%\Microsoft.NET\Framework\v2.0.50727\msbuild build.:mpause