【问题标题】:Package third party dll in context of multi target framework project, .Net Framework & .Net Core在多目标框架项目、.Net Framework 和 .Net Core 的上下文中打包第三方 dll
【发布时间】:2020-08-15 17:26:55
【问题描述】:

我在 csproj 中使用下一个配置:

  <ItemGroup Condition="$(TargetFramework.StartsWith('net4'))">
    <Reference Include="amqmdnet">
      <HintPath>..\bin\amqmdnet.dll</HintPath>
    </Reference>
    <Content Include="..\bin\amqmdnet.dll">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      <Pack>true</Pack>
      <PackagePath>lib\$(TargetFramework)</PackagePath>
    </Content>
  </ItemGroup>

  <ItemGroup Condition="$(TargetFramework.StartsWith('netstandard2')) or $(TargetFramework.StartsWith('netcoreapp3'))">
    <Reference Include="amqmdnetstd">
      <HintPath>..\bin\amqmdnetstd.dll</HintPath>
    </Reference>
    <Content Include="..\bin\amqmdnetstd.dll">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      <Pack>true</Pack>
      <PackagePath>lib\$(TargetFramework)</PackagePath>
    </Content>
  </ItemGroup>

当项目使用具体框架设置时,它的每个部分都可以正常工作,例如:

    <TargetFramework>netcoreapp3.1</TargetFramework>

但是当项目是多框架注意到发生时,dll不包括在内

    <TargetFrameworks>netcoreapp3.1;net451</TargetFrameworks>

我在多框架场景中收到此消息,这是唯一的上下文:


我是如何进行的

由于某种原因nuget spec 没有填充元数据;我用 nuget 版本 5.5.1.6542 测试它。

  1. 我用VS建项目,怎么样。
  2. 将 {项目文件夹}\bin\Release\xxx.nupkg 的扩展名更改为 .zip
  3. 从 xxx.zip 中提取 xxx.nuspec 文件。它还将包含依赖项元数据。
  4. 使用NuGet Package Explorer 编辑xxx.nuspec
  5. 构建nuget xxx.nuspec
  6. .nuspec documentation

回答

看到这个link。 NuGet doc

我会把它贴在这里:

  <PropertyGroup>

<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);GetMyPackageFiles</TargetsForTfmSpecificBuildOutput>
  </PropertyGroup>


  <Target Name="GetMyPackageFiles">
    <ItemGroup Condition="$(TargetFramework.StartsWith('net4'))">
      <BuildOutputInPackage Include="amqmdnet.dll">
        <FinalOutputPath>..\bin\amqmdnet.dll</FinalOutputPath>
      </BuildOutputInPackage>
    </ItemGroup>
    <ItemGroup Condition="$(TargetFramework.StartsWith('netstandard2')) or $(TargetFramework.StartsWith('netcoreapp3'))">
      <BuildOutputInPackage Include="amqmdnetstd.dll">
        <FinalOutputPath>..\bin\amqmdnetstd.dll</FinalOutputPath>
      </BuildOutputInPackage>
    </ItemGroup>
  </Target>

【问题讨论】:

    标签: visual-studio msbuild nuget visual-studio-2019


    【解决方案1】:

    在多目标框架项目的上下文中打包第三方dll, .Net 框架和 .Net 核心

    我尝试了您的示例,但我也遇到了同样的问题。当我使用TargetFrameworks 将此类 dll 设置为多目标框架项目并面临相同的情况时。

    由于您的情况,&lt;pack&gt;true&lt;/pack&gt; 不起作用。但是当我查看此类项目中的输出文件夹时,&lt;CopyToOutputDirectory&gt;Always&lt;/CopyToOutputDirectory&gt; 有效。并根据条件,将这两种dll复制到对应的目标框架文件夹中。

    但是,pack不起作用,还是比较奇怪。

    所以我将此问题报告给我们的 DC 论坛。见this link。如果我没有详细描述这个问题,你可以投票这个问题并添加任何 cmets。任何对这个问题感兴趣的人都会投票给你,以便得到工作人员的更多关注。

    建议

    作为一个建议,你可以use nuspec file with nuget.exe cli打包你的项目,我已经测试成功了。

    1)从此链接下载nuget.exe并将其路径配置到系统环境变量PATH中。

    2) 调用 VS 或 CMD 的开发人员命令提示符,然后 cd 你的项目路径(xxx.csproj 存在)

    然后调用nuget spec获取xxx.nuspec文件

    3)打开xxx.nuspec文件并修改如下:

    <?xml version="1.0" encoding="utf-8"?>
    <package >
      <metadata>
        ......
      </metadata>
    
    <files>
    <file src="bin\Debug\net451\amqmdnet.dll" target="lib\net451" />
    <file src="bin\Debug\net451\PRCB.IBM.MQ.dll" target="lib\net451"/>
    <file src="bin\Debug\netcoreapp3.1\amqmdnetstd.dll" target="lib\netcoreapp3.1" />
    <file src="bin\Debug\netcoreapp3.1\PRCB.IBM.MQ.dll" target="lib\netcoreapp3.1" />
    </files>
    
    </package>
    

    4) 最后,输入nuget pack xxx.nuspec,您将获得xxx.nupkg 文件。

    【讨论】:

    • 谢谢,我打算在github.com/microsoft/msbuild提出问题
    • 好像多项目框架项目打包的时候,条件内容和pack节点冲突,我去掉条件后,又一切正常了。至此,您可以尝试nuget.exe打包项目。它可以避免您当前的问题。
    • 如果您有更好的解决方案,可以在这里与我们分享答案。如果我的解决方案确实可以满足您的要求,您可以考虑接受它,因为问题可能需要一段时间才能解决,以便营造良好的氛围。谢谢:)
    猜你喜欢
    • 2021-04-01
    • 2021-03-02
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    • 2017-08-15
    相关资源
    最近更新 更多