MSBuild & SQL Server Database Project: Bind Deployment and Publish Assembly Configuration Customization

Visual Studio 2012. SQL Server Database Project.

Four build configurations were created in the solution: Debug, DevDb, TestDb, LocalDb.

Three publishing projects were created in the project: DevDb.publish.xml, TestDb.publish.xml, LocalDb.publish.xml

Pressing the F5 button (!) I want:

  • Deploy the project with the connection string from the project properties if the assembly configuration is Debug .
  • publish the project with a connection string from the corresponding publication profiles if the assembly configuration is DevDb , TestDb, or LocalDb .

To do this, I edit the project file (.sqlproj) xml, trying to catch the call to the Deploy target and replacing the standard deployment version with the target behavior:

<Target Name="Deploy">     

    <!-- The first statment is for Debug configuration  -->

    <MSBuild Condition=" '$(Configuration)' == 'Debug' "
      Targets="Deploy"
      Projects="$(MSBuildProjectFile)"
      Properties="Configuration=$(Configuration);" 
    />

    <!-- The second statement is for DevDb, TestDb, LocalDb configurations  -->

    <MSBuild Condition=" '$(Configuration)' != 'Debug' "
      Targets="SqlPublish"
      Projects="$(MSBuildProjectFile)"
      Properties="SqlPublishProfilePath=$(Configuration).publish.xml;
                  Configuration=$(Configuration);"
    />       
 </Target> 

The second statement works fine, and I get the deployment in the right place.

The problem is the first statement - it causes a cyclical dependence.

error MSB4006: there is a circular dependency in the dependency dependency graph that includes the target "deployment".

My question is: how to cross (catch and replace) the standard target, and if you want to call the standard target again?

Or am I trying to invent a wheel, and is there another way to do what I want? (What I want is described above under the words "Pushing F5 button" :)

+2
1

, .

, ​​PropertyGroup xml :

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">

    ----------- Skip -----------

  <PropertyGroup Condition=" '$(Configuration)' != 'Debug' and Exists('$(Configuration).publish.xml')">
    <DeployDependsOn>
      PublishDatabase
    </DeployDependsOn>
  </PropertyGroup>

  <Target Name="PublishDatabase"> <!-- Custom Target -->
    <Message 
        Text="Deploy is replaced with SqlPublish for configuration = $(Configuration)"
        Importance="high" />

    <MSBuild 
        Targets="SqlPublish" 
        Projects="$(MSBuildProjectFile)"
        Properties="SqlPublishProfilePath=$(Configuration).publish.xml;
                    Configuration=$(Configuration);" />
  </Target>
</Project>

:

  • Debug *.publish.xml, SqlPublish, , ​​ string .
  • Debug, Visual Studio, .
+3

Source: https://habr.com/ru/post/1546882/


All Articles