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> 

1 Answer 1

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> 
Sign up to request clarification or add additional context in comments.

5 Comments

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 Why this doesn't work for you? It will remove the reference you mention.
@vidstige If this is not the case you may be looking for something like this stackoverflow.com/questions/3788605/… Conditional Compilation or Conditional Atribute.
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 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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.