问题
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.
回答1:
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.
回答2:
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
回答3:
The key is conditionalize the BuildCmd property.
<Target Name="Baz">
<PropertyGroup>
<BuildCmd Condition="'$(BuildCmd)' != ''">:$(BuildCmd)</BuildCmd>
</PropertyGroup>
<MSBuild Projects="Foo.sln" Targets="bar$(BuildCmd)" />
</Target>
This way, if Clean or Rebuild are set, the colon is added. If nothing is added, BuildCmd will just be blank, defaulting to the Build target.
Note - The property group must reside in the target, or it will be overriden when you specify it on the command line.
回答4:
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>
回答5:
Just edit the sln file yourself and find out - MSBuild is a real easy syntax, just look for targets.
来源:https://stackoverflow.com/questions/232052/msbuild-specifying-a-target-from-the-command-line