How can I exclude a project from a build in MSBuild?
Asked Answered
O

4

70

I need to build a solution, but exclude one project. How should I do it?

I searched a lot about this issue, but nothing could help.

An ItemGroup section rises the following exception:

Invalid element . Unknown task or datatype.

PropertyGroup also rises the exception.

Below is my code sample:

<project name="TI 8.1.6 build script">
  <ItemGroup>
    <Solution Include="${ROOT}\Core\TI Core.sln" Exclude="${ROOT}\Utilities\DTS Indexing Service\Tdi.Origami.IndexUpdaterServiceSetup\Tdi.Origami.IndexUpdaterServiceSetup.wixproj"/>
  </ItemGroup>
...
</project>

How can I do this?

Ozieozkum answered 19/3, 2012 at 22:31 Comment(0)
C
100

You can exclude projects at the solution level for a specific build configuration by using the Configuration Manager Dialog in Visual Studio:

Configuration Manager Dialog

Then you can simply invoke msbuild on the solution file specifying the build configuration to use:

msbuild /property:Configuration=Release MySolution.sln
Castoff answered 19/3, 2012 at 22:46 Comment(9)
I set to the "Debug" mode to the project to be excluded. All another projects are set to "Release". In msbuild properties I set Configuration to Release. And exlcuded project is built with others and the exception is rised.Ozieozkum
Select Release as solution configuration, set all project configurations to Release and uncheck Build for Tdi.Origami.IndexUpdaterServiceSetupWisnicki
Yep, it works for me too. This should be marked as answer actually.Kellby
This worked for me, but I had to specify the Platform property on the MSBuild command line.Epiphysis
Is there any way to exclude a project from build at command line?Highspeed
Does not work for projects not currently loaded (e.g. unsupported) which which is big disadvantage since MSBuild will still attempt to build them and inevitably fail.Familiar
I'm trying to do this from the command-line too. Why do so many Windows devs assume that everyone is using the GUI to build?Dannydannye
I'm with all of the complaints here. The original question was via MSBUILD not via the GUI. So all of the upvotes on the wrong answers just makes me shake my head. Yes it works from the GUI, but if I need to automate this task in a Continuous Integration environment, MSBUILD appears to build all of the projects in the configuration, regardless of whether you checked the Build box in the GUI. If a flag exists in MSBUILD to comply with the Configuration Manager settings, I'd love to know what it is.Filia
I'm trying to get it working for CI with Azure DevOps as well, and it didn't work for me at first. The problem was my "Active Solution Platform" was set to "Mixed Platforms" but once I changed it to use "Any Cpu" which is what my CI build step was using, then it started to skip the projects like I needed it to. Just make sure the configuration and platform both match it exactly and you should be good.Hydrophilic
S
25

The solution suggested by Enrico is the most versatile solution that would work always. An alternative solution might be to use a <MSBuild> task directly. This will work for you if you have all your project files under a particular directory, or be able to easily enumerate all projects you want to build (i.e. number of projects in your solution is not very big).

For example, this MSBuild file will build every project under your current directory except for a specific project:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <ItemGroup>
    <MyProjectReferences Include="**\*.*proj" />
    <MyProjectReferences Exclude="Utilities\DTS Indexing Service\Tdi.Origami.IndexUpdaterServiceSetup\Tdi.Origami.IndexUpdaterServiceSetup.wixproj" />
  </ItemGroup>

  <Target Name="BuildAllExceptWixProject">
    <MSBuild Projects="@(MyProjectReferences)" Targets="Build" />
  </Target>

</Project>

Then you can build that using command line msbuild <myproject> /t:BuildAllExceptWixProject

Seraphina answered 21/3, 2012 at 4:27 Comment(2)
You was right. But not ProjectReferences but ProjectReference. And Include is necessary property for ProjectReference item, so I put Include/Exclude into the same item. And it works! The project was successfully excluded. Thank you!!Ozieozkum
@LubochknAndrew, the name of the group could be any valid XML element name. The ProjectReference group just one of the groups that is already used in standard Microsoft targets, that does not make this name any better than any other name you can think of. Notice, the example in my answer does not use standard Microsoft targets.Seraphina
D
18

In your solution file (.sln), remove the Build.0 entries. For example:

Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MyProject", "MyProject.vcxproj", "{2281D9E7-5261-433D-BB04-176A61500CA3}"
EndProject

GlobalSection(ProjectConfigurationPlatforms) = postSolution
    {2281D9E7-5261-433D-BB04-176A61500CA3}.Debug|x86.Build.0 = Debug|x64

If you delete this "Build.0" entry, it will load in the solution fine, but will not be built, either through the GUI or via external MSBuild.

Deloisedelong answered 17/5, 2019 at 17:27 Comment(0)
M
5

Since VS 2019 and MSBuild 16.7, the right way is to use Solution filters. Ref

Macklin answered 1/7, 2021 at 3:21 Comment(2)
I don;t think that's a good solution because that would filter the devs and they would start mucking up the file. build.targets.prop seems like a better solutionDespumate
The answer here is "use Solution filters." The link just tells you about them...Mooring

© 2022 - 2024 — McMap. All rights reserved.