NuGet package files not being copied to project content during build

泪湿孤枕 提交于 2020-01-22 07:20:11

问题


I am building an MVC4 web application with VS2012 professional with NuGet Package Manager version 2.2.31210. I have multiple projects in my solution, all sharing various packages I installed using NuGet. One of my projects is an MVC4 web application where I am using packages such as bootstrap, jquery UI, etc, all installed using NuGet.

When I clone a fresh copy of my entire solution from my repository and build my MVC4 project, the package restore feature seems to be working: it creates the packages directory under the solution direcotry and populates it will all the versions of the packages I expect to see. However, the content files do not get copied to the appropriate places in the MVC app directory. The weird thing is that it does create directories for the content, but does not copy the content files themselves.

For example, I am using the Twitter Bootstrap package which appears in the packages/Twitter.Bootstrap.2.2.2. In the MVC project a directory called bootstrap (containing css, img, and js directories) gets created in the Content directory. But, no css or js files are copied into those directories!

Does anyone have a clue what magic incantation I must utter to get the build to copy these content files from the NuGet packages directory?


回答1:


I have found a workaround, but it is ugly. By executing the following command in the NuGet Package Manager Console: Update-Package -Reinstall all the files are indeed copied to their proper places within the Mvc project Content and Scripts directories.

Unfortunately, this is risky because you are likely to end up with the wrong versions of certain packages. For example, in my case after the command finishes executing (which takes quite a while by the way), I end up with jQuery version 1.4.4. This is way old, and I assume it must be an explicit dependency of some other package that is being updated. So it appears that the order in which the packages actually get updated by NuGet is significant (it does not appear to parse the entire dependency tree for all packages and pick only the latest versions from the union of all dependencies, which seems like it would be the preferred behavior). Rather, as the command executes I see it replacing the jQuery package several times with different versions as it works its way through all the packages and their dependencies, only to end up with a very old version.

A similar approach is the execute the Update-Package -Reinstall command explicitly for each package that is causing my problem, but this is incredibly tedious and error prone.

The NuGet Package Restore feature should yield the same result as manually executing the Install-Package or Update-Package -Reinstall command for a package, but it does not.




回答2:


This is a very common issue we are all having. I've created an MSBuild Task NugetContentRestoreTask that will do this trick for you. Run the following command in the Package Manager Console:

Install Nuget Content Restore MSBuild Targets

    PM> Install-Package MSBuild.NugetContentRestore

The only thing left is to call it from your BeforeBuild Target with something like this:

Project File Targets

    <Target Name="BeforeBuild">
      <NugetContentRestoreTask SolutionDir="$(SolutionDir)" ProjectDir="$(ProjectDir)" />
    </Target>

You can take a look at the source repo and find it on nuget.org

Additional Content Folders

This nuget only includes the default folders scripts, images, fonts, and content, it is not a recursive directory includes. For additional content subfolders - you must assign the property AdditionalFolders.

    <Target Name="BeforeBuild">
        <NugetContentRestoreTask SolutionDir="$(SolutionDir)" ProjectDir="$(ProjectDir)" 
              AdditionalFolders="less;sass;common" />
    </Target>



回答3:


I don't like to have the thirdparty JavaScript files under source control either. Thats why I've followed Jeff Handley advice in http://nuget.codeplex.com/workitem/2094 to create a solution my self. I didn't go the executable way, but created a nuget solution level package which does the trick.

http://www.nuget.org/packages/Baseclass.Contrib.Nuget.GitIgnoreContent/

It's tied to git, as it automatically updates the .gitignore file.

Short description:

Ignore nuget content files in git:

  • Generate entries in the .gitignore file to exclude nuget content files from the source repository

  • Restore nuget content files before building (Automatically in VS and manually with a powershell script

I've written a blog post describing how to use it. http://www.baseclass.ch/blog/Lists/Beitraege/Post.aspx?ID=9&mobile=0




回答4:


In Visual Studio 2015 Update 1, they now support contentFiles. The caveat with this is that it only works in projects that use project.json.

In reference to the problem that you are having, there is a good blog post that explains why you see this behaviour: NuGet Package Restore Common Misconceptions.




回答5:


For my projects it turned out that content files work with PackageReferences only:

  1. Existing project with nuget references via packages.config
  2. Installed NuGet package with content files
  3. Build project
  4. No content files in output directory

  5. Conversion of packages.config to PackageReferences
  6. Build project
  7. Content files have been copied to output directory

IDE is Visual Studio 2017. The project is an application project which means it is in the old csproj format.



来源:https://stackoverflow.com/questions/14942374/nuget-package-files-not-being-copied-to-project-content-during-build

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!