问题描述
我一直在尝试将已安装的 7ZipCLI NuGet 包中的 7za.exe 文件包含到一个项目中,我想在其中创建一个 NuGet 包。 NuGet 包面向 .NET Standard2.0 和 .NET Framework v4.6.1+。我正在使用在 Build 上创建 NuGet 包的 Net Standard 功能。
字体完美复制到 NuGet 包,但 .exe 不想复制。
有人可以帮我把 7za.exe 放在包的内容文件夹中吗?它不需要是内容文件夹,我只想在安装 NuGet 包时将其复制到消费项目的输出目录。
这是 .csproj 文件:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.1.13</Version>
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);IncludeAssemblyReferences</TargetsForTfmSpecificBuildOutput>
<Platforms>x86</Platforms>
<PackageId>DocumentGeneration</PackageId>
<Authors>Me</Authors>
<Company>Me</Company>
<Product>DocumentGeneration</Product>
<Description>A library for creating PDF documents.</Description>
<PackageTags>Report Certificate Generator iText PDF</PackageTags>
<AssemblyVersion>1.0.0</AssemblyVersion>
<FiLeversion>1.0.0.0</FiLeversion>
<copyLocalLockFileAssemblies>true</copyLocalLockFileAssemblies>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'">
<OutputPath>$(SolutionDir)Output\</OutputPath>
<DocumentationFile>$(SolutionDir)Output\DocumentGeneration.xml</DocumentationFile>
<GenerateResourceUsePreserializedResources>true</GenerateResourceUsePreserializedResources>
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x86'">
<OutputPath>$(SolutionDir)Output\</OutputPath>
<DocumentationFile>$(SolutionDir)Output\DocumentGeneration.xml</DocumentationFile>
<GenerateResourceUsePreserializedResources>true</GenerateResourceUsePreserializedResources>
<DebugType>embedded</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
<ItemGroup>
<Content Include="Resources\Arial.ttf">
<copyToOutputDirectory>Always</copyToOutputDirectory>
<PackagecopyToOutput>true</PackagecopyToOutput>
<copyToPublishDirectory>true</copyToPublishDirectory>
</Content>
<Content Include="Resources\HelveticaNarrow.otf">
<copyToOutputDirectory>Always</copyToOutputDirectory>
<PackagecopyToOutput>true</PackagecopyToOutput>
<copyToPublishDirectory>true</copyToPublishDirectory>
</Content>
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net461'">
<packagereference Include="iTextSharp" Version="5.5.13.2" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">
<packagereference Include="itextsharp.netstandard" Version="5.5.13.2" GeneratePathProperty="true" />
<packagereference Include="System.Drawing.Common" Version="5.0.0" GeneratePathProperty="true" />
<packagereference Include="System.Resources.Extensions" Version="5.0.0" />
<packagereference Include="System.Resources.ResourceManager" Version="4.3.0" />
</ItemGroup>
<ItemGroup>
<packagereference Include="7ZipCLI" Version="9.20.0" GeneratePathProperty="true" IncludeAssets="all" />
<packagereference Include="System.Text.Encoding" Version="4.3.0" />
<packagereference Include="System.Text.Encoding.CodePages" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
<None Include="readme.txt" pack="true" PackagePath="." />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<None Include="$(Pkgitextsharp_netstandard)\lib\netstandard2.0\itextsharp.netstandard.dll">
<copyToOutputDirectory>Always</copyToOutputDirectory>
<PackagecopyToOutput>true</PackagecopyToOutput>
<Visible>false</Visible>
</None>
<Content Include="$(Pkg7ZipCLI)\tools\7za.exe">
<copyToOutputDirectory>Always</copyToOutputDirectory>
<PackagecopyToOutput>true</PackagecopyToOutput>
<PackagePath>content\Resources;contentFiles\any\any\Resources</PackagePath>
<Visible>false</Visible>
</Content>
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net461'">
<Content Include="$(Pkg7ZipCLI)\tools\7za.exe">
<copyToOutputDirectory>Always</copyToOutputDirectory>
<PackagecopyToOutput>true</PackagecopyToOutput>
<PackagePath>content\Resources;contentFiles\any\any\Resources</PackagePath>
<Visible>false</Visible>
</Content>
</ItemGroup>
<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx">
<Generator>PublicResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<Target Name="IncludeAssemblyReferences">
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<BuildOutputInPackage Include="$(Pkgitextsharp_netstandard)\lib\netstandard2.0\itextsharp.netstandard.dll">
<Visible>false</Visible>
</BuildOutputInPackage>
</ItemGroup>
</Target>
</Project>
还有这里是 NuGet 包目前的样子:
解决方法
如果你只是想把文件输出到packages.config
下的bin文件夹,你应该使用<packages_id>.props
文件,<PackageCopyToOutput>true</PackageCopyToOutput>
只用于PackageReference .
另外,<CopyToOutputDirectory>Always</CopyToOutputDirectory>
仅用于本地项目下的本地文件,而不是 nuget 包。它不适用于 nuget 包。
解决方案
1) 在 build 文件夹下添加一个名为 DocumentGeneration.props
(DocumentGeneration
) 的文件
2) 修改您的 DocumentGeneration.csproj
文件:
.....
<Content Include="$(Pkg7ZipCLI)\tools\7za.exe">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<PackageCopyToOutput>true</PackageCopyToOutput>
<PackagePath>content\Resources;contentFiles\any\any\Resources</PackagePath>
<Visible>false</Visible>
</Content>
<None Include="build\*.*">
<Pack>true</Pack>
<PackagePath>build</PackagePath>
</None>
.....
3) 将这些添加到 DocumentGeneration.props
文件中:
<Project>
<ItemGroup>
<None Include="$(ProjectDir)Resources\*.*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
或
<Project>
<None Include="$(MSBuildThisFileDirectory)..\content\**\*.*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>Resources\%(FileName)%(Extension)</Link>
</None>
</Project>
4) 重新打包您的 lib 项目。安装此新发布版本时,您应先clean nuget caches 或删除 C:\Users\xxx(current user)\.nuget\packages
下的所有缓存文件。
除此之外,还有a similar issue。
更新
我认为这是多目标框架功能的问题。如果您对 pkg 使用 GeneratePathProperty
,则该属性将在其他目标框架上丢失。这很奇怪,当您更改为使用 <TargetFramework>
时,效果很好。由于问题在 TargetFrameworks
下,GeneratePathProperty=true
。我对此没有其他好主意,也找不到解决它们的好方法。
您应该在 DC Forum 或 raise an issue on github 上报告。完成后,您可以在此处分享。
1) 另外,由于您使用多目标框架,您还应该使用 nupkg 上的 buildCrossTargeting
文件夹来启用道具文件。 See this similar issue。
2) 其次,当我用这些来区分它们时,nuget也不能打包。这太奇怪了,我必须删除那个条件。
<ItemGroup Condition="'$(TargetFramework)'=='net461'">
...
<None Include="7za.exe" Pack="true" PackagePath="content\Resources;contentFiles\any\any\Resources">
...
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='netstandard2.0'">
...
<None Include="7za.exe" Pack="true" PackagePath="content\Resources;contentFiles\any\any\Resources">
...
</ItemGroup>
我的想法是 content
和 contentFiles
节点指向整个项目,并且在将它们打包为内容时不能使用 targetframeworks
指定条件。我所知道的是,content
和 contentFiles
是针对整体的目标,不能针对 multi-targetframeworks 下的 targetframework
。
你应该使用这些:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
</PropertyGroup>
...
<ItemGroup>
<PackageReference Include="7ZipCLI" Version="9.20.0" GeneratePathProperty="true">
</PackageReference>
</ItemGroup>
...
<Target Name="FunCopy" BeforeTargets="PrePareForBuild">
<Copy SourceFiles="$(Pkg7ZipCLI)\tools\7za.exe" DestinationFolder="$(ProjectDir)"></Copy>
</Target>
<ItemGroup>
<None Include="$(ProjectDir)7za.exe" Pack="true" PackagePath="content\Resources;contentFiles\any\any\Resources">
<PackageCopyToOutput>true</PackageCopyToOutput>
<Visible>false</Visible>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<None Include="build\*.*" Pack="true" PackagePath="build;buildCrossTargeting"></None>
</ItemGroup>
...
</Project>