Nuget Conflicting projectreference and packagereference

时间:2017-04-17 15:17:01

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

我在.NET 4.6.1框架上运行ASP.NET Core应用程序。我有一个包含多个项目的解决方案。所有项目都是通过其.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,我希望ProjectReference引用MyProject2。

1 个答案:

答案 0 :(得分:2)

我最近尝试做同样的事情,找不到答案,但想出了一些对我有用的东西。您可以在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>