tags:

views:

6638

answers:

4

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.

A: 

Just edit the sln file yourself and find out - MSBuild is a real easy syntax, just look for targets.

ripper234
The .sln file isn't in MSBuild syntax.
+3  A: 

From: http://msdn.microsoft.com/en-us/library/ms164311.aspx

/target:targets

Builds these targets in this project. Use a semicolon or a comma to separate multiple targets, or specify each target separately. /t is also acceptable. For example: /target:Resources;Compile

Tim Stewart
The problem is specifying what to do with each target: build, rebuild, clean. Part of the problem is that the term "target" is used on the command line to specify a target within the msbuild file, and again to specify which project to build within a solution file (namespace collision).
A: 

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 the 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.

CheGueVerra
I'm trying to build just a specific project within Foo.sln.
See my answer below.
A: 

Using CheGueVerra's template, I came up with the following solution:

<PropertyGroup>
    <ProjBuildCmd Condition="'$(BuildCmd)' != 'Build'">:$(BuildCmd)</ProjBuildCmd>
    <SolnBuildCmd Condition="'$(BuildCmd)' != 'Build'">$(BuildCmd)</SolnBuildCmd>
</PropertyGroup>

And then instead of using $(BuildCmd) directly, I use $(ProjBuildCmd) or $(SolnBuildCmd) like this:

<!-- Build the 'bar' project only -->
<Target Name="Baz">
  <MSBuild Projects="Foo.sln" Targets="bar$(ProjBuildCmd)" />
</Target>

<!-- Build the whole solution -->
<Target Name="Baz2">
  <MSBuild Projects="Foo.sln" Targets="$(SolnBuildCmd)" />
</Target>