Targeting a Project in a Solution Folder in MSBuild

Normally, when you invoke the MSBuild task to build a solution file, you can just add TARGETS=”ProjectName”, where ProjectName is just the name of the project, and don’t include the .csproj extension.

I already knew that if your project name includes a period, you need to replace that with an underscore (so the project “MySite.Web” becomes “MySite_Web”).

But the UnitTests project kept coming up as not a known build target name.

Finally, I found my answer in a comment in the answer to “specify project file of a solution using msbuild” on Stack Overflow.

Turns out that when a project appears in a solution folder (as opposed to just being in a directory), you need to include the name of the solution folder, and a backslash. So since the UnitTests project is in the UnitTests solution folder, the MSBuild invocation ends up looking like this:

<MSBuild
    Projects="$(SourceLocation)\$(SolutionName)"
    Properties="Configuration=Release; Platform=Any CPU; OutDir=$(OutputFolder)Assemblies\; WarningLevel=0;" 
    Targets="UnitTests\UnitTests" />