dotnet core nuget 包在还原时复制内容文件 [英] dotnet core nuget package copying content files on restore

查看:28
本文介绍了dotnet core nuget 包在还原时复制内容文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我觉得我已经走到了绳索的尽头,但希望有人比我在这里知道的更多.我有一些 Typescript 文件,尽管这几乎无关紧要,因为我的所有内容文件都存在此问题.

So I feel like I have come to the end of the rope here, but hoping someone knows more than I do here. I have some Typescript files, though that is mostly irrelevant as I am having this problem with all content files.

我能够生成 nuget,或更准确地说 dotnet 包,nuget 包,通过在 中使用它,将我的内容文件包含在包中.我的 parent 项目的 csproj:

I am able to generate a nuget, or more precisely dotnet pack, nuget package that includes my content files in the package by using this in the .csproj of my parent project:

<ItemGroup>
  <Content Include="ScriptsUtility.ts">
    <Pack>true</Pack>
    <PackagePath>contentFilesScripts;contentScripts</PackagePath>
  </Content>
</ItemGroup>

我可以浏览生成的 .nupkg 并看到该文件确实已添加到包中的 contentScriptscontentFilesScripts位置

I can browse the generated .nupkg and see that indeed the file was added to the package in both the contentScripts and contentFilesScripts locations

问题是,每当我在我的子"项目中使用这个包时,Typescript 永远不会被复制到 child 项目的任何文件夹中,尽管我可以看到它提取到 .nugetpackagesparent... 文件夹中.

The problem is that whenver I consume this package in my 'child' progect, that Typescript never gets copied into any folder of the child project, though I can see it extracted in the .nugetpackagesparent... folders.

起初我认为这是我在 parent 项目中的初始设置的问题,它可能是,但是在尝试了本书中的所有内容后,未能将内容文件复制到 项目.然后我尝试在我的包的 tools 文件夹中尝试使用 Init.ps1 的黑暗路径,虽然无法调试,但它似乎也偶尔运行(我完全卸载并重新安装了该软件包,但大部分时间它仍然无法运行.)这可能是方法,但我不知道为什么我无法将其输出到 Package Manager Consolecode>...也许 Init.ps1 仍有希望,但我似乎无法弄清楚.最后,我看到了 nuget .targets 的一些潜力文件 但我似乎也能掌握如何将它用于我的目的!我希望得到一些关于如何完成这项工作的反馈.

At first I thought it was something with my initial settings in the parent project, and it may be, but after trying what seems like everything in the book, that fails to copy the content files to the child project. I then tried going the dark path of trying to use Init.ps1 in the tools folder of my package, and though it was impossible to debug, it also seemed to run sporatically (I completely unistalled and reinstalled the package and it still failed to run most of the time.) This could be the way but I don't know why I can't get it to output to the Package Manager Console... maybe there's still hope with Init.ps1 but I can't seem to figure it out. Finally I see some potential with a nuget .targets file but I can's seem to grasp how to use it for my purpose either! I would love some feedback as to how to get this done.

推荐答案

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

在 Nuget v3.1 中,对于使用 project.json 文件的项目,从 Nuget 包中导入内容已被弃用.从那时起,project.json 文件已被删除,以支持新的 .csproj 格式.如果您使用的是 packages.config 文件,那么从 Nuget 包导入内容应该仍然有效.

Importing content from a Nuget package was depreciated for projects using a project.json file in Nuget v3.1. Since then the project.json file has been dropped in favour of the new .csproj format. Importing content from a Nuget package should still work though if you're using the packages.config file instead.

还提到的是,还有其他包管理器可用于交付内容.

Also mentioned is the fact that there are other package managers available for delivering content.

在我看来,新世界的答案是创建一个包含实用程序.js 的节点模块,然后让 npm 将其交付到您的项目中.

It looks to me like the answer in the new world is to create a node module containing utility.js and let npm deliver it to your project.

可能的解决方法:

我已经查看了 .targets 来复制文件并使其正常工作,但它确实在每个构建中运行 - 这对您来说可能是也可能不是问题.我不能用它做我想做的事.

I've looked at .targets to copy files and got this working, but it does run on each build - which may or may not be a problem for you. I can't do what I want with it.

在 [PackageId].targets 中:

In [PackageId].targets:

<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)....contentScriptsjs***.*"/>
        </ItemGroup>
        <Copy SourceFiles="@(SourceScripts)" DestinationFiles="@(SourceScripts -> '$(MSBuildProjectDirectory)wwwrootjs\%(RecursiveDir)%(Filename)%(Extension)')" Condition="!Exists('$(MSBuildProjectDirectory)wwwrootjs\%(RecursiveDir)%(Filename)%(Extension)')" />
    </Target>
    <!-- Or do this for the individual script -->
    <Target Name="CopyUtilityScriptToProject" BeforeTargets="Build">
        <Copy SourceFiles="$(MSBuildThisFileDirectory)....contentScriptsjsUtility.js" DestinationFiles="$(MSBuildProjectDirectory)wwwrootjsUtility.js" Condition="!Exists('$(MSBuildProjectDirectory)wwwrootjsUtility.js')" />
    </Target>
</Project>
<!-- Note: condition can be removed from either if you want it to overwrite each build -->

并在 .csproj 文件中(将 [PackageId] 替换为您的包名称):

and in the .csproj file (replacing [PackageId] with the name of your package):

<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
etcoreapp2.0[PackageId].targets" PackagePath="build
etcoreapp2.0[PackageId].targets" />
            <!-- Either -->
            <Content Include="Scriptsjs***.*" PackagePath="contentScriptsjs;contentFilesScriptsjs" />
            <!-- or -->
            <Content Include="ScriptsjsUtility.js" PackagePath="contentScriptsjs;contentFilesScriptsjs" />
        </ItemGroup>
</Project> 

似乎存在一个错误,当 [PackageId] 未在 .csproj 中明确设置时,构建目标不起作用.虽然这很可能是我的开发环境的问题.

There seemed to be a bug whereby when the <PackageId>[PackageId]</PackageId> wasn't set explicitly in the .csproj, the build targets didn't work. Although that may well be an issue with my development environment.

这篇关于dotnet core nuget 包在还原时复制内容文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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