views:

191

answers:

1

After days of messing with it, i got my web deployment project to do what i want. It compiles the site, makes a copy, modified the web.config in the copy to point to a different database, zips up the copy and deletes the copy folder leaving only the zip file behind. It then does the same thing for production. But, i feel that my solution is kind of backwards, mainly because i have the connection strings hardcoded. Can you please look at my AfterBuild section and let me know if it's good, or should be changed?

<Target Name="AfterBuild">
  <Exec Command="del &quot;$(MSBuildProjectDirectory)\Release\*.csproj&quot;, &quot;$(MSBuildProjectDirectory)\Release\*.user&quot;, &quot;$(MSBuildProjectDirectory)\Release\*.vspscc&quot;" />
  <Exec Command="xcopy /y /e  &quot;$(MSBuildProjectDirectory)\Release&quot; &quot;$(MSBuildProjectDirectory)\Production\&quot;" />
  <XmlUpdate Namespace="http://schemas.microsoft.com/.NetConfiguration/v2.0" 
             XmlFileName="$(MSBuildProjectDirectory)\Production\web.config"
             Xpath="//configuration/connectionStrings/add[@name='MySqlProviderConnection']/@connectionString" 
             Value="###" />
  <CreateItem Include="$(MSBuildProjectDirectory)\Production\**" Exclude="">
    <Output ItemName="ProductionZipFiles" TaskParameter="Include" />
  </CreateItem>
  <Zip ZipFileName="production.zip" WorkingDirectory="$(MSBuildProjectDirectory)\Production" Files="@(ProductionZipFiles)" />
  <Exec Command="rmdir /s /q &quot;$(MSBuildProjectDirectory)\Production\&quot;" />
  <Exec Command="xcopy /y /e  &quot;$(MSBuildProjectDirectory)\Release&quot; &quot;$(MSBuildProjectDirectory)\Staging\&quot;" />
  <XmlUpdate Namespace="http://schemas.microsoft.com/.NetConfiguration/v2.0"
             XmlFileName="$(MSBuildProjectDirectory)\Staging\web.config"
             Xpath="//configuration/connectionStrings/add[@name='MySqlProviderConnection']/@connectionString"
             Value="###" />
  <CreateItem Include="$(MSBuildProjectDirectory)\Staging\**">
    <Output ItemName="StagingZipFiles" TaskParameter="Include" />
  </CreateItem>
  <Zip ZipFileName="staging.zip" WorkingDirectory="$(MSBuildProjectDirectory)\Staging" Files="@(StagingZipFiles)" />
  <Exec Command="rmdir /s /q &quot;$(MSBuildProjectDirectory)\Staging\&quot;" />
</Target>
+1  A: 

I would suggest remvoing the usage of the Exec task to delete files and to copy them. MSBuild has constructs specifically for this. The only caveat to remember is that if you want to perform any action on any files created during your build then you must declare them inside of a target.

Besides that you can put your connection string inside a property like:

<PropertyGroup>
    <StagingConString Condition=" '$(StagingConString)'=='' >VALUE-HERE</StagingConString>
    <ProdConString Condition=" '$(ProdConString )'=='' >VALUE-HERE</ProdConString >
</PropertyGroup>

Then you can just use the property inside the XmlUpdate using the $(ProdConString) syntax. Take note of the conditions, this allows those properties to be easily overridden by people who might extend the build here. Take a look at http://msdn.microsoft.com/en-us/magazine/dd419659.aspx for more details on that and other things.

About your usage of the CreateItem task, this is good for MSBuild 2.0 but if you are using MSBuild 3.5 (i.e. VS 2008+) projects then you can just use the sytnax inside of targets themselves.

Sayed Ibrahim Hashimi