Use msbuild to build an application and msbuild to build an application

Source: Internet
Author: User

Use msbuild to build an application and msbuild to build an application

Msbuild is a platform provided by Microsoft for generating applications. You can control and process your software projects through an xml configuration file. It is also integrated into vs, which does not depend on.

Elements of xml configuration (Architecture:

  • Project File

Attribute

Item

Task

Target

 

Attribute:
<PropertyGroup>    <AssemblyName>MSBuildSample</AssemblyName>    <OutputPath>Bin\</OutputPath>  </PropertyGroup>
Item:
<ItemGroup>    <Compile Include="helloworld.cs" />  </ItemGroup>
Task:
    <MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')" />    <Csc Sources="@(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe" />
Objective: To combine tasks in sequence
<Target Name="Build">    <MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')" />    <Csc Sources="@(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe" />  </Target>

 

The following is the simplest xml architecture file on msdn:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">  <ItemGroup>    <Compile Include="helloworld.cs" />  </ItemGroup>  <Target Name="Build">    <Csc Sources="@(Compile)"/>    </Target></Project>

Compile the helloworld. cs file using a task.

 

The following is a simple example of a complete c/s architecture application:

Attributes defined by the xml file:
<PropertyGroup> <OutDir> output </OutDir> <OutServerDir >$ (OutDir) server </OutServerDir> <OutClientDir >$ (OutDir) client </OutClientDir> <SolutionFile> .. \ xx \ your project file. sln </SolutionFile> <ServerDir> xx \ bin \ server Directory after project compilation </ServerDir> <ClientDir> xx \ bin \ client Directory after project compilation </ClientDir> </PropertyGroup>

 

Items defined in the xml file:
<ItemGroup> <ServerDirFiles Include = "$ (ServerDir )\**\*. * "Exclude =" definition of the file to be excluded "/> <ClientDirFiles Include =" $ (ClientDir )\**\*. * "Exclude =" definition of the file to be excluded "/> </ItemGroup>

 

Task list defined by the xml file:
  <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>
Summary of the above fragments: <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 \ your project file. sln </SolutionFile> <ServerDir> xx \ bin \ server Directory after project compilation </ServerDir> <ClientDir> xx \ bin \ client Directory after project compilation </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

When using this msbuild xml file, you can write a batch processing command:

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

 


  

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.