Search code examples
msbuildweb-config

Run MSBuild on a csproj doesn't apply web.config transofmration


I'm using Team City to handle our deployment.

So I created a MSBuild step to compile our .sln file.

After the build I have these three files in my output directory :

  • Web.config
  • Web.Release.config
  • Web.Debug.config

That means that the msbuild task doesn't transform the web.config.

But when I use the publish functionality in visual studio , the transformation is done.

So, what's the difference between a msbuild and a publish ? And how can I force my msbuild task to transform the config ?


Solution

  • I found my answer here

    https://github.com/geersch/TeamCity/blob/master/src/part-3/README.md

    Here is my task :

    <Target Name="Publish">    
       <RemoveDir Directories="$(DestinationPath)" ContinueOnError="true" />         
       <MSBuild Projects="$(SourcePath)/$(ProjectFile)" Targets="Rebuild;ResolveReferences;_CopyWebApplication" Properties="WebProjectOutputDir=$(DestinationPath);OutDir=$(DestinationPath)\bin\" />  
    
       <TransformXml Source="$(SourcePath)/Web.Config" Transform="$(SourcePath)/Web.$(Configuration).config" Destination="$(DestinationPath)/Web.config" />
       <ItemGroup>
          <FilesToDelete Include="$(DestinationPath)/Web.*.config"/>
       </ItemGroup>
       <Delete Files="@(FilesToDelete)"/>
    </Target>
    

    You call it like this

     <MSBuild Projects="$(MSBuildProjectFullPath)" 
             Targets="Publish" 
             Properties="Configuration=Release;ProjectFile=WebProject.csproj;SourcePath=C:\webproject\;DestinationPath=C:\inetpub\wwwroot\app\"/>
    

    Thanks