using msbuild to create a sql database - msbuild

Is there a good tutorial for Creating a database using msbuild?
jean paul boodhoo does it using nant in this post.
he sets properties to be used in an nant build file
<properties>
<property name="sqlToolsFolder" value="C:\Program Files\Microsoft SQL Server\90\Tools\Binn"/>
<property name="osql.ConnectionString" value="-E"/>
<property name="initial.catalog" value="Northwind"/>
<property name="config.ConnectionString" value="data source=(local);Integrated Security=SSPI;Initial Catalog=${initial.catalog}"/>
<property name="database.path" value="C:\root\development\databases" />
<property name="osql.exe" value="${sqlToolsFolder}\osql.exe" />
</properties>
then can create the database using the command line like this..
c:\> build builddb
I installed the MSBuild Extension pack but I could not find where to enter the connection string to connect to the database
Thanks
RESOLVED
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="constants.proj"/>
<Target Name="QueryDb">
<PropertyGroup>
<_Command>-Q "SELECT * FROM Users"</_Command>
<_Command2>-i test.sql</_Command2>
</PropertyGroup>
<Exec Command="$(sqlcmd) $(_Command)" /><!---->
</Target>
</Project>
and Constants.proj looks like this
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<sqlToolsFolder>C:\Program Files\Microsoft SQL Server\90\Tools\Binn</sqlToolsFolder>
<initialCatalog>NorthwindTest</initialCatalog>
<serverInstance>(local)\SQLEXPRESS</serverInstance>
<configConnectionString>data source=$(serverInstance);Integrated Security=SSPI;Initial Catalog=$(initialCatalog)</configConnectionString>
<osqlExe>"$(sqlToolsFolder)\osql.exe"</osqlExe>
<sqlcmd>$(osqlExe) -U someuser -P somepassword -d $(initialCatalog) -S (local)\SQLEXPRESS</sqlcmd>
<!--<sqlcmd>$(osqlExe) -E -d $(initialCatalog) -S (local)\SQLEXPRESS</sqlcmd>-->
</PropertyGroup>
</Project>
then at the vs command prompt run
msbuild db.targets /t:QueryDb
the command that runs is this
"C:\Program Files\Microsoft SQL Server\90\Tools\Binn\osql.exe" -U someuser -P somepassword -d NorthwindTest -S (local)\SQLEXPRESS -Q "SELECT * FROM UserProfile"
Thank you
Sayed

If you are comfortable with the approach followed in that post then you can simply follow that from MSBuild as well. For example create the file constants.proj (you can name it whatever you like) and db.targets (also name it whatever you want). And then those would contain something like:
constants.proj
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<sqlToolsFolder>C:\Program Files\Microsoft SQL Server\90\Tools\Binn</sqlToolsFolder>
<osqlConnectionString>-E</osqlConnectionString>
<initialCatalog>Northwind</initialCatalog>
<configConnectionString>data source=(local);Integrated Security=SSPI;Initial Catalog=$(initialCatalog)</configConnectionString>
<databasePath>C:\root\development\databases</databasePath>
<osqlExe>$(sqlToolsFolder)\osql.exe</osqlExe>
</PropertyGroup>
</Project>
And then in db.targets you would just build the command line with those properties and use the Exec task to execute it, like the following.
db.targets
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="constants.targets"/>
<Target Name="CreateDb">
<PropertyGroup>
<_Command> ... FILL IN HERE ... </_Command>
</PropertyGroup>
<Exec Command="$(_Command)" />
</Target>
</Project>

The MSBuild Extension pack contains tasks (namely MSBuild.ExtensionPack.Sql2005 and MSBuild.ExtensionPack.Sql2008) to manipulate SQL databases and the following example:
<!-- Create a database -->
<MSBuild.ExtensionPack.Sql2005.Database TaskAction="Create" DatabaseItem="ADatabase2" MachineName="MyServer\SQL2005Instance"/>
<!-- Create the database again, using Force to delete the existing database -->
<MSBuild.ExtensionPack.Sql2005.Database TaskAction="Create" DatabaseItem="ADatabase2" Force="true" Collation="Latin1_General_CI_AI" MachineName="MyServer\SQL2005Instance"/>

Related

How to specify target dir during Copy Task in MSBuild

I have existing MSBuild code that I got help with but I need further help.
<!--?xml version="1.0" encoding="utf-8"?-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Demo">
<ItemGroup>
<Source1 Include="C:\temp\path01\**\*.txt" /> <!-- \%2A.txt -->
<Target1 Include="C:\temp\path02" />
</ItemGroup>
<Target Name="Demo" BeforeTargets="Build;Rebuild">
<!--<Exec Command="xcopy.exe /C /S /F /R /Y /I /D #(Source1) #(Target1)" />-->
<Copy SourceFiles="#(Source1)" DestinationFiles="#(Source1->'#(Target1)\path02\%(RecursiveDir)%(Filename)%(Extension)')"
SkipUnchangedFiles="true" />
<Message Text="== END ===============================" />
</Target>
</Project>
But seems like #(Target1) is not working. It actually creates the directory called #(Target1) instead of using Item value. See the result below.
Creating directory "#(Target1)\path02".
Copying file from "C:\temp\path01\0001.txt" to "#(Target1)\path02\0001.txt".
Copying file from "C:\temp\path01\0002.txt" to "#(Target1)\path02\0002.txt".
Creating directory "#(Target1)\path02\sub".
Copying file from "C:\temp\path01\sub\sub-0003.txt" to "#(Target1)\path02\sub\sub-0003.txt".
Copying file from "C:\temp\path01\sub\sub-0004.txt" to "#(Target1)\path02\sub\sub-0004.txt".
== END ===============================
I would like to avoid hard coding the destination dir if possible.
TIA.

