In Visual Studio 2012 I was running the following Post-Build task to compile and combine my LESS files:
$(MSBuildBinPath)\msbuild.exe "$(ProjectDir)MSBuildSettingsLess.xml"
With my MSBuildSettingsLess.xml looking like:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/MsBuild/2003">
<Target Name="CompileDotlessCss">
<ItemGroup>
<Binaries Include="*.dll;*.exe"/>
</ItemGroup>
<!-- Compile dotLess CSS into minified full CSS -->
<Exec Command="$(SolutionDir)..\Tools\dotless.compiler.exe $(ProjectDir)..\GMHC\Content\less\responsive.less $(ProjectDir)..\GMHC\Content\bootstrap-responsive.less.css"/>
<Exec Command="$(SolutionDir)..\Tools\dotless.compiler.exe $(ProjectDir)..\GMHC\Content\less\bootstrap.less $(ProjectDir)..\GMHC\Content\bootstrap.less.css"/>
</Target>
</Project>
In Visual Studio 2013, that fails with the following error:
The command "C:\Program Files (x86)\MSBuild\12.0\bin\msbuild.exe "C:\Users\Administrator\Documents\Projects\MedicalMissions\GMHC\MSBuildSettingsLess.xml exited with code 9009.
The output windows shows the following:
1> 'C:\Program' is not recognized as an internal or external command,
1> operable program or batch file.
1> C:\Program Files (x86)\MSBuild\12.0\bin\Microsoft.Common.CurrentVersion.targets(4429,5): error MSB3073: The command "C:\Program Files (x86)\MSBuild\12.0\bin\msbuild.exe "C:\Users\Administrator\Documents\Projects\MedicalMissions\GMHC\MSBuildSettingsLess.xml"
Any idea why it is failing or how to get to the bottom of it?
The answer was actually pretty simple, I just had to surround the call in quotes. So the new Post-Build task is:
"$(MSBuildBinPath)\msbuild.exe" "$(ProjectDir)MSBuildSettingsLess.xml"
Looks like it is backward compatible with VS 2012 as well.
You may need quotes in your msbuild defintion (.proj) file.
Generic example:
<Exec Command=""c:\Folder With Spaces\myfile.exe""/>
The same problem occurred for me but i am using space in folder. The solution is working when i remove the space.
Do:
D:\MyDetailsMuthuvelF\Project_Document\
Don't:
D:\My Details Muthuvel F\Project_Document\
Related
I emit a console app with Mono.Cecil and I want to integrate MSBuild into the build process. But then when I run dotnet build on my custom project file, MSBuild throws an error saying Expected file "obj\Debug\net5.0\refint\test.dll" does not exist. It's trying to find the generated assembly inside the refint folder. When the assembly gets generated inside obj\Debug\net5.0\test.dll as it should. Is there a way I can change the path where MSBuild looks for the output assembly? Everything on the side of the IL generator works, I even get a runnable exe inside the build folder. Here's my project file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<OutputType>Exe</OutputType>
<DefaultLanguageSourceExtension>.ao</DefaultLanguageSourceExtension>
<OutputPath>C:\Users\filip\source\alto\samples\test\obj\Debug\net5.0\</OutputPath>
</PropertyGroup>
<Target Name="CreateManifestResourceNames" />
<Target Name="CoreCompile" DependsOnTargets="$(CoreCompileDependsOn)">
<Exec Command="dotnet run --project "$(MSBuildThisFileDirectory)\..\..\src\aoc\aoc.csproj" -- #(Compile->'$(MSBuildThisFileDirectory)', ' ') /o "#(IntermediateAssembly)" #(ReferencePath->' /r "%(Identity)"', ' ')"
WorkingDirectory="$(MSBuildProjectDirectory)" />
</Target>
</Project>
Thank you in advance.
I didn't read the whole error message.
C:\Program Files\dotnet\sdk\6.0.100-preview.7.21379.14\Microsoft.Common.CurrentVersion.targets(4527,5): error : Expected file "obj\Debug\net5.0\refint\test.dll" does not exist.
It actually points me to a file where the error was thrown. This is where:
<!-- Copy the reference assembly build product (.dll or .exe). -->
<CopyRefAssembly
SourcePath="#(IntermediateRefAssembly)"
DestinationPath="$(TargetRefPath)"
Condition="'$(ProduceReferenceAssembly)' == 'true' and '$(CopyBuildOutputToOutputDirectory)' == 'true' and '$(SkipCopyBuildProduct)' != 'true'"
>
<Output TaskParameter="DestinationPath" ItemName="ReferenceAssembly"/>
<Output TaskParameter="DestinationPath" ItemName="FileWrites"/>
</CopyRefAssembly>
It's trying to make a reference assembly when I don't need one. So I just set the ProduceReferenceAssembly property to false, since I don't need one.
So, I created a publish profile for my web app. I did this with the GUI in VS 2017 Enterprise. I then found an example of how to change the pubxml to make it zip all the output files. Eventually I ended up with this:
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<WebPublishMethod>FileSystem</WebPublishMethod>
<PublishProvider>FileSystem</PublishProvider>
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<SiteUrlToLaunchAfterPublish />
<LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
<PrecompileBeforePublish>True</PrecompileBeforePublish>
<EnableUpdateable>True</EnableUpdateable>
<DebugSymbols>False</DebugSymbols>
<WDPMergeOption>DonotMerge</WDPMergeOption>
<ExcludeApp_Data>False</ExcludeApp_Data>
<publishUrl>bin\Release\Publish</publishUrl>
<DeleteExistingFiles>True</DeleteExistingFiles>
<Configuration>Release</Configuration>
</PropertyGroup>
<Target Name="ZipPublishOutput" AfterTargets="GatherAllFilesToPublish">
<Exec Command='powershell -nologo -noprofile -command "compress-archive -force -path $(WPPAllFilesInSingleFolder)\* -destinationpath $(publishUrl).zip"' />
</Target>
</Project>
This works perfectly fine if I do a publish through Visual Studio. However, I've been trying everything I can think of to get this to work in MSBuild, with no luck.
Here is an example of what I think should work:
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\MSBuild.exe" WebApp.csproj /nologo /p:Configuration=Release /p:DeployOnBuild=true /p:PublishProfile=ReleasePublish
And for reference, here is the version of MSBuild:
Microsoft (R) Build Engine version 15.9.20+g88f5fadfbe for .NET Framework
I know MSBuild is finding the profile pubxml because I can make a change to the settings, such as publishUrl, and those changes are applied the next time I run MSBuild. What doesn't seem to work is the ZipPublishOutput target. And even running MSBuild with diagnostic verbosity shows that GatherAllFilesToPublish never runs, which would mean my target wouldn't run.
I've found many different suggestions and nothing seems to help. A lot of the solutions I found seemed to be with MSBuild not finding the pubxml, but that's not the case here.
So, is GatherAllFilesToPublish something that is Visual Studio specific, and therefore MSBuild doesn't know about it? That seems like how it's acting.
Just got the same problem using msbuild with Jenkins.
To execute the target GatherAllFilesToPublish with msbuild I had to add the target /t:GatherAllFilesToPublish in the msbuild command line.
You should have something like this :
/p:DeployOnBuild=true /p:PublishProfile=fullPathToMyPubXML.pubxml
/t:GatherAllFilesToPublish
I am curious, is it possible to reference a macro on a command line property assignment for MSBuild?
E.g:
msbuild.exe MySolution.sln /p:CustomBeforeMicrosoftCSharpTargets="$(SolutionDir)\custom.targets"
Would this also work when specified as "MSBuildArguments" from an "Edit Build Definition"/"Queue New Build" from Visual Studio connected to TFS?
E.g:
/p:CustomBeforeMicrosoftCSharpTargets="$(SolutionDir)\custom.targets"
Because it doesn't appear to be importing these targets for me. But the targets file is definitely there, alongside the solution, in the build workspace.
I don't want to have to specify an absolute path. Not sure how working with relative paths is meant to work here, can't find any advice on the internet, and debugging it is quite difficult, as it is called on a build agent using a workflow. The workflow logging is definitely reporting it is calling MSBuild with these arguments, but nowhere in the verbose logging output can I see it is making reference to the CustomBeforeMicrosoftCSharpTargets target, or calling it.
EDIT
I wrote a little test build project buildme.proj to further my understanding.
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<SetMe>NotInTheSandbox</SetMe>
</PropertyGroup>
<PropertyGroup>
<SomeMacroValue>c:\Sandbox\BuildTest</SomeMacroValue>
</PropertyGroup>
<PropertyGroup>
<AlreadySet>$(SomeMacroValue)\InTheSandbox</AlreadySet>
</PropertyGroup>
<Target Name="Build">
<Message Text="I am building!" />
<Message Text="Some macro value: $(SomeMacroValue)" />
<Message Text="$(SetMe)" />
<Message Text="$(AlreadySet)" />
</Target>
</Project>
When I execute with the command:
msbuild buildme.proj /p:SetMe="$(SomeMacroValue)\StillNotInSandbox"
I get the following output:
Microsoft (R) Build Engine version 12.0.31101.0
[Microsoft .NET Framework, version 4.0.30319.42000]
Copyright (C) Microsoft Corporation. All rights reserved.
Build started 10/12/2015 22:12:08.
Project "C:\Sandbox\BuildTest\buildme.proj" on node 1 (default targets).
Build:
I am building!
Some macro value: c:\Sandbox\BuildTest
$(SomeMacroValue)\StillNotInSandbox
c:\Sandbox\BuildTest\InTheSandbox
Done Building Project "C:\Sandbox\BuildTest\buildme.proj" (default targets).
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:00.02
So clearly, it is not behaving how I expected: The macro identifier appears in the output message text.
Is there a solution to this?
A "macro" like $(SolutionDir) exists only in VisualStudio and VS passes the value to MSBuild.
Instead MSBuild makes Environment variables available as properties, so a batch file like this
set SomeMacroValue=foo
msbuild buildme.proj /p:SetMe="$(SomeMacroValue)\StillNotInSandbox"
is probably what you are looking for.
And you can set environment variables per-user or per-machine (Control Panel\All Control Panel Items\System Advanced System Settings, Environment variables).
I'm trying to use a build script to run the dotless.compiler.exe to compile my .less files into .min.css on build:
<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/MsBuild/2003">
<!--
This MSBuild Script will compile all [*.less] files in the /CSS folder to their [*.min.css] counterparts.
-->
<ItemGroup>
<LessFiles Include="Styles\*.less" />
</ItemGroup>
<Target Name="CompileDotlessCss" AfterTargets="AfterBuild">
<ItemGroup>
<Binaries Include="*.dll;*.exe"/>
</ItemGroup>
<!-- Compile dotLess CSS into minified full CSS -->
<Exec Command="[MSBuild]\dotless.compiler.exe -m %(LessFiles.FullPath) $([System.String]::Copy('%(LessFiles.FullPath)').Replace('.less','.min.css'))" />
</Target>
</Project>
But when I build I get:
The command "[MSBuild]\dotless.compiler.exe -m C:\Source Control\MyProject\MyProject.Web\Styles\main.less C:\Source Control\MyProject\MyProject.Web\Styles\main.min.css" exited with code -1.
I suspect it has to do either with my project being under source control or simply the fact that the file path has a space in the "Source Control" folder.
How can I wrap the path in quotes (since the command itself is in quote)?
If it's the source control factor and it fails because the files are locked (I tried building with the file checked in).. how do I deal with this? I obviously want to keep my project under source control.
You can use "" and '' interchangeably in MSBuild files, inc. for paths with spaces, worst cases might require XML escapes like ". For better output beyond exit code try to up verbosity to detailed or diagnostic, may be it's something to do with [MSBuild], shouldn't it be a $() property?
I am executing MSBuild from a batch file. The MSBuild script is in a different directory than the directory I want MSBuild to consider the working directory when running the script. When invoking MSBuild.exe, how do I change its working directory?
Edit: More details
Let's say I have an MSBuild script located on some other server. I want to run a command thusly:
msbuild.exe \\my_server\c$\My\Path\To\Scripts\TestScript.msbuild
I run that command with my command prompt at c:\temp. Let's say my TestScript.msbuild has a task to create a file. The file has no path just a filename. I would expect that the file gets created inside c:\temp. But it doesn't it gets created next to the msbuild file that is sitting on the server. This is the behavior I want to change.
Edit #2
Here is the script I'm using in my test:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Files Include="HelloWorld.txt" />
</ItemGroup>
<Target Name="TouchFiles">
<Touch Files="#(Files)" AlwaysCreate="True" />
</Target>
</Project>
I am going into a command shell CDing into c:\temp and then executing the script. With or without the /p:OutDir switch that #Nick Nieslanik mentions, the HelloWorld.txt file appears in the folder where the *.msbuild file is and not c:\temp.
I ran across this while looking for a solution to my problem. Here's my solution (build script):
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Default">
<Exec Command="build.bat" WorkingDirectory="..\[your dir]\" />
</Target>
</Project>
I believe that's more what you were originally looking for?
My problem was that my batch file called another that it expected to be in the same directory, but since my ms build script was being run elsewhere, the batch file failed to find the second batch file.
#jkohlhepp - I see now. You are doing the opposite of what I described in my comment to some degree.
MSBuild common targets use the MSBuildProjectDirectory to determine the output folder unless you override that. So in your case, you could run
msbuild.exe \\my_server\c$\My\Pat\To\Scripts\TestScript.msbuild /p:OutDir=c:\temp
to force the output to be dropped in that location.
EDIT:
Given the project file above, you'd need to edit it to do something like the following for this to work:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<OutDir Condition=" '$(OutDir)' == '' ">bin\debug\</OutDir>
</PropertyGroup>
<ItemGroup>
<!-- Without prefacing files with paths, they are assumed relative to the proj file -->
<FilesToCreate Include="$(OutDir)HelloWorld.txt" />
</ItemGroup>
<Target Name="TouchFiles">
<Touch Files="#(FilesToCreate)" AlwaysCreate="True" />
</Target>
</Project>
In current versions of MSBuild the well-known property MSBuildStartupDirectory can be used in the msbuild file to retrieve the absolute path of the folder where MSBuild is called.
https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-reserved-and-well-known-properties?view=vs-2019
This option perhaps did not exist in msbuild around the time when the question was asked. I didn't want to spend too much time investigating it.