MSBuild + .dbproj - msbuild

i am trying to deploy db project through msbuild.
i am getting below error
MSBuild - Deploy DB project
C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe
/nologo
/noconsolelogger "C:\Builds\2\Test\ePlanner\Sources\WebSiteBuildTest\Database\ePlanner\ePlanner\ePlanner.dbproj"
/m:1
/t:"Deploy"
/fl
/flp:"logfile=deploymentdb.log;encoding=Unicode;verbosity=normal"
/p:TargetDatabase="ePlanner3";"TargetConnectionString="Data Source=SACHIN-PC%3Buser=sa%3Bpwd=M0nday!";"DefaultDataPath="C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA";DeployToDatabase=true
/p:OutDir="C:\Builds\2\Test\ePlanner\Binaries\\"
/p:Configuration="Debug"
/p:RunCodeAnalysis="False"
/dl:WorkflowCentralLogger,"C:\Program Files\Microsoft Team Foundation Server 2010\Tools\Microsoft.TeamFoundation.Build.Server.Logger.dll";"Verbosity=Normal;BuildUri=vstfs:///Build/Build/53;InformationNodeId=10631;TFSUrl=http://sachin-pc:8080/tfs/DefaultCollection;"*WorkflowForwardingLogger,"C:\Program Files\Microsoft Team Foundation Server 2010\Tools\Microsoft.TeamFoundation.Build.Server.Logger.dll";"Verbosity=Normal;"
MSBUILD : error MSB1008: Only one project can be specified.
Switch: Source=SACHIN-PC%3Buser=sa%3Bpwd=M0nday!;
DefaultDataPath=C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA;DeployToDatabase=true
For switch syntax, type "MSBuild /help"
TF270015: 'MSBuild.exe' returned an unexpected exit code. Expected '0'; actual '1'.
Please help me out

In order to run a .dbproj in MsBuild the machine needs to have VisualStudio with the database project components installed, something to do with licensing. I cannot tell from the error dump that you provided if this is what is causing you the issue but it solved our issue when building on our build server.

You've got conflicting quotation marks in your command line:
/p:TargetDatabase="ePlanner3";"TargetConnectionString="Data Source=SACHIN-PC%3Buser=sa%3Bpwd=M0nday!";"DefaultDataPath="C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA";DeployToDatabase=true
The TargetConnectionString property is completely surrounded in double quotes, but then you use double quotes to specify the connection string. Try escaping the quotes for the connection string.