How to find if file exist with msbuild command(or before)

I need to find if certain file exist prior to the run of a power-shell script
The file (if exist) will be in a specific folder.
Can i check for it's existence through the proj file or something like that?
Note the second MyCheck looks at the (conditional) value of (the first) MyCheck
<PropertyGroup>
<MyCheck Condition="Exists($(MyFileOrFolderName))">true</MyCheck>
<MyCheck Condition="'$(MyCheck)'==''">false</MyCheck>
</PropertyGroup>
<Message Text="My-File-Or-Folder-Name already exists? : $(MyCheck)" />
OR
<PropertyGroup>
<MyCheck>false</MyCheck>
<MyCheck Condition="Exists($(MyFileOrFolderName))">true</MyCheck>
</PropertyGroup>
<Message Text="MyFileOrFolderNameexists? : $(MyCheck)" />
In order to execute PS script, depending on the existance of a file, you can create a Target element in your *.*proj file with condition depending on the file existance:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="14.0">
<PropertyGroup>
<ScriptLocation>.\Do-Something.ps1</ScriptLocation>
<PowerShellExe Condition=" '$(PowerShellExe)'=='' ">%WINDIR%\System32\WindowsPowerShell\v1.0\powershell.exe
</PowerShellExe>
</PropertyGroup>
<Target Name="RunPSScript" Condition="Exists($(ScriptLocation))">
<Exec Command="$(PowerShellExe) -NonInteractive -executionpolicy Unrestricted -command "$(ScriptLocation)""/>
</Target>
</Project>
See more details here about executing PS scripts from msbuild projects. You can use AfterTargets, BeforeTargers, or any other methods to control order of the execution this target.

How to Automate Publish using Cruise control and MsBuild

I tried to automate update, build and Publish using cruise Control, SVN and Ms build . Take update from svn repository and build is working fine but publishing time, in the bin folder only the project dlls are coming , but other dlls like 'system.web' , 'Newtonsoft.Json' , etc and many more dlls are not updating. My 'ccnet.config' file is given :
<cruisecontrol xmlns:cb="urn:ccnet.config.builder" xmlns="http://thoughtworks.org/ccnet/1/8">
<project name="MyProjectBuild" queue="Q1" queuepriority="1">
<sourcecontrol type="svn">
<trunkurl>https://ip/svn//trunk/CruiseControlTest</trunkurl>
<workingdirectory>F:\CruiseControlPOC\src</workingdirectory>
<username>username</username>
<password>password</password>
</sourcecontrol>
<tasks>
<msbuild>
<executable>C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe</executable>
<workingdirectory>F:\CruiseControlPOC\BuildScript\</workingdirectory>
<projectfile>Build.xml</projectfile>
</msbuild>
</tasks>
</project>
</cruisecontrol>
My 'Build.xml' file which 'ccnet.config' refer is given here:
<target name="Run">
<calltarget targets="Compile" />
<calltarget targets="DoPublish" />
</target>
<target name="Compile">
<msbuild projects="..\src\HelloWorld\HelloWorld.sln" />
</target>
<target name="DoPublish">
<msbuild projects="..\src\HelloWorld\HelloWorld\HelloWorld.csproj" targets="_CopyWebApplication;_BuiltWebOutputGroupOutput" properties="OutDir=..\Output\;WebProjectOutputDir=..\..\..\Publish\HelloWorldSite\;"> </msbuild>
<copy sourcefiles="..\Output\" destinationfolder="..\Publish\HelloWorldSite\bin\"> </copy>
</target>
Can anyone tell how to use copy command so as to copy build dlls from Output folder to Publish/bin folder , or is there any other to Publish files using MsBuild and Cruise Control. Thanks
You could add another task after your MSBuild task in the ccnet.config to call a Power Shell script after the build like so:
<exec>
<executable>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</executable>
<baseDirectory>D:\Tools</baseDirectory>
<buildArgs>-File "D:\Tools\ProgramNamePostBuildTasks.ps1"</buildArgs>
<buildTimeoutSeconds>600</buildTimeoutSeconds>
</exec>
Then in your powershell script you could use xcopy to copy files where ever you want. If you are going to copy the files to a server, you will need to map a connection to the server first. But here is a simple example of how the powershell script will look:
del "D:\Binfolder\*.*"
xcopy "D:\OutputDirectory\ProgramX.dll" "D:\BinFolder\"

