how to use msbuild properties in sqlproj (SQL Server 2012) script

佐手、 提交于 2019-12-03 13:24:12

In a sql server 2012 sqlproj (SSDT database project) you use publishing profiles. You can start off by right-clicking your database project and choosing 'Publish'.

You can then set desired options and save these in a so-called publishing profile in your project. Double-clicking this profile launches the publishing wizard with the correct options set.

In your publish profile you can include hard-coded values for sqlcmd variables:

<ItemGroup>
    <SqlCmdVariable Include="ProjectDirectory">
        <Value>UNKNOWN</Value>
    </SqlCmdVariable>
</ItemGroup>

If desired, you can update these with dynamic values during build. In your msbuild project:

<Target Name="SetProjectDirectoryInPublishXml">
    <ItemGroup>
        <Namespaces Include="nsMsbuild">
            <Prefix>nsMsbuild</Prefix>
            <Uri>http://schemas.microsoft.com/developer/msbuild/2003</Uri>
        </Namespaces>
    </ItemGroup>
    <ItemGroup>
        <SSDTPublishFiles Include="$(SolutionBinFolder)\**\*.publish.xml" />
    </ItemGroup>
    <MSBuild.ExtensionPack.Xml.XmlFile Condition="%(SSDTPublishFiles.Identity) != ''"
                                   TaskAction="UpdateElement"
                                   File="%(SSDTPublishFiles.Identity)"
                                   Namespaces="@(Namespaces)" 
                                   XPath="//nsMsbuild:SqlCmdVariable[@Include='ProjectDirectory']/nsMsbuild:Value" 
                                   InnerText="$(MSBuildProjectDirectory)"/>
</Target>

This requires an extension to update the XML. I use the msbuild extension pack.

Credits for this mechanism go to Jamie Thomson

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!