微软建站SQL Server数据库项目:将部署和发布目标绑定到生成配置 [英] MSBuild & SQL Server Database Project: bind the deploy and publish desination to the build configuration

查看:106
本文介绍了微软建站SQL Server数据库项目:将部署和发布目标绑定到生成配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Visual Studio2012.SQLServer数据库项目.

Visual Studio 2012. SQL Server Database Project.

在解决方案中创建了四个构建配置:Debug,DevDb,TestDb,LocalDb.

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

在项目中创建了三个发布配置文件:DevDb.publish.xml,TestDb.publish.xml,LocalDb.publish.xml

Three publish profiles were created in project: DevDb.publish.xml, TestDb.publish.xml, LocalDb.publish.xml

按下F5按钮(!),我想这样做:

  • 部署项目,其中包含来自项目属性如果的连接字符串 构建配置为 Debug .
  • 发布项目,带有连接字符串 从相应的发布配置文件中如果构建配置为 DevDb TestDb LocalDb .
  • deploy project with connection string from project properties if build configuration is Debug.
  • publish project with connection string from the corresponding publish profiles if build configuration is DevDb, TestDb or LocalDb.

为此,我编辑项目(.sqlproj)xml,尝试捕获对Deploy目标的调用,并用自定义行为替换标准的Deploy目标:

To do this I edit project (.sqlproj) xml, trying to catch a call of the Deploy target and replace the standart Deploy target with a custom 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 deployment to the right destination.

问题出在第一条语句上-它产生了循环依赖.

The problem is with the first statement - it produces a circular dependency.

错误MSB4006:目标依赖关系图中涉及目标部署"的循环依赖.

我的问题是:如何相交(捕获并替换)标准目标,以及是否需要再次调用标准目标?

My question is: how to intersect (catch and replace) the standart target and if it's required to invoke the standart target again?

还是我要重新发明轮子,还有另一种方法可以做我想做的事情? (我在上面的按F5按钮"中描述了我想要的内容:)

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

推荐答案

我认为我有解决方案.

因此,如果将带有Condition和自定义Target的PropertyGroup放在数据库项目xml文件的末尾:

So if we put such a PropertyGroup with Condition and a custom Target to the end of database project xml file:

<?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目标,并且此发布 操作将获得所需的配置,例如连接 来自相应的发布配置文件的字符串.
  • 如果解决方案构建配置为调试",则当标准Visual Studio从项目属性中获取连接字符串时,我们将保留该行为.
  • If the solution build configuration is differ from Debug and the publish profile *.publish.xml exists, then the Deploy target and operation will be replaced with SqlPublish target and this publish operation will get the required configuration such as connection string from the corresponding publish profile.
  • If the solution build configuration is Debug, then we preserve the standart Visual Studio behaviour when it take th connection string from the project properties.

这篇关于微软建站SQL Server数据库项目:将部署和发布目标绑定到生成配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