使用.exe文件作为内容文件创建NuGet包 [英] Creating a NuGet package with .exe file as content file

查看:124
本文介绍了使用.exe文件作为内容文件创建NuGet包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图将已安装的7ZipCLI NuGet包中的7za.exe文件包含到一个项目中,我希望创建该项目.NuGet软件包同时面向.NET Standard2.0和.NET Framework v4.6.1 +.我正在使用Net Standard在Build上创建NuGet软件包的功能.

字体可以完美地复制到NuGet包中,但是.exe不想复制.

有人可以帮助我将7za.exe放在程序包的内容文件夹中吗?它不需要是内容文件夹,我只想将其复制到安装NuGet软件包时正在使用的项目的输出目录中.

这是.csproj文件:

 < Project Sdk ="Microsoft.NET.Sdk"< PropertyGroup>< TargetFrameworks> netstandard2.0; net461</TargetFrameworks>< GeneratePackageOnBuild> true</GeneratePackageOnBuild><版本> 1.1.13</版本>< TargetsForTfmSpecificBuildOutput> $(TargetsForTfmSpecificBuildOutput); IncludeAssemblyReferences</TargetsForTfmSpecificBuildOutput>< Platforms> x86</Platforms>< PackageId> DocumentGeneration</PackageId>< Authors> Me</Authors><公司>我</公司><产品>文档生成</产品><说明>用于创建PDF文档的库.< PackageTags>报告证书生成器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>完整</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>嵌入式</DebugType>< DebugSymbols> true</DebugSymbols></PropertyGroup>< ItemGroup>< Content Include ="Resources \ Arial.ttf">< CopyToOutputDirectory>始终</CopyToOutputDirectory>< PackageCopyToOutput> true</PackageCopyToOutput>< CopyToPublishDirectory> true</CopyToPublishDirectory></内容>< Content Include ="Resources \ HelveticaNarrow.otf">< CopyToOutputDirectory>始终</CopyToOutputDirectory>< PackageCopyToOutput> true</PackageCopyToOutput>< CopyToPublishDirectory> true</CopyToPublishDirectory></内容></ItemGroup>< ItemGroup Condition ='''''$(TargetFramework)'=='net461'''>< PackageReference Include ="iTextSharp"版本="5.5.13.2"./></ItemGroup>< ItemGroup Condition ="'$(TargetFramework)'=='netstandard2.0'''>< PackageReference Include ="itextsharp.netstandard";版本="5.5.13.2".GeneratePathProperty ="true";/>< PackageReference Include ="System.Drawing.Common";版本="5.0.0&"GeneratePathProperty ="true";/>< PackageReference Include ="System.Resources.Extensions";版本="5.0.0&"/>< PackageReference Include ="System.Resources.ResourceManager";版本="4.3.0&"/></ItemGroup>< ItemGroup>< PackageReference Include ="7ZipCLI"版本="9.20.0"GeneratePathProperty ="true";IncludeAssets =全部"./>< PackageReference Include ="System.Text.Encoding";版本="4.3.0&"/>< PackageReference Include ="System.Text.Encoding.CodePages"版本="5.0.0&"/></ItemGroup>< ItemGroup><无包含="readme.txt";pack ="true"PackagePath =."/></ItemGroup>< ItemGroup Condition ='''$(TargetFramework)'=='netstandard2.0'''><无包含="$(Pkgitextsharp_netstandard)\ lib \ netstandard2.0 \ itextsharp.netstandard.dll">< CopyToOutputDirectory>始终</CopyToOutputDirectory>< PackageCopyToOutput> true</PackageCopyToOutput>< Visible> false</Visible></无>< Content Include ="$(Pkg7ZipCLI)\ tools \ 7za.exe">< CopyToOutputDirectory>始终</CopyToOutputDirectory>< PackageCopyToOutput> true</PackageCopyToOutput>< PackagePath> content \ Resources; contentFiles \ any \ any \ Resources</PackagePath>< Visible> false</Visible></内容></ItemGroup>< ItemGroup Condition ='''''$(TargetFramework)'=='net461'''>< Content Include ="$(Pkg7ZipCLI)\ tools \ 7za.exe">< CopyToOutputDirectory>始终</CopyToOutputDirectory>< PackageCopyToOutput> true</PackageCopyToOutput>< PackagePath> content \ Resources; contentFiles \ any \ any \ Resources</PackagePath>< Visible> false</Visible></内容></ItemGroup>< ItemGroup><编译更新="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><目标名称="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>始终</CopyToOutputDirectory> 仅适用于本地项目下的本地文件,而不适用于nuget包.而且不适用于nuget程序包.

解决方案

1)下添加一个名为 DocumentGeneration.props (< packages_id> .props,而您的packageID为 DocumentGeneration )的文件构建文件夹

2)修改您的 DocumentGeneration.csproj 文件:

  .....< Content Include ="$(Pkg7ZipCLI)\ tools \ 7za.exe">< CopyToOutputDirectory>始终</CopyToOutputDirectory>< PackageCopyToOutput> true</PackageCopyToOutput>< PackagePath> content \ Resources; contentFiles \ any \ any \ Resources</PackagePath>< Visible> false</Visible></内容><无包含="build \ *.*">< Pack> true</Pack>< PackagePath> build</PackagePath></无>..... 

