为什么Msbuild任务无法部署数据库,但Exec工作正常

时间:2012-03-31 19:05:52

标签: deployment msbuild database-project

我正在尝试在自动构建服务器处理中部署数据库项目(dbproj格式,而不是新的SSDT sqlproj)。我找到了以下内容:

当我在我的Msbuild脚本中使用Exec任务调用deploy时 - 一切正常:

<Exec Command="$(MSBuildPath)\MSBuild.exe $(SourceFilesPath)\$(DeployDatabaseProjectName)\$(DeployDatabaseProjectName).dbproj 
/t:Deploy 
/p:OutputPath=$(BaseOutput)\$(DeployDatabaseProjectName)\ 
/p:TargetDatabase=$(DeployDatabaseName)
/p:TargetConnectionString=$(DeployDatabaseConnectionString)" />

但是当我尝试使用Msbuild任务重复此操作时 - 它的行为有所不同:

<MSBuild Projects="$(SourceFilesPath)\$(DeployDatabaseProjectName)\$(DeployDatabaseProjectName).dbproj" 
            Targets="Deploy"
            Properties="Configuration=$(BuildConfiguration);
            TargetDatabase=$(DeployDatabaseName);
            TargetConnectionString=&quot;$(DeployDatabaseConnectionString)&quot;;
            OutputPath=$(BaseOutput)\$(DeployDatabaseProjectName)\;
            " />

Msbuild任务在DeployDatabaseConnectionString:

中以分号破坏
<DeployDatabaseConnectionString>Data Source=$(DeployDatabaseServer);Integrated Security=True;Pooling=False</DeployDatabaseConnectionString>

它会报告这样的事情:

名称“Integrated Security”包含无效字符“”

但如果我用百分比编码值 - %3B替换分号 - 它将在SqlDeployTask中破坏:

错误MSB4018:“SqlDeployTask”任务意外失败。

将TargetConnectionString传递给部署SqlProject目标的正确方法是什么?

PS:我可以和exec任务一起使用,但是在msbuild脚本中调用msbuild.exe会伤害我内在的完美主义者。

1 个答案:

答案 0 :(得分:4)

我找到了正确的方法 - 新的Msbuild允许在item上定义AdditionalProperties元数据。因此,使用此功能一切正常,并且没有转义\编码

的问题
<ItemGroup>
    <DbProjectToBuild Include="$(SourceFilesPath)\$(DeployDatabaseProjectName)\$(DeployDatabaseProjectName).dbproj">
        <AdditionalProperties>Configuration=$(BuildConfiguration)</AdditionalProperties>
        <AdditionalProperties>OutputPath=$(BaseOutput)\$(DeployDatabaseProjectName)\</AdditionalProperties>
        <AdditionalProperties>TargetDatabase=$(DeployDatabaseName)</AdditionalProperties>
        <AdditionalProperties>TargetConnectionString="Data Source=$(DeployDatabaseServer);Integrated Security=True;Pooling=False"</AdditionalProperties>
    </DbProjectToBuild>
</ItemGroup>        
<MSBuild Projects="%(DbProjectToBuild.Identity)" Targets="Build;Deploy" />