MSBuild: Specifying a target from the command line

msbuild

I have an MSBuild task to build a specific project in a solution file. It looks something like this:

<Target Name="Baz">
  <MSBuild Projects="Foo.sln" Targets="bar:$(BuildCmd)" />
</Target>

From the command line, I can set my BuildCmd to either Rebuild or Clean and it works as expected:

msbuild /target:Baz /property:BuildCmd=Rebuild MyMsbuildFile.xml
msbuild /target:Baz /property:BuildCmd=Clean MyMsbuildFile.xml

But what word do I use to set BuildCmd to in order to just build? I've tried Build and Compile and just leaving it blank or undefined, but I always get an error.

msbuild /target:Baz /property:BuildCmd=Build MyMsbuildFile.xml
Foo.sln : error MSB4057: The target "bar:Build" does not exist in the project.

msbuild /target:Baz /property:BuildCmd=Compile MyMsbuildFile.xml
Foo.sln : error MSB4057: The target "bar:Compile" does not exist in the project.

msbuild /target:Baz MyMsbuildFile.xml
Foo.sln : error MSB4057: The target "bar:" does not exist in the project.

Best Answer

I understood that you want to build a target with a specific command: Build, Clean, etc.

This is how I would do it.

Create a property to receive your build command, when not specified defaults to Build

<PropertyGroup>
  <BuildCmd Condition=" '$(BuildCmd)' == ''">Build</BuildCmd>
</PropertyGroup>

After, create the target that will start MSBuild with the specified target in the parameter:

<Target Name="Stackoverflow">
  <MsBuild Projects="Foo.sln" Targets="$(BuildCmd)" />
</Target>

Then call your MSBuild file with the target and BuildCmd parameter like so:

msbuild msbuild.xml /t:Stackoverflow /p:BuildCmd=Clean

Just make sure the target exists in the solution or project file.