67

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andrew Lubochkn
  • 916
  • 2
  • 7
  • 16

5 Answers5

99

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
Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
  • 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. – Andrew Lubochkn Mar 20 '12 at 11:08
  • 12
    Select `Release` as solution configuration, set all project configurations to `Release` and uncheck `Build` for `Tdi.Origami.IndexUpdaterServiceSetup` – The Chairman Mar 20 '12 at 18:00
  • 2
    Yep, it works for me too. This should be marked as answer actually. – Tamas Ionut Mar 14 '14 at 09:21
  • 1
    This worked for me, but I had to specify the Platform property on the MSBuild command line. – yoyo Apr 21 '14 at 18:34
  • 14
    Is there any way to exclude a project from build at command line? – Bat_Programmer Jun 16 '17 at 05:28
  • 1
    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. – wondra Oct 24 '17 at 07:43
  • 8
    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? – Hakanai May 05 '18 at 03:15
  • 14
    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. – Tam Bui May 29 '19 at 05:17
  • 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. – Brad Lawrence Aug 01 '22 at 21:43
24

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
seva titov
  • 11,720
  • 2
  • 35
  • 54
  • 4
    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!! – Andrew Lubochkn Mar 21 '12 at 21:12
  • 2
    @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. – seva titov Jun 16 '16 at 20:26
16

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.

BmanMA
  • 161
  • 1
  • 4
6

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

STA
  • 30,729
  • 8
  • 45
  • 59
leolcao
  • 61
  • 1
  • 1
  • 1
    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 solution – Nick Turner Jul 07 '21 at 14:31
-4

create a master.proj file:

in another ItemGroup add DefaultExclude properties for programs - put it in front of the solution -- BA was Canadian
Configuration=Release Release drop the master.proj into the directory with the programs and msbuild the master.proj compiles everything except... that HelloWorld