簡體   English   中英

使用.exe文件作為內容文件創建NuGet package

[英]Creating a NuGet package with .exe file as content file

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. NuGet package 同時針對 .NET Standard2.0 和 .NET Framework v4.6.1+。 我正在使用 Net Standard 功能在 Build 上創建 NuGet 包。

Fonts完美復制到NuGet package,但是.exe不想復制。

有人可以幫我將 7za.exe 放在包的內容文件夾中嗎? 它不需要是內容文件夾,我只是希望在安裝 NuGet package 時將其復制到使用項目的 output 目錄。

這是 .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 package 目前的樣子:

當前的 NuGet 包

如果您只想將文件 output 放入packages.config下的 bin 文件夾中,則必須使用<packages_id>.props文件,並且<PackageCopyToOutput>true</PackageCopyToOutput>僅適用於PackageReference

此外<CopyToOutputDirectory>Always</CopyToOutputDirectory>僅適用於本地項目下的本地文件,而不適用於 nuget package。 它不適用於 nuget package。

解決方案

1)build文件夾下添加一個名為DocumentGeneration.props的文件(<packages_id>.props 並且您的 packageID 是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項目。 安裝此新版本時,應首先清理 nuget 緩存或刪除C:\Users\xxx(current user)\.nuget\packages下的所有緩存文件。

此外,還有一個類似的問題

更新

我認為這是多目標框架功能的問題。 如果您對 pkg 使用GeneratePathProperty ,則該屬性將在其他目標框架上丟失。 這很奇怪,當您更改為使用<TargetFramework>時,它運行良好。 由於問題是在TargetFrameworks下使用GeneratePathProperty=true 我對此沒有其他好主意,也找不到解決它們的好方法。

在此處輸入圖像描述

您應該在DC 論壇上報告或在 github 上提出問題 當你完成它時,你可以在這里分享它。

1)另外,由於您使用多目標框架,您還應該使用 nupkg 上的buildCrossTargeting文件夾來啟用道具文件。 看到這個類似的問題

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>

我的想法是contentcontentFiles節點針對整個項目,並且在將它們打包為內容期間,您不能使用targetframeworks指定條件。 我所知道的是contentcontentFiles是整體的目標,不能針對multi-targetframeworks 下的 targetframework 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM