4
votes

I'm attempting to setup a .csproj file to have a conditional item group which will remove all of the elements in the <ProjectReference> item group.

For example:

<ItemGroup>
   <ProjectReference Include="..\..\..\..\Projects\Registrar\Ucsb.Sa.Registrar.Common\Ucsb.Sa.Registrar.Common\Ucsb.Sa.Registrar.Common.csproj">
      <Project>{1EDDDE57-0181-41B4-B2AE-FB76450F85C8}</Project>
      <Name>Ucsb.Sa.Registrar.Common</Name>
   </ProjectReference>
</ItemGroup>
<ItemGroup Condition="$(OnBuildServer) == 'true'">
   <ProjectReference Remove="*" />
</ItemGroup>
<ItemGroup Condition="$(OnBuildServer) == 'true'">
   <Reference Include="Ucsb.Sa.Registrar.Common">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>$(RegCommonDll)</HintPath>
   </Reference>
</ItemGroup>

But, when I load the project into VS 2008, I get the error message 'The attribute "Remove" in element <ProjectReference> is unrecognized". The odd thing is that the Remove attribute is in the schema (C:\Program Files\Microsoft Visual Studio 9.0\Xml\Schemas\1033\MSBuild\Microsoft.Build.Core.xsd). There is MSDN documentation on it (http://msdn.microsoft.com/en-us/library/bb651786.aspx). And, there is a comment about it at the bottom of the MSDN article titled "MSBuild Items".

The .csproj file seems to be pointing to .NET 3.5; but I am unable to verify if that version of msbuild is being used to load the project (does anyone know how to do that?)

First line of .csproj file:

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

PS. I got the idea to use the conditionals from Build with msbuild and dynamically set project references

1
I also tried changing the registry keys to default to 3.5. These keys don't configure Visual Studio, but instead configure the default values when creating a new project (msdn.microsoft.com/en-us/library/bb397428.aspx, \HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\3.5\, \HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions\2.0). That, of course, didn't do the trick. I also went through the PATH Environment Variable and replaced all references to the 2.0 directory with 3.5. Again, this didn't produce a solution.smaglio81

1 Answers

11
votes

You cannot use the Remove attribute with static Items. Static items are those declared outside of targets. You can only use this attribute inside of dynamic item declarations. Dynamic item declarations are those found inside of a target. For example take a look at the following build script.

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

  <ItemGroup>
    <ProjectReference  Include="One.dll"/>
  </ItemGroup>
  <Target Name="Demo">
    <ItemGroup>
      <ProjectReference  Remove="@(ProjectReference)"/>
    </ItemGroup>
    <Message Text="ProjectReference : @(ProjectReference)"/>
  </Target>
</Project>

Also note that you should not use Remove="*" that will not remove everything. It will remove every file in the current directory which is contained in the ProjectReference item group. If you want to clear out an item you have to do Remove="@(ProjectReference)" where ProjectReference is the item.