I am trying to call MSBuild from a command line. Everything was working fine when I was using a path that had no spaces, but now I have a path that has spaces and the command is failing.
Command (works):
"C:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe"
/t:Rebuild "C:\Projects\myProject.csproj"
/p:OutDir=c:\temp\deploy\funAndGames\Deployment\bin\
/p:WebProjectOutputDir=c:\temp\deploy\funAndGames\Deployment\
/p:Configuration=Release
I then added quotes and changed OutDir to OutPath (doesn't work):
"C:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe"
/t:Rebuild "C:\Projects\myProject.csproj"
/p:OutPath="c:\temp\deploy\funAndGames\Deployment\bin\"
/p:WebProjectOutputDir="c:\temp\deploy\funAndGames\Deployment\"
/p:Configuration=Release
What I am aiming for is something like this (doesn't work):
"C:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe"
/t:Rebuild "C:\Projects\myProject.csproj"
/p:OutPath="c:\temp\deploy\fun and games\Deployment\bin\"
/p:WebProjectOutputDir="c:\temp\deploy\fun and games\Deployment\"
/p:Configuration=Release
Any help on the syntax around OutDir/OutPath and WebProjectOutputDir with spaces? Is it possible? If it isn't does anyone know what the reason is (due to some Url's not having spaces type thing?)
Just found this out an answer to this old question.
To handle spaces, you should use the escape character \ on all folders. Basically
/p:OutPath="c:\temp\deploy\fun and games\Deployment\bin\"
should be
/p:OutPath="c:\\temp\\deploy\\fun and games\\Deployment\\bin\\"
and magically it works!
Try add "
ex:
/p:OutPath=""c:\temp\deploy\fun and games\Deployment\bin\""
Msbuild also seems to work with spaces in the OutDir if you switch \ to /, while using quotes:
/p:OutDir="c:/temp/deploy/fun and games/out/"
/p:WebProjectOutputDir="c:/temp/deploy/fun and games/Deployment/"
For me the working solution is:
/p:SQLCMD="\"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE\""
In other words: Putting all the string into quotes (the external quotes aren't passed as value to MSBuild).
The value inside MSBuild for this property is: "C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE" (with the quotes).
> "C:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe"
> /t:Rebuild
> "C:\Projects\myProject.csproj"
/p:OutPath="c:\temp\deploy\funAndGames\Deployment\bin\"
----------------------------------------
/p:WebProjectOutputDir="c:\temp\deploy\fun and games\Deployment\"
----------------------------------------
> /p:Configuration=Release
Try this.
Also try via VSStudio GUI. Then copy the settings & try with MS Build.
If you have multiple parameters in a switch you can't really 'avoid' the problem by fixing the path. What you can do is put your parameters of the switch between " some_parameters1 some_parameters2 ".
Something like:
<Exec Command="SomeCommand /useMultipleParameterSwitch="value1:blabla1 | value2:blabla2""/>
Of course a lot depends of the syntax of the switches but that works for me and my team.
To do this when using a .proj file and your path is included in properties like $(DeployFolder) and $(NuGetExe), you can use """ like this:
<Exec Command=""$(NuGetExe)" pack -OutputDirectory "$(DeployFolder)"" />
Related
Is there a way to specify the output like
msbuild zlibvc.sln /t:zlibvc /p:OUT=$(OutDir)\zlib1.dll
by example, changing the output from the standard zlibwapi.dll to zlib1.dll ?
In case of C# projects, we do modify csproject file in the following two places like this:
Before:
<AssemblyName>zlibwapi.dll</AssemblyName>
and
<OutputPath>bin\</OutputPath>
After:
<ZLibAssemblyName Condition="$(ZLibAssemblyName) == '' ">zlibwapi.dll</ZLibAssemblyName>
<AssemblyName>$(ZLibAssemblyName)</AssemblyName>
and
<ZLibOutputPath Condition="$(ZLibOutputPath) == '' ">bin\</ZLibOutputPath>
<OutputPath>$(ZLibOutputPath)</OutputPath>
Then call your msbuild command like below:
msbuild zlibvc.sln /t:zlibvc /p:ZLibOutputPath=$(OutDir)\ /p:ZLibAssemblyName=zlib1.dll
Hope this or a similar thing works in vcbuild task as well.
For example given a project quickie.vcxproj, to build it under c:\output\c without specifying the project name explicitly:
msbuild /t:Rebuild /p:Configuration=Debug /p:PlatformToolset=v100 quickie\quickie.vcxproj /p:OutDir=c:\output\c\$(MSBuildProjectName)\
The result is a directory c:\output\c\$(MSBuildProjectName)
Required result is: c:\output\c\quickie
Attemping with c:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild
No. Exceeding thirty characters
I've got my windows shell configured as displaying black text on a white background. This makes it almost impossible to read the default msbuild output due to the very pale colours (especially the yellow warnings).
Therefore I'd like to try one of the following, but I can't work out if it is possible.
I'd like to set a global setting to permanently turn off colourized output in msbuild; or
If (1) isn't possible is it possible to turn this output per-invocation (e.g. with command line arguments).
Does anyone know how to do one of the above?
In MSBuild 4.0 this is possible using the /consoleloggerparameters or /clp switch:
msbuild C:\some_path\your.sln /clp:disableconsolecolor
Alternatively, for previous MSBuild engines, this is possible using PowerShell:
Out-Host will display the default color:
Powershell -Command "msbuild C:\some_path\your.sln | out-host"
Write-Host will let you customize the colors:
Powershell -Command "msbuild C:\some_path\your.sln | write-host -foreground "white""
To completely disable colors use the /clp:disableconsolecolor option when invoking MSBuild.exe (for more information on the /clp option run MSBuild.exe /?).
Update as #KMoraz has commented, and updated his answer to, this only works with MSBuild 4.0 onwards.
If you want to disable color output you can also use the following (which will not work with MSBuild 4.0):
MSBuild.exe arguments > CON 2>&1
This got me curious ;-) so here is one more option that should work with all versions of MSBuild.exe and doesn't rely on CON redirection:
MSBuild.exe arguments 2>&1| findstr /r ".*"
Basically, what happens is that all lines of output are piped through findstr.exe since that uses a pattern to match "everything", all lines are simply output again, but loosing their attributes (color) information. In my tests the 2>&1 (redirect stderr to stdout) was not really necessary, as it looks MSBuild doesn't output any (colored) messages to stderr, but I added it for good measure.
The Story So Far
I've got a nice solution with a desktop application project, a few library projects, and a couple of development tools projects (also desktop applications). At the moment, my build server outputs all of the code into one OutputPath. So we end up with
drop-x.y.z\
Company.MainApplication.exe <-- main application
Company.MainApplicationCore.dll <-- libraries
Helper.exe <-- developer tools
Grapher.exe
Parser.exe
... <-- the rest of the output
But, we're growing up and people outside of our team want access to our tools. So I want to organize the output. I decided that what we would want is a different OutputPath per executable project
drop-x.y.z\
Company.MainApplication\
Company.MainApplication.exe <-- main application
Company.MainApplicationCore.dll <-- libraries
... <-- application specific output
Helper\
Helper.exe <-- developer tools
... <-- tool specific output
Grapher\
Grapher.exe
...
Parser\
Parser.exe
...
What I Did
I found this simple command. I like it because it retains all the Solution working-dir context that makes msbuild a pain.
msbuild /target:<ProjectName>
For example, from my solution root as a working directory, I would call
PS> msbuild /target:Helper /property:OutputPath="$pwd\out\Helper"
I'm testing this from PowerShell, so that $pwd resolves to the full path to my working directory, or the Solution root in this case. I get the output I desire.
However, when I run this command
PS> msbuild /target:Company.MainApplication /property:OutputPath="$pwd\out\Company.MainApplication"
I get the following error output (there's no more information, I ran with /verbosity:diagnostic)
The target "Company.MainApplication" does not exist in the project.
What I Need
The command fails on any project with a dot or dots in the name. I tried with many combinations of working directories and properties. I tried several ways of escaping the property values. I also tried running the command from a <Task> in a targets file.
I need to know either
A) How to fix this command to work property
B) How to achieve the same output with minimal friction
Try using an underscore as an escape character for the dot in the target parameter, e.g.
msbuild /target:Company_MainApplication /property:OutputPath="$pwd\out\Company.MainApplication"
Specify the target after the -target: switch in the format :. If the project name contains any of the characters %, $, #, ;, ., (, ), or ', replace them with an _ in the specified target name.
https://learn.microsoft.com/en-us/visualstudio/msbuild/how-to-build-specific-targets-in-solutions-by-using-msbuild-exe?view=vs-2019
Dan Nolan's answer and comments are correct. Just want to supplement the Microsoft documentation.
The /targets: switch is to identify a <Target to run in the project file. You need to supply your .csproj file as a an argument that is not prefixed by a /xx option marker.
You might also want to work based on the .sln file. In that case, you still dont specify the project in the .sln to build in this manner. I'll leave you to search up the correct syntax in case that's what you end up doing.
I'm running msbuild from the command line with the following:
msbuild mysolution.sln -p:outputdir=c:\mydir
When I run this, the outputdir is being ignored and the default specified in the csproj file is being used.
The MSDN doc for this tool says that I should be able to override the build directory using this parameter. What am I doing wrong?
You should use OutputPath and more important you should use the right syntax :
msbuild mysolution.sln /p:OutputPath=c:\mydir
Note that OutputPath is preferred over OutDir. The documentation used to be wrong about this, but I see that they've finally fixed it.
Beyond that, it's difficult to say exactly what the problem is, since you didn't show the exact path that you're passing as a parameter. There are two possible problems that I can imagine:
The OutputPath option specifies the path to the output directory relative to the project directory. That means you can't set it to a global path like C:\mydir. I assume it is unable to find the path you specified, and so it defaults to the one specified in your project file.
If the path that you're actually specifying as a parameter contains spaces, the command is likely to fail. I believe you need to wrap the path in quotes and append an extra backslash to the end of the path string.
I believe you should be using OutputPath.