.net NuGet在一个包中针对x86、x64和AnyCPU

jxct1oxe  于 2023-04-22  发布在  .NET
关注(0)|答案(1)|浏览(144)

由于我在下面的问题上花了很多时间,我想与你分享我的结果:
NuGet允许针对很多不同的设置,但当涉及到一些高级的东西,它的文档不是很好.也有很多不同的东西浮动,但它都是一个大混乱.
所以我被这个困住了:如何在单个软件包中包含具有不同目标版本的x86、x64和Any CPU构建?

f87krz0w

f87krz0w1#

关于这一点,现有的SO答案有两种,但都缺乏一些细节。
这其实很简单。至少,如果你花了5个小时在这上面。
1.您的文件需要放置在build\{target framework version}\{architecture}
1.您需要添加一个.props文件来包含您的lib。该文件应包含以下内容:

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

确保将MyDll替换为您的包和.dll名称!*

它必须保存在每个build/{target framework version}文件夹中,并且必须使用名称MyDll.props。它的最终路径应该如下所示:build/net452/MyDll.props。您需要将其放置在每个build/{target framework version}目录中。需要这样做,以便NuGet不再令人讨厌,并让它包含我们的.props文件。遗憾的是,不可能在build/文件夹中只包含一次(理论上,是的!实际上,它不起作用)。
1.如果你想摆脱警告WARNUNG: NU5127: This package does not contain a lib/ or ref/ folder, and will therefore be treated as compatible for all frameworks. Since framework specific files were found under the build/ directory for net452, consider creating the following empty files to correctly narrow the compatibility of the package: -lib/net452/_._,你需要在每个lib\{target framework version}目录中添加一个名为_._的空文件。

最终结构:

您的组装NuGet包应该看起来像这样:

{root}/
  build/
    net452/
      MyDll.props
      AnyCPU/
        MyDll.dll
        MyDll.xml
        MyDll.pdb
      x86/
        MyDll.dll
        MyDll.xml
        MyDll.pdb
      x64/
        MyDll.dll
        MyDll.xml
        MyDll.pdb            
    netstandard2.1/
      MyDll.props
      AnyCPU/
        ...
      .../
  lib/
    net452/
      _._
    netstandard2.1/
      _._

示例.nuspec

<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
    <metadata minClientVersion="3.2">
        ...
    </metadata>
    <files>
        <!-- .NETFramework 4.5.2 -->
        <file src="LSDOTNETCORE.props" target="build\net452" />
        <file src="_._" target="lib\net452" />
    
        <file src="Build\AnyCPU\Release\net452\LSDOTNETCORE.dll" target="build\net452\AnyCPU" />
        <file src="Build\AnyCPU\Release\net452\LSDOTNETCORE.xml" target="build\net452\AnyCPU" />
        <file src="Build\AnyCPU\Release\net452\LSDOTNETCORE.pdb" target="build\net452\AnyCPU" />    
        
        <file src="Build\x86\Release\net452\LSDOTNETCORE.dll" target="build\net452\x86" />
        <file src="Build\x86\Release\net452\LSDOTNETCORE.xml" target="build\net452\x86" />
        <file src="Build\x86\Release\net452\LSDOTNETCORE.pdb" target="build\net452\x86" />
        
        <file src="Build\x64\Release\net452\LSDOTNETCORE.dll" target="build\net452\x64" />
        <file src="Build\x64\Release\net452\LSDOTNETCORE.xml" target="build\net452\x64" />
        <file src="Build\x64\Release\net452\LSDOTNETCORE.pdb" target="build\net452\x64" />  
        
        <!-- .NET-Standard -->
        <file src="LSDOTNETCORE.props" target="build\netstandard2.1" />
        <file src="_._" target="lib\netstandard2.1" />
    
        <file src="Build\AnyCPU\Release\netstandard2.1\LSDOTNETCORE.dll" target="build\netstandard2.1\AnyCPU" />
        <file src="Build\AnyCPU\Release\netstandard2.1\LSDOTNETCORE.xml" target="build\netstandard2.1\AnyCPU" />
        <file src="Build\AnyCPU\Release\netstandard2.1\LSDOTNETCORE.pdb" target="build\netstandard2.1\AnyCPU" />    
        
        <file src="Build\x86\Release\netstandard2.1\LSDOTNETCORE.dll" target="build\netstandard2.1\x86" />
        <file src="Build\x86\Release\netstandard2.1\LSDOTNETCORE.xml" target="build\netstandard2.1\x86" />
        <file src="Build\x86\Release\netstandard2.1\LSDOTNETCORE.pdb" target="build\netstandard2.1\x86" />
        
        <file src="Build\x64\Release\netstandard2.1\LSDOTNETCORE.dll" target="build\netstandard2.1\x64" />
        <file src="Build\x64\Release\netstandard2.1\LSDOTNETCORE.xml" target="build\netstandard2.1\x64" />
        <file src="Build\x64\Release\netstandard2.1\LSDOTNETCORE.pdb" target="build\netstandard2.1\x64" />      
    </files>
</package>

相关问题