1

Is it possible to remove a project reference from the command line? And if so, how? Imagine I have two projects in a solution: WPF project A, and Class Library B. A has a project reference to B so that it will depend on the output of project B. Now I wan't to remove the project reference from the command line as to be able to automate in our build. Looking as the .csproj file the project reference looks something like this.

<ProjectReference Include="..\B\B.csproj">
  <Project>{7B68745C-382E-4272-897D-123A0AD80391}</Project>
  <Name>B</Name>
</ProjectReference>
AndrewRalon
  • 496
  • 1
  • 9
  • 24
vidstige
  • 12,492
  • 9
  • 66
  • 110

1 Answers1

3

ProjectReference is an item, you can add the Item to be conditionally excluded inside a conditioned ItemGroup:

<PropertyGroup>
    <ExcludeReference Condition="'$(ExcludeReference)'==''">false</ExcludeReference>
</PropertyGroup>
<ItemGroup Condition="'$(ExcludeReference)'=='true'">
    <ProjectReference Include="..\B\B.csproj">
        <Project>{7B68745C-382E-4272-897D-123A0AD80391}</Project>
        <Name>B</Name>
    </ProjectReference>
</ItemGroup>

From the command line you can pass:

MsBuild SomeProject.proj /p:ExcludeReference=true

UPDATE:

You can have your optional reference in a separate project and import it:

ConditionalReferences.proj

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
        <ProjectReference Include="..\B\B.csproj">
            <Project>{7B68745C-382E-4272-897D-123A0AD80391}</Project>
            <Name>B</Name>
        </ProjectReference>
    </ItemGroup>
</Project>

And in your .csproj

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

    <PropertyGroup>
        <ExcludeReference Condition="'$(ExcludeReference)'==''">false</ExcludeReference>
    </PropertyGroup>

    <Import Project="ConditionalReferences.proj" Condition="'$(ExcludeReference)'=='false'"/>
</Project>
Rolo
  • 3,208
  • 1
  • 24
  • 25
  • Thanks I didn't know this. Should be helpful when trying to create an desktop app that builds on both x86 and 64. However in this case I really want to remove the reference as we're packaging up a sample project in our solution for further distribution. – vidstige Dec 09 '14 at 06:29
  • @vidstige Why this doesn't work for you? It will remove the reference you mention. – Rolo Dec 09 '14 at 12:54
  • @vidstige If this is not the case you may be looking for something like this http://stackoverflow.com/questions/3788605/if-debug-vs-conditionaldebug Conditional Compilation or Conditional Atribute. – Rolo Dec 09 '14 at 12:56
  • The reference will still be there inside the csproj file, while I would like to remove it so that it is not inside the file. The reason is I'm sending the file to someone else. – vidstige Dec 09 '14 at 13:28
  • @vidstige please review my update. That will allow you to have some conditional configuration in a separate file so you don't need share that with someone else. – Rolo Dec 09 '14 at 14:04