Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
programming4us
Windows 7

Visual Basic 2010 : Advanced Compilations with MSBuild - Introducing MSBuild (part 2) - Understanding Tasks and Creating Targets

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
8/20/2011 3:40:34 PM

Understanding Tasks and Creating Targets

MSBuild has the concept of a task, which is simply a unit of work that can be executed during the build process. You can add both predefined and custom tasks to the build process inside the project file. (Most built-in tasks are self-explanatory, and if you use Visual Studio to create the project file, IntelliSense can be helpful as usual.) Tasks are specified within Target elements. A target is basically a container for tasks, and you can execute tasks by referring the enclosing Target element. For example, the following task sends a message to the specified output log (which is the Console window by default):

<Target Name="SendMessage">
<Message Importance="high"
Text="This is a custom message to demonstrate tasks"/>
</Target>

The important thing is assigning the Name property so that you can later refer to target. To execute a task in the build process, you need to run MSBuild by supplying a /target: switch (or simply /t:) and passing the target name as in the following example:

MsBuild MsBuildTest.vbproj /target:SendMessage

Figure 2 shows how the message appears in the log.

Figure 2. Executing a message task.

You can also execute multiple targets in a single command line by separating their names with a semicolon. With tasks you can execute also external applications that can influence the build process. The following code demonstrates how to accomplish this:

<Target Name="ExecuteMyCustomTool">
<Exec Command="MyExternalFile.exe" ContinueOnError="false"
WorkingDirectory="C:\MyAppsFolder"/>
</Target>

The Exec built-in task enables running the external application to be executed specified with the Command property and that resides in the WorkingDirectory path. You can also set if the build process must break in case the external application returns an error code (ContinueOnError).

Interacting with MsBuild in Code

MSBuild is a managed application taking advantage of Microsoft.Build.Framework.dll assembly and of the Microsoft.Build.Framework namespace. They also expose types for writing code against MSBuild, such as the ILogger and ITask interfaces.


Inside target definitions you can also refer to properties defined inside PropertyGroup nodes. The following code demonstrates how you can create a directory during the build process by assigning to the new directory the name of a folder previously defined within properties:

<Target Name="CreateDataDir">
<MakeDir Directories="$(MyDataDirectory)"/>
</Target>

To add a reference to a property value, you use the $ symbol followed by the name of the property enclosed within parentheses. Now consider the following sample project that invokes the Visual Basic compiler to build multiple code files into one executable:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Compile"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<!–– Defines a series of custom items
pointing to VB code files ––>
<ItemGroup>
<VBFile Include="File1.vb"/>
<VBFile Include="File2.vb"/>
</ItemGroup>

<!–– Defines a property storing the
output exe name––>
<PropertyGroup>
<AssemblyName>MyApp.exe</AssemblyName>
</PropertyGroup>

<!–– Creates a new target––>
<Target Name = "Compile">
<Vbc
Sources = "@(VBFile)"
OutputAssembly = "$(AssemblyName)">
<Output
TaskParameter = "OutputAssembly"
ItemName = "EXEFile" />
</Vbc>
</Target>
</Project>


Vbc is a predefined tag that tells MSBuild to run the Visual Basic command-line compiler against the specified set of files. By mixing the preceding code with concepts previously discussed, it should be clearer how you could run an external tool instead of a .NET compiler, such as a documentation compiler, against a set of desired files and to let MSBuild pass all information to the external tool and build the final file.

Other -----------------
- Microsoft Visio 2010 : Enhancing Diagrams by Adding Hyperlinks & Using Hyperlinks
- Microsoft Visio 2010 : Modifying an Existing Report
- Microsoft PowerPoint 2010 : Avoiding Harmful Attacks & Using the Trust Center
- Microsoft PowerPoint 2010 : Sending a Presentation for Review Using E-Mail & Sending a Presentation by Internet Fax
- Troubleshooting Performance Problems (part 3) - Troubleshooting Disk Performance Problems & Configuring Power Settings
- Troubleshooting Performance Problems (part 2) - Data Collector Sets and Reports
- Troubleshooting Performance Problems (part 1) - Task Manager & Performance Monitor
- Microsoft Excel 2010 : Analyzing Worksheet Data - Creating or Modifying a Table Style & Formatting Table Elements
- Microsoft Excel 2010 : Analyzing Worksheet Data - Creating a Table & Formatting a Table
- Microsoft Word 2010 : Adding a Digital Signature to a Macro Project & Assigning a Macro to a Toolbar
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server