【问题标题】:ProjectReference is not copied to output as part of parent NuGet packageProjectReference 未作为父 NuGet 包的一部分复制到输出
【发布时间】:2022-01-21 17:50:45
【问题描述】:

我有 2 个 csproj 项目:Child.csproj 和 Parent.csproj。 Child.csproj 通过 ProjectReference 被 Parent.csproj 引用:

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

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\Child.csproj" />
  </ItemGroup>

</Project>

子项目也可以作为 x86 dll 或 x64 dll 提供。它被打包为 NuGet 包以及父项目。为了打包 2 个不同版本的子 dll,使用了下一个道具和目标文件:

Child.props

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <Reference Include="Child" Condition="'$(Platform)' == 'x86'">
          <HintPath>$(MSBuildThisFileDirectory)..\lib\x86\Child.dll</HintPath>
        </Reference>
        <Reference Include="Child" Condition="'$(Platform)' == 'x64'">
          <HintPath>$(MSBuildThisFileDirectory)..\lib\x64\Child.dll</HintPath>
        </Reference>
      </ItemGroup>
    </Project>

子目标

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="PlatformCheck" BeforeTargets="BuildOnlySettings"
    Condition="(('$(Platform)' != 'x86') AND  ('$(Platform)' != 'x64'))">
    <Error  Text="$(MSBuildThisFileName) does not work correctly on '$(Platform)' platform. You need to specify platform (x86 or x64)." />
  </Target>
</Project>

此外,子项目是使用下一个 nuspec 构建的:

<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
    <metadata>
        <id>Child</id>
    </metadata> 

    <files>
        <file src="bin\x64\Release\netstandard2.0\Child.dll" target="lib\x64" />
        <file src="bin\x86\Release\netstandard2.0\Child.dll" target="lib\x86" />
        <file src="build\Child.props" target="build" />
        <file src="build\Child.targets" target="build" />
    </files>
</package>

这两个项目都被打包为 NuGet 包。当我直接引用子包时,它的 dll 被复制到输出文件夹。但是,当我引用 Parent 包时,我希望 Child.dll 与 Parent.dll 一起复制到项目的输出文件夹中,该文件夹引用 Parent 但它是 (Child.dll) 未复制。

【问题讨论】:

    标签: c# .net-core msbuild nuget nuget-spec


    【解决方案1】:

    您还需要在您的 nuspec 文件 https://docs.microsoft.com/en-us/nuget/reference/nuspec#dependencies-element 中指定您的依赖项

    或者,根本不要使用 nuspec 并在项目文件中的一个位置定义属性 - 这是现在发布 NuGet 包的推荐方式https://docs.microsoft.com/en-us/nuget/reference/msbuild-targets#pack-target。通过此设置,您的 csproj 中有项目引用这一事实足以将依赖项正确添加到您的包中

    【讨论】:

      【解决方案2】:

      传递依赖通常在构建应用程序/可执行项目之前不会得到解决 - 因为在那之前:没有足够的信息来知道哪个 要包含的实际 dll。如果您在构建库项目(引用 Parent)时看到这种情况,那么:这是正常的和预期的,并且不代表任何问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-01
        • 2021-01-06
        • 2015-01-02
        • 1970-01-01
        • 1970-01-01
        • 2021-12-31
        • 1970-01-01
        • 2020-06-22
        相关资源
        最近更新 更多