【问题标题】:nuspec contentFiles not added to a projectnuspec contentFiles 未添加到项目中
【发布时间】:2017-08-28 14:36:21
【问题描述】:

我有一个内部包含 project.json 的 Web 项目 (mvc5)。

另外,我有一个 nuget 包。在这个包内(除了 dll 参考)我有一些内容文件(cshtml 文件、css、javascript 等)。

有两个目标要实现:

  • 将包安装到项目后,我希望将内容文件包含到项目中。
  • 构建项目后,我希望 nuget 恢复内容文件

nuspec 文件:

<?xml version="1.0"?>
    <package>
      <metadata>
        /.../

        <dependencies>
            <group targetFramework="net461">
              /.../

           </group>
        </dependencies>
          <contentFiles>
        <files include="**/*.*" flatten="false" copyToOutput="false" buildAction="Content" /> 
      </contentFiles>
      </metadata>
      <files>
         /.../
    <file src="..\src\Content\**\*.*" target="contentFiles\any\any\Content" /> 
    <file src="..\src\Views\**\*.*" target="contentFiles\any\any\Views" /> 
      </files>
    </package>

嗯,Visual Studio 正在毫无问题地添加 nuget 包。参考也包括在内。但内容总是缺失。内容文件不会从 /packages/ 文件夹复制,也不包含在项目中。 当我构建项目时也是如此 - 它只是复制 dll 而根本不触及包中的内容文件。

只有当我有 copyToOutput="true" 时它才有效,但所有内容当然都放在 bin 文件夹中。

我需要恢复内容文件的原因是 nuget 包中的文件被 tfs 'ignore file' (.tfignore) 忽略了。我不想让这些内容文件成为 TFS 中 Web 项目的一部分。但是,当然,TFS 构建服务器无法构建项目,因为缺少一些内容文件(包中的文件)。所以我希望构建服务器上的 nuget 在它统计构建项目之前恢复内容文件。

如果可能的话,知道如何完成这项工作吗?或者这可能是不可能的,我必须将内容文件包含到项目中?

【问题讨论】:

    标签: visual-studio nuget nuget-package project.json nuspec


    【解决方案1】:

    这是设计使然。 NuGet 包不再应该修改项目的源代码,而只是添加到其构建的输出或构建过程中(除了 .NET 库)。用于向项目添加源的 content 文件夹仅继续支持基于 packages.config 的项目,因为这将是现有项目的重大更改,但使用 project.jsonPackageReference (VS 2017) 的项目获得新行为。

    【讨论】:

      【解决方案2】:

      您可以将 msbuild 目标添加到您的包中。在这个目标中你可以执行 nuget restore 命令...

      示例:

      <?xml version="1.0"?>
      <package >
          <metadata>
          <!-- ... -->
          </metadata>
          <files>
            <!-- Include your MSBuild target to \build -->
            <file src="build\myNuGetRestoreTarget.targets" target="build" />
          </files>
      </package>
      

      见: https://docs.microsoft.com/en-us/nuget/create-packages/creating-a-package#including-msbuild-props-and-targets-in-a-package

      【讨论】:

        【解决方案3】:

        我处于这种情况。以下是我的处理方式。

        1. 不要将您的文件保留为content,内容文件会添加到项目文件中。将目标重命名为其他名称。我将其保留为drop。你的 nuspec 文件应该是这样的:

           <package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
               <metadata>
                   <id>ID</id>
                   <version>1.0</version>
                   <title>Title</title>
                   <authors>J</authors>
                   <owners>J</owners>
                   <requireLicenseAcceptance>false</requireLicenseAcceptance>
                   <description>Binary Files such as images, fonts, icons and svgs</description>
                   <copyright>J</copyright>
               </metadata>
               <files>
                   <file src="..\..\Internal.Resources.targets" target="build" />
                   <file src="..\..\**\*.*" target="drop" exclude="**\*.targets"/>
               </files>
           </package>
          
        1. 将目标文件保留为您的drop 文件之一。如下:

          <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
            <ItemGroup>
             <RequiredFiles Include="$(MSBuildThisFileDirectory)..\drop\**\*.*" />
             <None Include="@(RequiredFiles)">
              <Link>%(RecursiveDir)%(FileName)%(Extension)</Link>
              <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
             </None>
            </ItemGroup>
           </Project>
          

        现在您的nuget install 将只提取packages 文件夹中的内容,将目标添加到您的项目中。构建时,目标将从包目录中获取文件并放入构建目录。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-05-29
          • 2017-04-06
          • 2022-10-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多