3)将它们添加到 DocumentGeneration.props 文件中:

 < Project>< ItemGroup><无包含="$(ProjectDir)Resources \ *.*"< CopyToOutputDirectory> PreserveNewest</CopyToOutputDirectory></无></ItemGroup></Project> 

 < Project><无包含="$(MSBuildThisFileDirectory).. \ content \ ** \ *.*"< CopyToOutputDirectory> PreserveNewest</CopyToOutputDirectory><链接>资源\%(文件名)%(扩展名)</链接></无></Project> 

4)重新打包您的lib项目.安装此新发行版本时,应首先

您应该在 DC论坛在github上引发了一个问题.完成后,您可以在这里分享.

1)另外,由于您使用多目标框架,因此还应该使用nupkg上的 buildCrossTargeting 文件夹来启用props文件.看到此类似问题.

2)其次,当我使用它们来区分它们时,nuget也无法打包它.这太奇怪了,我必须删除该条件.

 < ItemGroup Condition ='''$(TargetFramework)'=='net461'''>...<无包含="7za.exe";Pack ="true"PackagePath ="content \ Resources; contentFiles \ any \ any \ Resources">...</ItemGroup>< ItemGroup Condition =''''$(TargetFramework)'=='netstandard2.0''>...<无包含="7za.exe";Pack ="true"PackagePath ="content \ Resources; contentFiles \ any \ any \ Resources">...</ItemGroup> 

我的想法是 content contentFiles 节点以整个项目为目标,并且在将它们打包为内容时无法通过 targetframeworks 指定条件.我所知道的是 content contentFiles 是整体的目标,而不能在 multi-targetframeworks 下定位到 targetframework strong>.

您应该使用这些:

 < Project Sdk ="Microsoft.NET.Sdk"< PropertyGroup>< TargetFrameworks> netstandard2.0; net461</TargetFrameworks></PropertyGroup>...< ItemGroup>< PackageReference Include ="7ZipCLI"版本="9.20.0"GeneratePathProperty ="true"</PackageReference></ItemGroup>...<目标名称="FunCopy";BeforeTargets ="PrePareForBuild"<复制SourceFiles ="$(Pkg7ZipCLI)\ tools \ 7za.exe"DestinationFolder ="$(ProjectDir)"</Copy></Target>< ItemGroup><无包含="$(ProjectDir)7za.exe";Pack ="true"PackagePath ="content \ Resources; contentFiles \ any \ any \ Resources">< PackageCopyToOutput> true</PackageCopyToOutput>< Visible> false</Visible>< CopyToOutputDirectory>始终</CopyToOutputDirectory></无></ItemGroup>< ItemGroup><无包含="build \ *.*"Pack ="true"PackagePath ="build; buildCrossTargeting"</None></ItemGroup>...</Project> 

I have been trying to include the 7za.exe file from the installed 7ZipCLI NuGet package into a project, of which I want a NuGet package to be created. The NuGet package targets both.NET Standard2.0 and .NET Framework v4.6.1+. I am using the Net Standard capability of creating NuGet packages on Build.

The Fonts copy perfectly to the NuGet package, but the .exe does not want to copy.

Can someone please help me get the 7za.exe to be placed in the package's content folder? It does not need to be the content folder, I just want it copied to the consuming project's output directory on Installing of the NuGet package.

Here is the .csproj file:

<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>

Also here is what the NuGet package looks like currently:

解决方案

If you just want the file be output into the bin folder under packages.config, you should have to use <packages_id>.props file and <PackageCopyToOutput>true</PackageCopyToOutput> is only for PackageReference.

Also, <CopyToOutputDirectory>Always</CopyToOutputDirectory> is only for local file under the local project rather than the nuget package. And it does not work for nuget package.

Solution

1) add a file called DocumentGeneration.props(<packages_id>.props and your packageID is DocumentGeneration) under build folder

2) modify your DocumentGeneration.csproj file:

.....

      <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) add these on the DocumentGeneration.props file:

<Project>
    <ItemGroup>

        <None Include="$(ProjectDir)Resources\*.*">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>           
        </None> 
    </ItemGroup>

</Project>

Or

<Project>
        <None Include="$(MSBuildThisFileDirectory)..\content\**\*.*">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
            <Link>Resources\%(FileName)%(Extension)</Link>
        </None>
 </Project>

4) repack your lib project. When you install this new release version, you should first clean nuget caches or delete all cache files under C:\Users\xxx(current user)\.nuget\packages.

Besides, there is a similar issue.

Update

I think it is an issue for multi-targetframeworks feature. And if you use GeneratePathProperty for the pkg, the property will lost on the other targetframework. It is quite strange and when you change to use <TargetFramework>, it works well. Since the issue is under TargetFrameworks with GeneratePathProperty=true. I have no other good idea for that and cannot find the good solution to fix them.

And you should report it on DC Forum or raise an issue on github. And when you finish it, you could share it here.

1) Also, since you use multi-targetframeoworks, you should also use buildCrossTargeting folder on the nupkg to enable the props file. See this similar issue.

2) Second, when I use these to distinguish between them, nuget also cannot pack it. And it is so strange, and I have to remove that condition.

<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>

My thought is that content and contentFiles node target to the whole project and you cannot specify a condition with targetframeworks during packing them as content. And what I know is that content and contentFiles are targets to the whole and cannot target to a targetframework under multi-targetframeworks.

You should use these:

<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>

这篇关于使用.exe文件作为内容文件创建NuGet包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