Dot-Net-Core

dotnet core nuget 包在還原時複製內容文件

  • January 14, 2022

所以我覺得我已經走到了盡頭,但希望有人比我知道的更多。我有一些Typescript文件,儘管這幾乎無關緊要,因為我對所有內容文件都有這個問題。

通過在我的項目中使用它,我可以生成一個nuget,或更準確地說dotnet pack,nuget 包,其中包含我的內容文件:.csproj``parent

<ItemGroup>
 <Content Include="Scripts\Utility.ts">
   <Pack>true</Pack>
   <PackagePath>contentFiles\Scripts\;content\Scripts</PackagePath>
 </Content>
</ItemGroup>

我可以瀏覽生成.nupkg的文件並查看該文件確實已在content\ScriptscontentFiles\Scripts位置添加到包中

問題是,每當我在我的“子”項目中使用這個包時,它Typescript永遠不會被複製到child項目的任何文件夾中,儘管我可以看到它被提取到.nuget\packages\parent\...文件夾中。

起初我認為這是我在parent項目中的初始設置,可能是這樣,但是在嘗試了書中的所有內容之後,無法將內容文件複製到child項目中。Init.ps1然後我嘗試在我的包的文件夾中嘗試使用的黑暗路徑tools,雖然它無法調試,但它似乎也偶爾執行(我完全解除安裝並重新安裝了包,它仍然無法執行大部分時間.) 這可能是這樣,但我不知道為什麼我不能讓它輸出到Package Manager Console……也許還有希望,Init.ps1但我似乎無法弄清楚。最後,我看到了nuget.targets文件的一些潛力但我似乎也掌握瞭如何將它用於我的目的!我希望得到一些關於如何完成這項工作的回饋。

來自: 宣布支持通用 Windows 平台的 NuGet 3.1

對於在 Nuget v3.1 中使用 project.json 文件的項目,從 Nuget 包中導入內容已被棄用。從那時起,project.json 文件已被刪除,取而代之的是新的 .csproj 格式。如果您改用 packages.config 文件,從 Nuget 包導入內容應該仍然有效。

還提到了一個事實,即有其他包管理器可用於傳遞內容。

在我看來,新世界的答案是創建一個包含 utility.js 的節點模組,然後讓 npm 將其傳遞給您的項目。

可能的解決方法:

我查看了 .targets 來複製文件並使其正常工作,但它確實在每個建構上執行 - 這對您來說可能是也可能不是問題。我不能用它做我想做的事。

圖片

$$ PackageId $$.目標:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <!-- Either do this for all scripts in the Scripts/js folder -->
   <Target Name="CopyScriptsToProject" BeforeTargets="Build">
       <Message Text="Copying scripts to project" />
       <ItemGroup>
           <SourceScripts Include="$(MSBuildThisFileDirectory)..\..\content\Scripts\js\**\*.*"/>
       </ItemGroup>
       <Copy SourceFiles="@(SourceScripts)" DestinationFiles="@(SourceScripts -> '$(MSBuildProjectDirectory)\wwwroot\js\%(RecursiveDir)%(Filename)%(Extension)')" Condition="!Exists('$(MSBuildProjectDirectory)\wwwroot\js\%(RecursiveDir)%(Filename)%(Extension)')" />
   </Target>
   <!-- Or do this for the individual script -->
   <Target Name="CopyUtilityScriptToProject" BeforeTargets="Build">
       <Copy SourceFiles="$(MSBuildThisFileDirectory)..\..\content\Scripts\js\Utility.js" DestinationFiles="$(MSBuildProjectDirectory)\wwwroot\js\Utility.js" Condition="!Exists('$(MSBuildProjectDirectory)\wwwroot\js\Utility.js')" />
   </Target>
</Project>
<!-- Note: condition can be removed from either if you want it to overwrite each build -->

並在 .csproj 文件中(替換

$$ PackageId $$使用您的包裹名稱):

<Project Sdk="Microsoft.NET.Sdk">
   ... any Globals for source control stuff ...
   <PropertyGroup>
       <TargetFramework>netcoreapp2.0</TargetFramework>
       <Version>7.0.0</Version>
       <PackageId>[PackageId]</PackageId>
   </PropertyGroup>
   ... any PackageReference stuff ...
   <ItemGroup Label="Packaging">
       <Content Include="build\netcoreapp2.0\[PackageId].targets" PackagePath="build\netcoreapp2.0\[PackageId].targets" />
           <!-- Either -->
           <Content Include="Scripts\js\**\*.*" PackagePath="content\Scripts\js;contentFiles\Scripts\js" />
           <!-- or -->
           <Content Include="Scripts\js\Utility.js" PackagePath="content\Scripts\js;contentFiles\Scripts\js" />
       </ItemGroup>
</Project> 

似乎有一個錯誤,當<PackageId>[PackageId]</PackageId>.csproj 中沒有明確設置時,建構目標不起作用。儘管這很可能是我的開發環境的問題。

引用自:https://stackoverflow.com/questions/49425012