简体   繁体   English

Nuget Conflicting projectreference和packagereference

[英]Nuget Conflicting projectreference and packagereference

I'm running a ASP.NET Core app on the .NET 4.6.1 framework. 我正在.NET 4.6.1框架上运行ASP.NET Core应用程序。 I have 1 solution with multiple projects in it. 我有一个包含多个项目的解决方案。 All of the projects are class libraries that reference each other via PackageReferences in their .csproj (this way we can build, package and version them independently). 所有项目都是通过其.csproj中的PackageReferences相互引用的类库(这样我们就可以独立地构建,打包和版本化它们)。 However, I want to be able to test their integration with one another without needing to push them up to NuGet first - aka I want to use them as ProjectReferences in the solution, but PackageReferences when building them through my Jenkins build process in order to version the components separately. 但是,我希望能够在不需要首先将它们推送到NuGet的情况下测试它们之间的集成 - 我想在解决方案中使用它们作为ProjectReferences,但是在通过我的Jenkins构建过程构建它们时使用PackageReferences以便版本这些组件分开。

When .NET Core was project.json based, this worked fine. 当.NET Core基于project.json时,这很好用。 I would set the version at the top of the project.json and if a project existed with that version in the solution it would reference it as a project, otherwise it would look for it on my NuGet feed. 我将版本设置在project.json的顶部,如果在解决方案中存在该版本的项目,它会将其作为项目引用,否则它将在我的NuGet源上查找它。

The problem with using ProjectReferences is that all project's would get the same version when they are built and sent to NuGet. 使用ProjectReferences的问题在于,所有项目在构建并发送到NuGet时都会获得相同的版本。

Is there any way to do this in csproj? 在csproj中有没有办法做到这一点? Look for a project reference if it exists, otherwise look at NuGet? 查找项目引用(如果存在),否则查看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>

Above is an example, I would like MyProject2 to be referenced by ProjectReference if 1.4.0 exists in the solution. 上面是一个例子,如果解决方案中存在1.4.0,我希望ProjectReference引用MyProject2。

I recently was try to do the same thing and couldn't find the answer but figured out something that works for me. 我最近尝试做同样的事情,找不到答案,但想出了一些对我有用的东西。 You can use the Exists condition in MSBuild for the csproj to include the project reference when its there and exclude the package reference if its there: 您可以使用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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM