title: MSBuild - how to force "AfterBuild" target when I do deployment?
body: <p>I have the following setup: ASP.Net MVC .Net 4.0 solution with 5 projects in it, and several solution configurations (<em>Site1-Stage, Site1-Live, Site2-Stage</em>, etc). The reason for this is simple - we deploy same codebase to multiple servers with different config settings.</p>
  
  <p>To manage these configurations, I use the approach described by Troy Hunt in his <a href="http://www.troyhunt.com/2010/11/you-deploying-it-wrong-teamcity.html" rel="noreferrer">You're deploying it wrong! TeamCity, Subversion &amp; Web Deploy part 1: Config transforms</a> article. In 2 words - I do NOT have <em>web.config</em> in my SVN repo, instead I have <em>Web.Base.Config</em>, <em>Web.Site1-Stage.Config</em>, etc and <em>XmlTransformation</em> task in project <em>AfterBuild</em> target. During the build, the required <em>web.config</em> is generated based on selected configuration:</p>
  
  <pre><code> &lt;UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll" /&gt;
    &lt;Target Name="AfterBuild"&gt;
      &lt;TransformXml Source="Web.Base.config" Transform="Web.$(Configuration).config" Destination="Web.config" StackTrace="true" /&gt;
    &lt;/Target&gt;
  </code></pre>
  
  <p><strong>Here comes the problem:</strong> when I'm execute MSBuild like this:</p>
  
  <pre><code>msbuild MySolution.sln /P:configuration=Site1-Stage /t:rebuild
  </code></pre>
  
  <p>all goes well, web.config is properly generated for the <em>Site1-Stage</em> configuration. However, if I run this command:</p>
  
  <pre><code>msbuild MySolution.sln /P:configuration=Site1-Stage /t:rebuild /P:DeployOnBuild=True
  </code></pre>
  
  <p>I get the following error:</p>
  
  <blockquote>
    <p>"MySolution.sln" (rebuild target) (1) -> "MySolution\MyWebProj.csproj"
    (Rebuild target) (3) -> (PreTransformWebConfig target) ->   C:\Program
    Files
    (x86)\MSBuild\Microsoft\VisualStudio\v10.5\Web\Microsoft.Web.Publishing.targets(1399,5):
    error : Copying file Web.config to
    obj\Site1-Stage\TransformWebConfig\original\Web.config failed. Could
    not find file 'Web.config'. [MySolution\MyWebProj.csproj]</p>
  </blockquote>
  
  <p>I tried to explicitly add "AfterBuild" target into MSBuild command line:</p>
  
  <pre><code>msbuild MySolution.sln /P:configuration=Site1-Stage /t:rebuild,AfterBuild /P:DeployOnBuild=True
  </code></pre>
  
  <p>but it resulted in the same error.</p>
  
  <p><strong>Why do I need this:</strong> it's a very isolated example, and in reality I'm trying to setup automated publishing from TeamCity CI server. I think if I add new build step with "Visual Studio (sln)" runner BEFORE my current publishing step, that would work, it will first rebuild the project (and generate web.config) - and then publish. However, i have lots of publishing steps (around 20 now) and I would like to avoid that. My understanding is that "Publish" process does the build as part of it, so I would like to "reuse" that.</p>
  
  <p><strong>Question is:</strong> how should I modify my MSBuild command line to force config transformation to happen?</p>
  
  <p>Thank you.</p>