从命令行删除项目引用

时间:2014-12-03 06:59:16

标签: visual-studio msbuild

是否可以从命令行中删除项目引用?如果是这样,怎么样?想象一下,我在解决方案中有两个项目:WPF项目A和类库B.A有一个项目引用B,因此它将取决于项目B的输出。现在我不想删除项目引用命令行,以便能够在我们的构建中自动化。看作.csproj文件,项目引用看起来像这样。

<ProjectReference Include="..\B\B.csproj">
  <Project>{7B68745C-382E-4272-897D-123A0AD80391}</Project>
  <Name>B</Name>
</ProjectReference>

1 个答案:

答案 0 :(得分:1)

ProjectReference是一个项目,您可以在有条件的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>

从命令行可以传递:

  

MsBuild SomeProject.proj / p:ExcludeReference = true

更新:

您可以在单独的项目中使用可选参考并导入它:

<强> 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>

在你的.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>