--- lang: ja-jp breaks: true --- # パッケージ化されていない外部参照ライブラリを NuGetパッケージに同梱して配布する 2022-06-30 > .NET Core, ローカルライブラリをNuGetでパッケージ化して、他のプロジェクトにインストールする方法 > https://www.aruse.net/entry/2018/09/09/191550 > Including referenced project DLLs in nuget package [.Net Core RC3 *.csproj file] > https://stackoverflow.com/questions/41979310/including-referenced-project-dlls-in-nuget-package-net-core-rc3-csproj-file > NuGet pack and restore as MSBuild targets > https://docs.microsoft.com/en-us/nuget/reference/msbuild-targets#targetsfortfmspecificcontentinpackage > NuGet ターゲットとして MSBuild パックおよび復元する > https://docs.microsoft.com/ja-jp/nuget/reference/msbuild-targets `*.csproj` ```xml= <PropertyGroup> <TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);NuGetPackageFiles</TargetsForTfmSpecificBuildOutput> </PropertyGroup> <Target Name="NuGetPackageFiles"> <ItemGroup> <BuildOutputInPackage Include="lib\ude\UtfUnknown.dll"> <TargetPath>lib\ude</TargetPath> </BuildOutputInPackage> </ItemGroup> </Target> ``` :::warning `<TargetPath></TargetPath>`を指定すると、出力フォルダではなくプロジェクトフォルダにリンクとして追加される。 以下のように指定すると、出力フォルダにdllがコピーされる。 ※ちょっと動作が良くわからない。。。 ```xml= <ItemGroup> <BuildOutputInPackage Include="lib\ude\UtfUnknown.dll" /> </ItemGroup> ``` ::: ## その他の方法 > 外部 DLL を NuGet パッケージに含める方法 > https://kuttsun.blogspot.com/2020/10/dll-nuget.html `csproj`に以下のように記述すると、出力ディレクトリに `lib\7z\**\*.dll` として出力される。 ```xml= <ItemGroup> <Content Include="lib\7z\**\*.dll"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <Pack>true</Pack> <PackageCopyToOutput>true</PackageCopyToOutput> </Content> </ItemGroup> ``` :::warning ```xml= <Content Include="lib\SqlServer.Types.14.0.1016.290\nativeBinaries\**\*.dll"> <Link>lib\SqlServerTypes\%(RecursiveDir)\%(Filename)%(Extension)</Link> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <Pack>true</Pack> <PackageCopyToOutput>true</PackageCopyToOutput> </Content> ``` 以上のようにしてリンクとしてコンテンツを読み込むと、出力フォルダにリンク通りに出力される。  しかし、NuGetで配布すると、リンクのパスではなくオリジナルのパスで出力されてしまった。  つかえない。。。 ::: ## 色々試した結果、`Content`タグで、`PackagePath`を使用するとうまくいく ### コンテンツとして出力フォルダにコピーする場合 ```xml= <Content Include="lib\SqlServer.Types.14.0.1016.290\nativeBinaries\**\*.dll"> <Link>lib\SqlServerTypes\%(RecursiveDir)\%(Filename)%(Extension)</Link> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <Pack>true</Pack> <PackageCopyToOutput>true</PackageCopyToOutput> <PackagePath>contentFiles\any\$(TargetFramework_NetFramework)\lib\SqlServerTypes</PackagePath> </Content> ``` ### 直接出力フォルダにコピーする場合 ```xml= <Content Include="lib\SqlServer.Types.14.0.1016.290\lib\net40\**\*.dll"> <Link>lib\SqlServerTypes\%(RecursiveDir)\%(Filename)%(Extension)</Link> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <Pack>true</Pack> <PackageCopyToOutput>true</PackageCopyToOutput> <PackagePath>lib\$(TargetFramework_NetFramework)</PackagePath> </Content> ``` :::warning 上記の中で、`$(TargetFramework)`は使用できないので注意が必要。 `<ItemGroup Condition="'$(TargetFramework)' == '$(TargetFramework_NetFramework)'">`のような条件を記載してもパッケージングされない。 ※`$(TargetPlatformVersion)`も使用できない。 ::: :::danger `TargetsForTfmSpecificBuildOutput`を使ったパッケージングも色々と試したが、複数のファイルをサブフォルダに配布しようとするとファイル一つしかパッケージングされなかった。 例えば、以下のように複数のファイルを`ja`フォルダにパックしようとしてもファイル一つのみがパックされていた。。。。 ※原因不明。。。 ```xml= <BuildOutputInPackage Include="$(OutputPath)ja\Microsoft.ReportViewer.Common.resources.dll"> <TargetPath>ja</TargetPath> </BuildOutputInPackage> <BuildOutputInPackage Include="$(OutputPath)ja\Microsoft.ReportViewer.DataVisualization.resources.dll"> <TargetPath>ja</TargetPath> </BuildOutputInPackage> <BuildOutputInPackage Include="$(OutputPath)ja\Microsoft.ReportViewer.WinForms.resources.dll"> <TargetPath>ja</TargetPath> </BuildOutputInPackage> ``` ::: ## 参考例 ```xml= <Content Include="lib\SqlServer.Types.14.0.1016.290\nativeBinaries\**\*.dll"> <Link>lib\SqlServerTypes\%(RecursiveDir)\%(Filename)%(Extension)</Link> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <Pack>true</Pack> <PackageCopyToOutput>true</PackageCopyToOutput> <PackagePath>contentFiles\any\$(TargetFramework_NetFramework)\lib\SqlServerTypes</PackagePath> </Content> <Content Include="lib\SqlServer.Types.14.0.1016.290\lib\net40\**\*.dll"> <Link>lib\SqlServerTypes\%(RecursiveDir)\%(Filename)%(Extension)</Link> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <Pack>true</Pack> <PackageCopyToOutput>true</PackageCopyToOutput> <PackagePath>lib\$(TargetFramework_NetFramework)</PackagePath> </Content> <Content Include="lib\ReportViewerControl.WebForms.150.900.148\lib\net40\**\*.dll"> <Link>lib\ReportViewerControl.WebForms\%(RecursiveDir)\%(Filename)%(Extension)</Link> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <Pack>true</Pack> <PackageCopyToOutput>true</PackageCopyToOutput> <PackagePath>lib\$(TargetFramework_NetFramework)</PackagePath> </Content> <Content Include="lib\ReportViewerControl.Winforms.150.900.148\lib\net40\**\*.dll"> <Link>lib\ReportViewerControl.Winforms\%(RecursiveDir)\%(Filename)%(Extension)</Link> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <Pack>true</Pack> <PackageCopyToOutput>true</PackageCopyToOutput> <PackagePath>lib\$(TargetFramework_NetFramework)</PackagePath> </Content> <Content Include="$(TargetProjectDir)lib\7z\**\*.dll"> <Link>lib\7z\%(RecursiveDir)\%(Filename)%(Extension)</Link> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <Pack>true</Pack> <PackageCopyToOutput>true</PackageCopyToOutput> <PackagePath>contentFiles\any\$(TargetFramework_NetFramework)\lib\7z;contentFiles\any\$(TargetFramework_NetCoreWindows)$(TargetPlatformVersion_NetCoreWindows)\lib\7z</PackagePath> </Content> ``` :::danger その後、上記`PackagePath`の指定箇所でパッケージ発行時にエラーが発生するようになった。現状未調査。 https://hackmd.io/ZC1kQIt3QNOW8Aa223-6hw ::: ```xml= <PropertyGroup> <PackageId>$(AssemblyName)</PackageId> <PackageDescription>$(MSBuildThisFileName)</PackageDescription> <PackageTags>$(PackageTags);COMM;Common;</PackageTags> <PackageVersion>$(PackageVersion_XXXXXXXX_Core)</PackageVersion> <TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);NuGetPackageFiles</TargetsForTfmSpecificBuildOutput> </PropertyGroup> <Target Name="NuGetPackageFiles"> <ItemGroup Condition="'$(TargetFramework)' == '$(TargetFramework_NetFramework)'"> <BuildOutputInPackage Include="$(OutputPath)FastEnum.dll" /> <BuildOutputInPackage Include="$(OutputPath)ThoughtWorks.QRCode.dll" /> </ItemGroup> <ItemGroup Condition="'$(TargetFramework)' == '$(TargetFramework_NetCore)'"> <BuildOutputInPackage Include="$(OutputPath)FastEnum.dll" /> <BuildOutputInPackage Include="$(OutputPath)ThoughtWorks.QRCode.dll" /> </ItemGroup> <ItemGroup> </ItemGroup> </Target> ``` :::info 以下のプロパティを事前に設定する。 * `$(TargetFramework_NetFramework)`:net48 * `$(TargetFramework_NetCoreWindows)`:net6.0-windows * `$(TargetFramework_NetCore)`:net6.0 * `$(TargetPlatformVersion_NetCoreWindows)`:7.0 ::: ###### tags: `NuGet` `外部参照ライブラリ` `TargetsForTfmSpecificBuildOutput` `Pack`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up