【问题标题】:Conditionally include project based on path有条件地包含基于路径的项目
【发布时间】:2021-08-01 12:37:42
【问题描述】:

我有这个:

MyProject/
  Common/
    Common.csproj
  Main/
    Libs/
      Utils/
        Utils.csproj
      Legacy/
        Legacy.csproj
    Server/
      Server.csproj
    Directory.Build.props

我希望Main/ 中的所有项目都包含Common 项目。

所以Directory.Build.props 有这个:

<ItemGroup>
  <ProjectReference Include="../../Common/Common.csproj" />
</ItemGroup>

所以Server 项目包括Common 项目,因为它向下两层。但是Libs/中的项目不能包含它,因为它向下三层。

我需要这样做:

<ItemGroup>
  <ProjectReference Include="../../Common/Common.csproj" Condition="if 2 levels up is 'MyProject'"/>
  <ProjectReference Include="../../../Common/Common.csproj" Condition="if 3 levels up is 'MyProject'"/>
</ItemGroup>

我在Condition 中输入了什么?我知道我需要使用MSBuildThisFileDirectory,但不确定如何使用。

【问题讨论】:

  • 为什么不使用解决方案目录作为基本路径,而不是使用相对路径?
  • @CamiloTerevinto 我正在使用 dotnet core 和 vscode。没有解决方案目录 - 即没有 MyProject.sln 文件。只是一个包含项目目录的目录。

标签: .net msbuild


【解决方案1】:
<ItemGroup>
  <ProjectReference Include="../../Common/Common.csproj" Condition="Exists('../../Common')'"/>
  <ProjectReference Include="../../../Common/Common.csproj" Condition="Exists('../../../Common')'"/>
</ItemGroup>

【讨论】:

    猜你喜欢
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 2016-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多