MSBuild entry

What MSBuild that?

MSBuild full name (Microsoft Build Engine), is used to generate .NET platform programs. You may not know it, but if you are using VS do development, you must always use it. Because it manages to generate your project file for you in the back. When creating a new project, note the project folder *. * Proj file is to MSBuild offer, which is a text file, an XML-based format, which contains the file items contained, generates configuration, output configuration and other information. When a file or pictures added to the project, we will be in here to add a description, and vice versa delete a description information; configuration made to the project property pages will be stored here.

To understand why MSBuild

I want to know when this confusion stems from the previous school WPF (MSBuild at that time wanted to start from, and we have no energy, until now), because they do not know why they went XAML generated assembly, and finally into the XAML markup What, WPF XAML is how to deal with these tags with C # code? Generally when I write code that will clearly know what has become of this code is finally through the compiler, so that my heart comparative end. But this XAML, see through, to see through the heart block fast enough, it is not practical, I like inquisitive, so I think by the entrance explore what happens.

What I would like to get from the MSBuild

I said above, the first to solve the doubts;
The second project to understand how to organize these files together and generate the final program (just like on the WPF Build is generated out of exe, and it is a WP7 XAP package, why?);
Third understood at the time to read to understand the purpose, do not do in-depth research.

Basic Concepts MSBuild

MSBuild There are four basic blocks (properties, items, mission, goals):

MSBuild Properties:    properties are the key / value pairs, mainly used to store some configuration information.
MSBuild item:    main project file to store some information, files and metadata information (such as version number).
MSBuild task:    the Build process operation of some of the atoms (e.g., CSC, MakeDir)
MSBuild objectives:    a particular order tasks are grouped together, and allows the line to specify each portion separately in order.

Sentence summary of MSBuild: Using the configuration file for the project specific embodiment the operation sequence.

MSBuild property

Property declaratively:

Copy the code
. 1  <? XML Version = "1.0" encoding = "UTF-. 8" ?> 
2  <-! Root element, one item -> 
. 3  <-! The DefaultTargets default target set for execution -> 
4  < the Project the DefaultTargets = "Build" xmlns = "http://schemas.microsoft.com/developer/msbuild/2003" > 
. 5    <-! properties PropertyGroup element to be contained inside -> 
. 6    < PropertyGroup > 
. 7      <! - - declare a "linianhui" attribute, whose value is "Hello World" -> 
. 8      < linianhui > Hello World </ Linianhui > 
9   </ The PropertyGroup > 
10    <-! Target -> 
. 11    < the Target the Name = "Build" > 
12 is      <-! A built-in task MSBuild provided for recording information to generate $ (attribute name) to refer to the value of the property -> 
13 is      < the Message the Text = "$ (linianhui)" > </ the Message > 
14    </ the Target > 
15  </ the Project >
Copy the code

Save this file to the d: \ helloworld.xml file. CMD window open, enter MSBuild helloworld.xml:

MSBuild helloworld.xml

Print out the value "linianhui" attribute. MSBuild provide some retention properties, you can easily quote $, such as $ (MSBuildProjectFile) returns the full name of the project file (helloworld.xml). Other retention properties can be found MSDN help documentation.

MSBuild items

 Item declaratively:

Copy the code
<? Xml Version = "1.0" encoding = "UTF-8" ?> 
< Project the DefaultTargets = "Build" xmlns = "http://schemas.microsoft.com/developer/msbuild/2003" > 
  <-! Items are to be included in the interior elements ItemGroup -> 
  < ItemGroup > 
    <-! declare a "CSFile" item, include redistributes "csfile1.cs" file -> 
    < CSFile the include = "csfile1.cs" > 
      <! - Version represents metadata (additional information) of the -> 
      < Version > 1.0.0.0 </ Version > 
    </ CSFile > 
    <-! May also be ";" a plurality of files is introduced -> 
    <CSFile the Include = "csfile2.cs; csfile3.cs" /> 
  </ ItemGroup > 
  < the Target the Name = "Build" > 
    <! - @ reference value of the item, the default of ";" divided -> 
    <! - output "csfile1.cs; csfile2.cs; csfile3.cs" -> 
    < the Message the Text = "@ (CSFile)" > </ the Message > 
    <-! may add a second parameter to replace the default ";" separator -> 
    <! - output "csfile1.cs + csfile2.cs csfile3.cs +" -> 
    < the Message the Text = "@ (CSFile, '+')" > </ The Message > 
    <-! Metadata% of the referenced item, the output of "1.0.0.0" -> 
    <Message Text="%(CSFile.Version)"></Message>
  </Target>
</Project>
Copy the code

MSBuild task

Msaage above is a task for the print information, some common further comprising CSC, MakeDir, Copy, etc., most of the tasks are the output information, the information can be stored in the attribute or element OutPut item. CS First write the code as follows:

Copy the code
1  // save as D: \ MSBuildDemo.cs 
2  public  class MSBuildDemo
 . 3  {
 . 4      static  void the Main ()
 . 5      {
 . 6          the System.Console.WriteLine ( " the MSBuild compile Organization " );
 7      }
 8 }
Copy the code

Then change the project file as follows:

Copy the code
. 1  <? XML Version = "1.0" encoding = "UTF-. 8" ?> 
2  < the Project the DefaultTargets = "Build" xmlns = "http://schemas.microsoft.com/developer/msbuild/2003" > 
. 3    < ItemGroup > 
4      <! - Specifies the compiled file -> 
. 5      < CSFile the Include = "MSBuildDemo.cs" /> 
. 6    </ ItemGroup > 
. 7    < the Target the Name = "Build" > 
. 8      <! - use csc task corresponding csc compiler ->
9      <-! Sources property represents a collection of files to be compiled-> 
10      <-! The TargetType indicates compiled object type corresponds compiler csc / target parameter -> 
. 11      < the Csc the Sources = "@ (CSFile)" 
12 is           the TargetType = "EXE" > 
13 is        <-! OutputAssembly of csc output parameter -> 
14        <-! PropertyName represents the value stored TaskParameter attribute specified output parameters of this property to outputExeName -> 
15        <-! the output ItemName also a property representing a stored item in -> 
16        < the Output TaskParameter = "OutputAssembly" PropertyName = "outputExeFileName" /> 
. 17      </ the Csc >
18      <! -Message task can use the derived attribute outputExeFileName csc a -> 
19      <! - Output MSBuildDemo.exe -> 
20 is      < Message the Text = "$ (outputExeFileName)" /> 
21 is      <! - Exec task can run strip with a specified program (which can add parameter) or command -> 
22      <-! run just MSBuildDemo.cs compiled program source file -> 
23      <-! operation result is "organized to translate the MSBuild" -> 
24      < Exec the Command = "$ (outputExeFileName)" > </ Exec > 
25    </ the Target > 
26 is  </ the Project >
Copy the code

The implementation of this project document with MSbuild, right on schedule print information.

MSBuild target

An example of the above Target element is the MSBuild target that according to compile the source code, compiled print file name, the file is executed this order organized three tasks. This is the objective things to do. First a brief introduction to it here, some extensions about (attributes, items, mission, goals) information will be introduced next one. If wrong, please correct me!

Note: The above information is all from MSDN help documentation.

Reproduced in: https: //www.cnblogs.com/zhangchenliang/archive/2012/09/01/2666833.html

Guess you like

Origin blog.csdn.net/weixin_34273481/article/details/93495244