Pages

MsBuild targets and tasks

Tuesday 18 November 2014

Today I came across something cool that I never new was possible. Customizing your build using targets and tasks.

The reason I came across this is because I needed to copy all the dll's for all of my projects to a single references folder so they can be used by various applications from the same location. In addition to having everything in the same location it will insure that all projects are using the latest version of the dll as the dll will be copied to the references folder on every build.

So if you open up your .csproj file with a text editor you will see an xml file structure. This is where the changes will need to be made. In my case I wanted to copy the dll after the build process so the references folder contained the latest version of the dll.

To do this i needed to override the "AfterBuild" target in my .csproj file. You can find the available targets in the following location:

%WinDir%\Windows\Microsoft.NET\Framework\%VersionNum%\Microsoft.Common.targets

Note: you can also create custom targets if you wish.

Now that I know what target i need to override I needed to find the task I was going to use within the target. You can find a list of tasks in the following location:

%WinDir%\Windows\Microsoft.NET\Framework\%VersionNum%\Microsoft.Common.Tasks

Below is a table of commonly used tasks:

TaskDescription
CreateItemCreates items. Since items are evaluated at the beginning of the build, if you need files generated by your build included in an item then you must use this task. If the item that is being created already exists then it will be appended.
CreatePropertyCreates a property. Properties are evaluated at the beginning of the build (except those created by CreateProperty). If the property that is being created exists, it will be overwritten.
CopyCopies files from one location to another.
DeleteDeletes files.
ErrorNotifies MSBuild that an error has occurred. Usually this is used with a Condition attribute that means something went wrong.
ExecInvokes an executable file.
MessagePasses a message to the MSBuild logger.
MSBuildInvokes MSBuild on another project file. You can also use the Exec task to start Msbuild.exe, but the MSBuild task has advantages such as getting the output of executed targets.
Referenced from http://msdn.microsoft.com/en-us/magazine/cc163589.aspx


I needed to copy the references from one folder to another so in my .csproj i added the following in order to complete my task.
     
    
     
   


That's it!

No comments:

Post a Comment