MSBUILD AdditionalProperties not respected in teamcity

I have the following piece of master msbuild script which triggers child scripts with the respective properties.
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build" ToolsVersion="3.5">
<PropertyGroup>
<BuildLabel>0.8.1.2</BuildLabel>
</PropertyGroup>
<Target Name="Build" >
<CallTarget Targets="BuildApplication"/>
</Target>
<Target Name="BuildApplication" >
<ItemGroup Condition="'$(Configuration)'==''">
<ProjectToBuild Include="./Application/Application.msbuild">
<AdditionalProperties>Configuration=Publish - Beta</AdditionalProperties>
</ProjectToBuild>
<ProjectToBuild Include="./Application/Application.msbuild">
<AdditionalProperties>Configuration=Publish - Production</AdditionalProperties>
</ProjectToBuild>
</ItemGroup>
<MSBuild Projects="#(ProjectToBuild)" Properties="BuildLabel=$(BuildLabel);Platform=Any CPU" />
</Target>
</Project>
While this script works fine on my local as well as the build server, it does NOT work( $(Configuration) is not available to child as shown by teamcity logs) when the same build server checks out the code and runs the script.
what could be the problem?
The tools version in teamCity was set to default due to which the child scripts were being called using MSbuild 2.0.Changing it to 4.0 fixed it

Problems using MsBuild using command line for Publish Click Once

I have Windows application in csproj in my solution, and I want generate Publish using command line (bat, cmd).
My script is (I put \r\n for better reading):
SET MSBUILD="%SystemRoot%\Microsoft.NET\Framework\v3.5\MSBuild.exe"
SET CARWIN="..\..\Security.CarWin.csproj"
rem msbuild para publish
%MSBUILD% /target:rebuild;publish %CARWIN%
/p:ApplicationVersion="1.0.0.0"
/p:Configuration=release
/p:PublishUrl="C:\ClickOnce\CarWin.WebInstall\Publicacion\"
/p:InstallUrl="http://desserver/carwinclickonce/Publicacion/"
/p:PublishDir="C:\ClickOnce\CarWin.WebInstall\Publicacion\"
note: I'll try too using /target:publish
But in path PublishDir or PublishUrl (C:\ClickOnce\CarWin.WebInstall\Publicacion) not generates any files.
I have seen many posts in this site and google but I not found any solution.
Use PublishDir instead of PublishUrl when running from command line.
msbuild /target:publish /p:Configuration=Release;PublishDir=c:\playground\
You can also change version, like ApplicationRevision=666;MinimumRequiredVersion=1.1
Take a look at this Stack Overflow question. Basically the PublishUrl property is ignored when running ClickOnce from the command line. But you can easily add the behaviour with an additional MSBuild-task.
I've created an additional MSBuild-File, for example a build.csproj. This contains a publish-task. This task first invokes the regular MS-Build of the target-project. Afterwards it copies the result to the publish-directory. Now I invoke the 'build.csproj' instead of the reguar project-file from the command-line:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Publish" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<!-- project name-->
<ProjectName>MyExampleProject</ProjectName>
<!--properties for the project-build-->
<DefaultBuildProperties>Configuration=Release</DefaultBuildProperties>
<!-- location of the click-once stuff, relative to the project -->
<ProjectPublishLocation>.\bin\Release\app.publish</ProjectPublishLocation>
<!-- Location you want to copy the click-once-deployment. Here an windows-share-->
<ProjectClickOnceFolder>\\TargetServer\deployments</ProjectClickOnceFolder>
</PropertyGroup>
<Target Name="Publish" DependsOnTargets="Clean">
<Message Text="Publish-Build started for build no $(ApplicationRevision)" />
<!-- run the original build of the project -->
<MSBuild Projects="./$(ProjectName).csproj"
Properties="$(DefaultBuildProperties)"
Targets="Publish"/>
<!-- define the files required for click-once-->
<ItemGroup>
<SetupFiles Include="$(ProjectPublishLocation)\*.*"/>
<UpdateFiles Include="$(ProjectPublishLocation)\Application Files\**\*.*"/>
</ItemGroup>
<!-- and copy them -->
<Copy
SourceFiles="#(SetupFiles)"
DestinationFolder="$(ProjectClickOnceFolder)\"/>
<Copy
SourceFiles="#(UpdateFiles)"
DestinationFolder="$(ProjectClickOnceFolder)\Application Files\%(RecursiveDir)"/>
</Target>
<Target Name="Clean">
<Message Text="Clean project" />
<MSBuild Projects="./$(ProjectName).csproj"
Properties="$(DefaultBuildProperties)"
Targets="Clean"/>
</Target>
</Project>
I don't know if this is a problem, but I noticed that you pass the /target parameter twice?
you could you use a semi-colon delimited example:
/target:rebuild;publish
MSDN Documentation on command line parameters and MSBuild
If that also does not work you could perhaps try to debug it by passing
/verbosity:diag