A place for spare thoughts


Deployment as part of development build

Filed under: MSBuild, VisualStudio — Ivan Danilov @ 19:25

Last two years or so I’ve been mostly developing solutions that are plugins to some other systems. Sometimes it is third-party library, sometimes it is some kind of corporate-standard-framework with some common presentation functionality whatever.

The shared feature of every such project is that is should be installed in some way into that external system. Most often installing is just copying DLLs into some predefined folder for plugins. Sometimes it is also required to register something in registry, settings of the system or somewhere else. And that is the problem. When you’re working from Visual Studio – the most natural process is to make changes and hit F5 to run and see your changes live (I don’t take testing into consideration here but it is an important part of course). Well, VS can run external application under debugger. But who will make installation steps?

I’ve seen several approaches there. Some teams just point OutputPath to external system’s plugins folder so that building takes place there. It is not a good idea as csproj files are under source version control system and paths could be different on different machines. Most obvious example is that when external system is 32bit – it has different paths on 32bit and 64bit windows. Other example is build server that could even not having this external system installed. Its task usually is to build (and probably run unit tests, code coverage, make installation kit etc), not to run actual system. Moreover if external system is running and have your assemblies from previous build locked (as they’re loaded into it) – VS can’t clean them or replace with new ones which sometimes lead to confusion.

Yet another teams perform build with default settings and then manually (or with help of some batch file) copying needed artifacts to the place where they should be. It is slightly better in terms of build server integration and locked files. But it requires three steps to run application: build solution from VS, switch to explorer (or whatever you’re using) and run batch file, switch back to VS and run external system with debugger attached. It is not only a pain, it is also error-prone as one could easily forget to do one of these steps.

And yet another teams have post-build step in some project. And that post-build step executes actual install. Here we have problem with project build skipping. VS has some heuristics to determine which projects should be built and which could be skipped. First of all it skips everything that is up-to-date (i.e. hasn’t any file or referenced project changed after last successful build). Also VS skips everything that wasn’t set up for build in current configuration (in the solution properties). Thus said if project build is skipped – post-build step is skipped also. Oops. VS doesn’t know that you have some dependencies in the post-build step. And worse it would be skipped without any warning so that you can find it only when something goes wrong. Not good definitely.

The solution to this problem that I came with in the end is to have separate project in the solution dedicated to this installation step. And this solution should have references to any other project in the solution. To be clear here, this Deployment project is not going to call compiler or produce build errors if referenced projects were not built (due to current configuration for example). The single purpose of these references is to give VS knowledge about when it could really skip build of Deployment project – only when no projects in the solution changed. Otherwise it should build Deployment.

The downside of this approach is evident: you should carefully watch over rule that Deployment has these references actual. But this task could be automated more or less easily. Maybe I will write a blog post soon about such thing. We have this checking as a part of build process so if anyone makes a change violating the rules we have failed build. It just works.

UPDATE: I’ve written such post. See here.

And here is example of Deployment.csproj file:

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
    <!-- VS2010 for some reason requires OutputType property set. Otherwise it throws
         an error when you try to open project properties window -->
    <ProjectReference Include="..\YourProject\YourProject.csproj">
    <!-- Other project references goes there -->
  <Target Name="Build">
      <ToCopy Include="..\YourProject\bin\Debug\*.dll" />
      <ToCopy Include="..\YourProject\bin\Debug\*.pdb" />
      <ToCopy Include="..\YourProject\bin\Debug\*.config" />
      <ToCopy Include="..\AnotherDirToCopyFrom\*.*" />
      <!-- Actually you could have any set of things to copy here -->
    <Message Text="ToCopy = $(ToCopy)" />
    <Message Text="DestinationFolder = $(InstallDir)\Plugins\YourPluginName" />
    <Copy Condition=" '$(InstallDir)' != '') " 
          DestinationFolder="$(InstallDir)\Plugins\YourPluginName" />
    <!-- This target copy things only if %InstallDir% environment variable exists.
         It prevents copying from executing on build server if it doesn't have the system
         You could have every conditional logic you want instead. -->
  <Target Name="Clean" />
  <!-- If you need to do something on the Clean - put it here. But don't delete
       this target at all because VS will give you an error that project does not have
       Clean target on each Clean or Rebuild -->
  <Target Name="Rebuild" DependsOnTargets="Build" />

The main point here is that we don’t import $(MSBuildToolsPath)\Microsoft.CSharp.targets and define our own Build, Clean and Rebuild targets which are called by VS. Probably you will have to define also Publish target if you use it.

Blog at WordPress.com.