Useful MSBuild Custom Tasks

3. April 2008 08:48 by Mrojas in General  //  Tags: , , , ,   //   Comments (0)

I present here the implementation of some useful tasks
In Artinsoft we perform massive migrations of VB6 code to VB.Net
and C#.

And sometimes after migration there are customizations to be
performed on the code, to add new functionality or to set certain new
properties.

 The idea was to provide a couple of very simple and puntual MSBuildTask
 to illustrate how easy it is to create custom tasks and to provide a starting
 point to create new one.

 You can freely use this code, just keep this comments and remember this is just
 a sample code. There are not warranties. ;) And i made it a rush I know it could have
 been written better

 Artinsoft
 mrojas@artinsoft.com
 

The implemented tasks are:

RemoveCOMReference 
 Removes COMReferences from your project. COM references are for when you are using things thru Interop
FixOutputPath 
 Resets the output paths to bin\Release and bin\Debug
AddProjectReference Add a reference to another project. A nice feature is that it generates RelativePaths the way Visual Studio does
AddSimpleReference Add a reference to a very simple references like the ones you add when you click Add Reference and add System.EnterpriseServices
ChangeCurrentBuildSetting This can be used for a lot of things.

For example to turn on or off the RegisterForComInterop setting

To set conditional compilation variables

To set debug info to pdbonly

The sky is the limit jeje

The following is a sample project file

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- make sure that the Assembly is in a place where msbuild can find it, a simple way is just to put it 
in the same directory of your .proj file -->
<UsingTask TaskName="SomeUsefulTasks.MSBuild.RemoveCOMReference"    
  AssemblyFile="SomeUsefulTasks.dll"/> 
<UsingTask TaskName="SomeUsefulTasks.MSBuild.FixOutputPath"         
  AssemblyFile="SomeUsefulTasks.dll"/> 
<UsingTask TaskName="SomeUsefulTasks.MSBuild.AddProjectReference"   
  AssemblyFile="SomeUsefulTasks.dll"/> 
<UsingTask TaskName="SomeUsefulTasks.MSBuild.AddSimpleReference"    
  AssemblyFile="SomeUsefulTasks.dll"/> 
<UsingTask TaskName="SomeUsefulTasks.MSBuild.ChangeProjectBuildSetting"    
  AssemblyFile="SomeUsefulTasks.dll"/> 
 
   <ItemGroup>
    <VSProjects Include="$(Start)\**\*.*proj" />
  </ItemGroup>
 
<!--
Run with 
MSBUILD SampleProject.proj /target:COMReference /p:Start="C:\MyCode"
-->
  <Target Name="COMReference">
    <RemoveCOMReference SourceFiles="@(VSProjects)" ComReferenceName="MSXML2" />
  </Target> 
  
  
<!-- 
Adds a project reference  
Run with 
MSBUILD SampleProject.proj /target:AddProjectReference /p:Start="C:\MyCode" /p:ProjectPath="C:\MyCode\MyNewSuperProject\Project1.csproj"
-->
   <Target Name="AddProjectReference">
      <AddProjectReference SourceFiles="@(VSProjects)"  AbsolutePathToProject="$(ProjectPath)"/>
   </Target> 
   
   
<!-- 
Adds a reference to a standard assembly 
Run with 
MSBUILD SampleProject.proj /target:AddSimpleReference /p:Start="C:\MyCode" /p:Reference="System.EnterpriseServices"   
-->
<Target Name="AddSimpleReference">
      <AddSimpleReference SourceFiles="@(VSProjects)" Reference="$(Reference)" />
</Target> 
 
  
<!-- 
Resets the OutputPaths to .\bin\Debug and .\bin\Release 
Run with 
MSBUILD SampleProject.proj /target:FixOutput /p:Start="C:\MyCode" /p:Reference="System.EnterpriseServices"   
-->
<Target Name="FixOutput">
    <FixOutputPath SourceFiles="@(VSProjects)"  />
</Target> 
  
<!-- 
Adds a reference to a standard assembly 
There are several options here for example to set the project debug info to pdb-only do this:
Run with 
MSBUILD SampleProject.proj /target:ChangeSettingToPDBOnly /p:Start="C:\MyCode" 
Or run with 
MSBUILD SampleProject.proj /target:ChangeSettingAddAConstant /p:Start="C:\MyCode" 
Or run with 
MSBUILD SampleProject.proj /target:SettingComInterop /p:Start="C:\MyCode" 
-->
 
<Target Name="ChangeSettingToPDBOnly">
      <ChangeProjectBuildSetting 
          SourceFiles="@(VSProjects)" 
          ConfigurationType="All" 
          Setting="DebugType" 
          NewValue="pdbonly" />
</Target> 
   
<Target Name="ChangeSettingAddAConstant">
      <ChangeProjectBuildSetting 
          SourceFiles="@(VSProjects)" 
          ConfigurationType="All" 
          Setting="DefineConstants" 
          NewValue="MYNEWVAL" 
          Add="True"/>
</Target> 
 
 
<Target Name="SettingComInterop">
      <ChangeProjectBuildSetting 
          SourceFiles="@(VSProjects)" 
          ConfigurationType="All" 
          Setting="RegisterForComInterop" 
         NewValue="true" />
</Target> 
 
  
</Project>

DOWNLOAD CODE AND BINARIES