Consider converting the project to .sqlproj. This new format provides a "Publish" MSBuild target. You just need to specify at SqlPublishProfilePath property a publish profile file with your deployment options.
Look that sample:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="ProjectsBuild">
<ItemGroup>
<!-- Defines a collection of projects to deploy -->
<ProjectsToPublish Include="SQLServer.sqlproj">
<Properties>SqlPublishProfilePath=myprofile.publish.xml</Properties>
</ProjectsToPublish>
<!-- Runs the target Publish -->
<MSBuild Projects="#(ProjectsToPublish)" Targets="Publish"/>
</ItemGroup>
</Project>
Visual Studio 2010/2012 needs the SSDT (Sql Server Data Tools - ) installed to understand .sqlproj. Your build server too. It will install the MSBuild .targets extensions that contains the target "Publish".
Publish profile example (it's a MSBuild project):
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<CreateNewDatabase>True</CreateNewDatabase>
<TargetDatabaseName>DB1</TargetDatabaseName>
<TargetConnectionString>Data Source=server1\dev;Integrated Security=True;Pooling=False</TargetConnectionString>
<BackupDatabaseBeforeChanges>False</BackupDatabaseBeforeChanges>
<BlockOnPossibleDataLoss>False</BlockOnPossibleDataLoss>
<NoAlterStatementsToChangeCLRTypes>False</NoAlterStatementsToChangeCLRTypes>
<DropObjectsNotInSource>True</DropObjectsNotInSource>
<DeployDatabaseInSingleUserMode>False</DeployDatabaseInSingleUserMode>
<DeployScriptFileName>db1.sql</DeployScriptFileName>
<ProfileVersionNumber>1</ProfileVersionNumber>
</PropertyGroup>
</Project>

Related

MSBuild doesn't deploy and prints no error

I'm trying to publish my project using MSBuild. Here's the command:
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSBuild.exe" D:\HamrahFarda\KhandeShow\KhandeShow.sln /t:UserService /p:DeployOnBuild=true /p:PublishProfile=DeployUserService /p:SolutionDir=D:\HamrahFarda\KhandeShow /property:PublishFolder=C:\Publish\KhandeShow\UserService
That full command can be broken into these parts:
I use MSBuild.exe that is installed with VS 2017 community edition for .NET Core.
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSBuild.exe"
I have a solution, and I only want to build and publish a project inside it. Thus:
D:\HamrahFarda\KhandeShow\KhandeShow.sln /t:UserService
I want it to deploy the built project:
/p:DeployOnBuild=true
I've created a publish profile beforehand:
/p:PublishProfile=DeployUserService
This is the publish profile:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PublishProtocol>FileSystem</PublishProtocol>
<Configuration>Release</Configuration>
<TargetFramework>netcoreapp2.0</TargetFramework>
<PublishDir>C:\Publish\KhandeShow\UserService</PublishDir>
</PropertyGroup>
</Project>
And last but not least, I'm overriding some properties and providing new ones to be used inside .csproj:
/p:SolutionDir=D:\HamrahFarda\KhandeShow /property:PublishFolder=C:\Publish\KhandeShow\UserService
But without any error (with a bunch of warnings) it doesn't publish anything to the destination folder. I'm stuck. Can you help please, cause MSBuild is really not handy and friendly.
From the log you posted I was able to determine the following:
The project you are trying to publish via a web publishing method is not a web project.
UserService.csproj is defined as
<Project Sdk="Microsoft.NET.Sdk">
which is a "normal" sdk-style .NET project. If you want to perform a file system publish of these projects, use dotnet publish or its msbuld equivalent msbuild /t:Publish instead.
If the project really is a web project, you should change it to
<Project Sdk="Microsoft.NET.Sdk.Web">
To get web tooling and publishing support.
Update: also note that PublishDir in the publish profile is not the correct variable to set in the publish profile. Use PublishUrl instead so that the FileSystem publish method will copy the files to the right directory.

Publish web project from JetBrains Rider

I am giving Rider a try, and so far, quite like it.
One feature I use in Visual Studio quite often is right click on a web project and publish to our testing server.
I cannot find a similar option in Rider, so what I have done is, create a run configuration, with the following settings:
Exe path: C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/MSBuild/15.0/Bin/amd64/msbuild.exe
Arguments: MySolution.sln /m /p:DeployOnBuild=True /p:PublishProfile=My-Project "/p:platform=Any CPU" /p:configuration=Release /p:VisualStudioVersion=15.0 /p:Password=****
Working Directory: C:\SolutionFolder
When I want to publish, I select it from the drop-down and click run.
This works 100%.
My question is, is this the best way to do it, sans setting up a CI pipeline? Am I missing an option or setting in the IDE?
As of June 2018, Rider doesn't have UI for publishing.
There is a feature request which you can vote for, once logged in YouTrack.
As a workaround, one can create a '.NET Executable' configuration like you did, and run it when you want to publish your project.
More detailed instructions follows:
Run > Edit Configuration
Add new configuration > .NET Executable
Name = your project name
Exe path = path to your MSBuild (for example C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/MSBuild/15.0/Bin/amd64/MSBuild.exe)
Program arguments = YourSolution.sln /t:"Your_Project" /p:PublishProfile=YourPublishProfile.pubxml /p:Configuration=Debug /p:DeployOnBuild=true /m
Working directory = C:/path/to/solution/dir/
Notes:
the project publish profile is usually located in the project folder, under Properties/PublishProfiles. If you don't have one you can start with the example reported below;
you need to replace the dots (.) in the project name with underscores (_). In the example above Your.Project was passed as Your_Project;
you can specify a different publishing directory, if not already specified in the publish profile, by adding the argument /p:PublishDir="C:/path/to/publish/dir/";
if you don't have Visual Studio installed on your machine, you can use the MSBuild bundled with the Build Tools for Visual Studio 2017.
Example of publish profile:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<WebPublishMethod>FileSystem</WebPublishMethod>
<LastUsedBuildConfiguration>Debug</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<SiteUrlToLaunchAfterPublish />
<LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
<ExcludeApp_Data>False</ExcludeApp_Data>
<publishUrl>..\YourPublishDirectory</publishUrl>
<DeleteExistingFiles>True</DeleteExistingFiles>
<ExcludeFilesFromDeployment>bin\*.dll.config</ExcludeFilesFromDeployment>
</PropertyGroup>
</Project>
Latest versions of Rider support publishing via UI. If you don't have Visual Studio installed on your machine, make sure the web project has Build.Microsoft.VisualStudio.Web.targets nuget package installed.

SSDT Publish does not use connection string from MSBuild command line

I'm trying to publish a .sqlproj project using MSBuild from my (TeamCity) build server. I use a .publish.xml for setting up most of the publish settings, but want to specify the connection string on the command line, so I can securely store it in TeamCity properties.
However, it seems like MSBuild doesn't use the connection string specified on the command line at all, and insists on using the one in the .publish.xml. I've tried removing it from and emptying it in the XML, but MSBuild doesn't like that so much, and spits back Before you can deploy a database, you must specify a connection string in the project properties or at a command prompt., which, of course, is exactly what I'm trying to do...
The full command used to publish is as follows:
msbuild /t:Build;Publish /p:SqlPublishProfilePath="c:\path\to\profile.publish.xml" /p:Publish_TargetConnectionString="the connection string" "c:\path\to\project.sqlproj"
I've tried using /p:TargetConnectionString and /p:SqlPublishProfilePath="c:\path\to\profile.publish.xml";Publish_TargetConnectionString="the connection string" as well, but to no avail.
The .publish.xml is as follows:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<IncludeCompositeObjects>True</IncludeCompositeObjects>
<TargetDatabaseName><!-- snip --></TargetDatabaseName>
<DeployScriptFileName><!-- snip -->.sql</DeployScriptFileName>
<TargetConnectionString><!-- snip snip --></TargetConnectionString>
<ScriptDatabaseOptions>False</ScriptDatabaseOptions>
<BlockOnPossibleDataLoss>False</BlockOnPossibleDataLoss>
<DeployDatabaseInSingleUserMode>False</DeployDatabaseInSingleUserMode>
<ProfileVersionNumber>1</ProfileVersionNumber>
</PropertyGroup>
</Project>
SSDT is at the latest version I could download from Microsoft as of today.
Has anyone ever gotten this to work?
As said above in the comments, I resorted to using MSBuild for building the .dacpac and sqlpackage.exe to publish it to SQL Server, and that works fine.

Error of 'Source (dirPath) and destination (iisApp) are not compatible for the given operation' when trying to deploy package

I'm trying to deploy a package via the MSDeploy task from within MSBuild.
I have configured two Item Groups to represent my source and destination.
The source is a package I have created eg. Solution.zip
The destination is IIS 7 on a remote server.
The configuration looks like this :
<ItemGroup>
<DeploySource Include="package">
<Path>$(PackagePath)</Path>
</DeploySource>
</ItemGroup>
<ItemGroup>
<DeployDestination Include="iisApp">
<ComputerName>https://myserver.com/msdeploy.axd</ComputerName>
<UserName>XXXXXXXX</UserName>
<Password>XXXXXXXX</Password>
<AuthType>Basic</AuthType>
<Path>Default Web Site/Umbraco.Web_deploy</Path>
</DeployDestination>
</ItemGroup>
<PropertyGroup>
<ConfigFileName>Staging.config</ConfigFileName>
</PropertyGroup>
I then call the MSDeploy task within MSBuild like this :
<Target Name="Deploy_v2">
<!-- Using ContinueOnError due to a bug in MSDeploy task-->
<MSDeploy
ContinueOnError="true"
ToolPath="C:\Program Files\IIS\Microsoft Web Deploy V3"
Verb="sync"
Source="#(DeploySource)"
Destination="#(DeployDestination)"
AllowUntrusted="true"
Replace="objectName=filepath,match=Configs\$(ConfigFileName),replace=web.config"
/>
</Target>
This results in the following command line execution
C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe -source:package='C:\CLIENTS\DAM\Components\Umbraco\SiteTemplate_v6_1_6\Output\Package\UmbracoTemplate_v6_1_6.zip' -dest:iisApp='Default WebSite/Umbraco.Web_deploy',ComputerName='https://myserver.com/msdeploy.axd',UserName='XXXXXXX',Password='XXXXXXXX',AuthType='Basic' -verb:sync -replace:objectName=filepath,match=Configs\Staging.config,replace=web.config -allowUntrusted
Which unfortunately results in the following error :
MSDEPLOY : Error: Source (dirPath) and destination (iisApp) are not compatible for the given operation.
It seems to me that MSDeploy is viewing my package as a dirPath. When I created the package it did use a dirPath as its source.
As the error states dirPath and iisApp are not compatible providers, although iisApp does use dirPath. See here
iisApp can take a directory as the source so try packaging the target directory via iisApp as follows:
msdeploy -verb:sync -source:iisApp=c:\inetpub\wwwroot -dest:package=Package.zip
Btw if you are using Visual Studio you can generate an MSDeploy package using MSBuild with /t:Publish.

Overriding MSBuildExtensionsPath in the MSBuild task is flaky

This is already cross-posted at MS Connect:
https://connect.microsoft.com/VisualStudio/feedback/details/560451
I am attempting to override the property $(MSBuildExtensionsPath) when building a solution containing a C# web application project via msbuild. I am doing this because a web application csproj file imports the file "$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v9.0\WebApplications\Microsoft.WebApplication.targets". This file is installed by Visual Studio to the standard $(MSBuildExtensionsPath) location (C:\Program Files\MSBuild). I would like to eliminate the dependency on this file being installed on the machine (I would like to keep my build servers as "clean" as possible). In order to do this, I would like to include the Microsoft.WebApplication.targets in source control with my project, and then override $(MSBuildExtensionsPath) so that the csproj will import this included version of Microsoft.WebApplication.targets. This approach allows me to remove the dependency without requiring me to manually modify the web application csproj file.
This scheme works fine when I build my solution file from the command line, supplying the custom value of $(MSBuildExtensionsPath) at the command line to msbuild via the /p flag. However, if I attempt to build the solution using the MSBuild task in a custom msbuild project file (overriding MSBuildExtensionsPath using the "Properties" attribute), it fails because the web app csproj file is attempting to import the Microsoft.WebApplication.targets from the "standard" Microsoft.WebApplication.targets location (C:\Program Files\MSBuild). Notably, if I run msbuild using the "Exec" task in my custom project file, it works. Even more notably, the FIRST time I run the build using the "MSBuild" task AFTER I have run the build using the "EXEC" task (or directly from the command line), the build works.
Has anyone seen behavior like this before? Am I crazy? Is anyone aware of the root cause of this problem, a possible workaround, or whether this is a legitimate bug in MSBuild?
Steps to Reproduce:
1) Create a new empty solution in MSVS 2008 (Fake.sln)
2) Add a new C# web application to the solution (WebApplication1.csproj)
3) Close MSVS
4) Copy the contents of "C:\Program Files\MSBuild\" to a directory called "MSBuildExtensions" in the directory containing your solution.
5) rename the directory "C:\Program Files\MSBuild\Microsoft\VisualStudio\v9.0\WebApplications" so that WebApplication1.csproj will not be able to import Microsoft.WebApplication.targets from that location.
6) Create a custom MSBuild project file called "TestBuild.proj" in the same directory as the solution. It should have the following content:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="BuildMSBuild">
<PropertyGroup>
<MSBuildExtensionsPath>$(MSBuildProjectDirectory)\MSBuildExtensions\</MSBuildExtensionsPath>
<BuildThis>Fake.sln</BuildThis>
</PropertyGroup>
<Target Name="BuildMSBuild">
<MSBuild Projects="$(BuildThis)" Properties="MSBuildExtensionsPath=$(MSBuildExtensionsPath);" Targets="Clean" />
<MSBuild Projects="$(BuildThis)" Properties="MSBuildExtensionsPath=$(MSBuildExtensionsPath);"/>
</Target>
</Project>
7) execute "msbuild TestBuild.proj" from a MSVS command prompt (note: the build may succeed the first time, but will fail if you run more than once)
Did you try setting the environment variable MSBuildExtensionPath in the CMD prompt and then running your build?
For example:
C:\> SET MSBuildExtensionsPath=C:\My\MSBuild\Extensons
Then on this project file:
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Build">
<Message Text='MSBuildExtensionsPath="$(MSBuildExtensionsPath)"' />
</Target>
</Project>
you will get the following output:
c:\Users\chuckeng\Desktop\ConsoleApplication1>"C:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe" my.proj
Microsoft (R) Build Engine Version 3.5.30729.4926
[Microsoft .NET Framework, Version 2.0.50727.4927]
Copyright (C) Microsoft Corporation 2007. All rights reserved.
Build started 6/25/2010 1:04:05 PM.
Project "c:\my.proj" on node 0 (default targets).
MSBuildExtensionsPath="C:\My\MSBuild\Extensons"
Done Building Project "c:\my.proj" (default targets).
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:00.03
This works from v4.0 as well. Although, support is generally better in v4.0 for things like this. And, v4.0 is 100% backward compatible (bugs not withstanding). So, you can build your v3.5 and prior projects with v4.0. Just select ToolsVersion 3.5.
msbuild my.proj /tv:3.5
Hope this helps...
Chuck England
Visual Studio
Program Manager - MSBuild
This is a bug in MSBuild 3.5 but it is fixed in MSBuild 4.
If you can, switch to MSBuild 4 (you still can compile your 3.5 projects), otherwise you'll have to override the property in the project file.
It works fine if you override MSBuildExtensionsPath directly in the web app .csproj file.
<PropertyGroup>
<MSBuildExtensionsPath>C:\Users\madgnome\Desktop\msbuild</MSBuildExtensionsPath>
<!-- It works too with relative path -->
<!--<MSBuildExtensionsPath>..\msbuild</MSBuildExtensionsPath>-->
</PropertyGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
Don't know if this might help anyone in the future, but I was able to use the following at the top of my file and it works as I would expect in both 32 and 64 bit build environments.
<PropertyGroup>
<MSBuildExtensionsPath Condition=" '$(MSBuildExtensionsPath64)' != '' ">$(MSBuildExtensionsPath64)</MSBuildExtensionsPath>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\ExtensionPack\4.0\MSBuild.ExtensionPack.tasks"/>