.NET Core - 指定 ReferencePath 构建项目

9
我有一个针对 .NetCore 平台的 .csproj 文件,其中包含一些经典引用。在开发环境中,我使用 hintpath 属性。但是,在 CI 环境中应该构建 csproj,其中引用的程序集位于不同的目录中。 在经典的 net4 中,我使用了 MSBuild 工具的 /p:ReferencePath 参数。 但是“dotnet build”没有类似的参数。 作为备选方案,我找到了“dotnet msbuild”命令,但是该工具忽略了 /p:ReferencePath=xxx 参数并显示了警告 MSB3245:无法解析此引用。无法定位程序集“AssemblyName”。请确保该程序集存在于磁盘上。如果代码需要此引用,则可能会出现编译错误。 请指导我,在哪里可以检查 dotnet-build/dotnet-msbuild 工具搜索引用程序集以及如何指定该目录?
3个回答

7
问题是由Microsoft.NET.Sdk.props引起的:AssemblySearchPaths没有ReferencePath。 通过添加到csproj来解决:
<PropertyGroup>
    <AssemblySearchPaths>
        $(AssemblySearchPaths);
        $(ReferencePath);
    </AssemblySearchPaths>
</PropertyGroup>

你还有其他选项吗?我尝试了这个,但是没有帮助。 - testing

3
  1. 您仍然可以使用MSBUILD在解决方案中构建.NET CORE/Standard项目。
  2. 这似乎是一个bug,我向Microsoft报告了这个问题(并不是关于core/standard的问题,而是关于新的项目文件格式),referencePath被新的项目文件格式所忽略。
  3. 在msbuild命令中加入/t:restore以及构建目标,这样它将同时还原和构建。
  4. 您CI/Build服务器情况的解决方法是创建一个特殊的解决方案配置,并将类似下面的内容添加到您的项目文件中。
<Choose>  
  <When Condition="'$(Configuration)|$(Platform)'=='YourSpecialConfiguration|x64'"><!-- attention here -->
    <ItemGroup>
      <Reference Include="your.dllname">
        <HintPath>yourSpecialPath\your.dllname.dll</HintPath><!-- attention here -->
        <Private>true</Private>
      </Reference>
      <!-- more references here -->
  </When>
  <Otherwise>
    <ItemGroup>
      <Reference Include="your.dllname">
        <HintPath>yourRegularPath\your.dllname.dll</HintPath><!-- attention here -->
        <Private>true</Private>
      </Reference>
      <!-- AND more references here -->
  </Otherwise>
</Choose>  

这将使您能够在CI/Build中仅更改配置名称并完成工作。

1

但是 "dotnet build" 没有类似的参数。

你为什么这么说?

dotnet cli 仍然支持使用 -p 而不是 /p 进行 "属性注入"。链接(搜索 "-p")

对于你的问题,build 命令将会像这个命令一样:

dotnet build -p:ReferencePath=xxx


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接