【问题标题】:Nuget Conflicting projectreference and packagereferenceNuget冲突项目引用和包引用
【发布时间】:2023-04-05 02:04:01
【问题描述】:

我正在 .NET 4.6.1 框架上运行 ASP.NET Core 应用程序。我有 1 个解决方案,其中包含多个项目。所有项目都是类库,它们通过 .csproj 中的 PackageReferences 相互引用(这样我们可以独立地构建、打包和版本化它们)。但是,我希望能够测试它们彼此的集成,而无需先将它们推送到 NuGet - 也就是我想在解决方案中将它们用作 ProjectReferences,但在通过我的 Jenkins 构建过程构建它们时使用 PackageReferences 以便版本组件分开。

当 .NET Core 是基于 project.json 时,这工作得很好。我会将版本设置在 project.json 的顶部,如果解决方案中存在具有该版本的项目,它将作为项目引用它,否则它将在我的 NuGet 提要中查找它。

使用 ProjectReferences 的问题在于,所有项目在构建并发送到 NuGet 时都会获得相同的版本。

有没有办法在 csproj 中做到这一点?有的话就找项目引用,否则看NuGet?

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <VersionPrefix>1.3.0</VersionPrefix>
    <TargetFramework>net461</TargetFramework>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
    <AssemblyName>MyProject1</AssemblyName>
    <PackageId>MyProject1</PackageId>
    <NetStandardImplicitPackageVersion>1.6.1</NetStandardImplicitPackageVersion>
    <GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
    <GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
    <GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="MyProject2" Version="1.4.0-*" />
    <PackageReference Include="Microsoft.AspNetCore.Http" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="1.1.1" />
  </ItemGroup>

  <ItemGroup Condition=" '$(TargetFramework)' == 'net461' ">
    <Reference Include="System" />
    <Reference Include="Microsoft.CSharp" />
  </ItemGroup>

</Project>

以上是一个示例,如果解决方案中存在 1.4.0,我希望 MyProject2 被 ProjectReference 引用。

【问题讨论】:

  • 你找到答案了吗?我也遇到了这个。我有两个独立的 nuget 库。在其之上的核心库和验证库,可以独立版本,但在一个存储库和构建/测试过程中管理它们。

标签: c# .net asp.net-core nuget csproj


【解决方案1】:

我最近尝试做同样的事情,但找不到答案,但找到了适合我的方法。您可以在 MSBuild 中使用 Exists 条件,以便 csproj 在项目引用存在时包含项目引用,如果存在则排除包引用:

<Project Sdk="Microsoft.NET.Sdk">
   ...
   <ItemGroup>
     <PackageReference Condition="!Exists('[path-to-project].csproj')" Include="[package-id]" Version="[pacakage-version].*" />
   </ItemGroup>
   ...
   <ItemGroup>
     <ProjectReference Condition="Exists('[path-to-project].csproj')" Include="[path-to-project].csproj" />
   </ItemGroup>
   ...
</Project>

【讨论】:

    猜你喜欢
    • 2013-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-26
    相关资源
    最近更新 更多