I am trying to understand how NuGet packages work so that I can package some of my repeatedly used source for future projects.
I am working with a simple class library (.Net Foundation) and using the CLI to make the nuspec file and then packing from the CLI I get a nuget package that installs the Class Library DLL perfectly.
But now I want to add a transform to the App.Config that includes a connection string and I want to copy a simple readme type file to the destination project.
Using the following nuspec file the readme does not install to the readme folder as expected (doesn't install at all) and I can't get any of the app.config.transforms to work at all.
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>$id$</id>
<version>$version$</version>
<title>$title$</title>
<authors>$author$</authors>
<owners>$author$</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>$description$</description>
<releaseNotes>Stuff</releaseNotes>
<copyright>Copyright 2019</copyright>
<tags>123 1234</tags>
</metadata>
<files>
<file src="app.config.transform" target="content\app.config.transform" />
<file src="Model\State1.cs" target="content\State1.cs"/>
<file src="new.txt" target="content\new.txt"/>
</files>
</package>
Where should I put the transform files? It seems that they should be in a content folder in my source project but that isn't working. When I pack the nuget package I get an error about the files not being inside the content folder even though they clearly are.
With the above nuspec file I get an error:
Access to the path 'C:\Users\ UserName \.nuget\packages\MyDLLProject.lookups\1.1.7041.17482\content' is denied.
Why is that?
Related
I have a Class Library (net47) project and I'd like to pack into a nuget my dll and several files of static content (js, css, images...). I want to use this dll and the content from the consumer projects. These projects will be MVC PackageReference projects. In these projects the local static files are in the wwwroot folder.
I have tried this: NuGet ContentFiles Demystified but I get my js and css files referenced (they aren't copied to my project content).
In my nuspec I've tried with all build options: EmbeddedResource, Content, None and Compile, but these references are imported always in Compile mode. So I get a compile error when I start debugging.
I know this was possible with Package.config projects and it's very simple but all my consumer projects will be PackageReference.
This is my nuspec
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>MyProject</id>
<version>1.0.0</version>
<authors>My</authors>
<owners>My</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>LVP</description>
<copyright>Copyright 2018</copyright>
<tags>Tag1 Tag2</tags>
<contentFiles>
<files include="any/any/bd.js" buildAction="content" flatten="true" copyToOutput="false"/>
</contentFiles>
</metadata>
<files>
<file src="contentFiles/any/any/bd.js" target="contentFiles/any/any/bd.js" />
</files>
</package>
I pack my nuget with this powershell command:
nuget pack MyProject.nuspec
Although I have also tried with the csproj:
nuget pack MyProject.csproj
And my source folder structure is this:
C:\...[projectPath]...\contentFiles\any\any\bd.js
Installation is ignoring my build action.
Why is always trying to compile my content files? Is there a better way to add static content to the consumer project?
Installation is ignoring my build action. Why is always trying to compile my content files? Is there a better way to add static content to the consumer project?
To answer your previous question Packing files on nuget, I have created a sample nuget package and set the build action to content for the content files, after install that nuget package, the build action would be set content:
Then I checked your .nuspec file, found it should be correct. So the issue is not related to your .nuspec file.
Besides, in the above image, you will notice that the path of the content file is nuget local cache:
C:\Users\<UserName>\.nuget\packages\
NuGet will first extract the nuget package from the local cache when install the nuget package to avoid downloading packages that are already on the computer. In other wards, although we have updated the nuget package in the local, nuget will detect the local cache first, if it found the same package in the cache, nuget will install it from cache rather than local feed.
To resolve this issue, please try to remove your nuget package in the local cache before installing the updated nuget package. Generally, when we package the same package again, wed better change the package version in the.nuspec` file so nuget local cache will not catch them.
Update for comment:
I've tried increasing the version number and deleting the nuget cache and the problem persists. My build action is always set to "C# Compiler". I just tried changing the name of the js file and the project imports the new name so I do not think it's a cache problem
After test your nuget package, I found the reason why you get that issue, we should keep the path the src and target paths are the same in the .nuspec file. Since you want set content file to the wwwroot folder, you should set the file in the wwwroot folder, then pack the .nuspec:
<contentFiles>
<files include="any/any/wwwroot/css/bd.css" buildAction="Content" copyToOutput="false" flatten="true" />
<files include="any/any/wwwroot/js/bd.js" buildAction="Content" copyToOutput="false" flatten="true" />
</contentFiles>
Following in my .nuspec scripts(Not need content node):
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata>
<id>BancaDigitalViewProvider</id>
<version>1.0.37</version>
<authors>Ibercaja</authors>
<owners>Ibercaja</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Login View Provider</description>
<copyright>Copyright 2018</copyright>
<tags>Banca Digital View Provider</tags>
<dependencies />
<contentFiles>
<files include="any/any/wwwroot/css/bd.css" buildAction="Content" copyToOutput="false" flatten="true" />
<files include="any/any/wwwroot/js/bd.js" buildAction="Content" copyToOutput="false" flatten="true" />
</contentFiles>
</metadata>
<files>
<file src="contentFiles/any/any/wwwroot/css/bd.css" target="contentFiles/any/any/wwwroot/css/bd.css" />
<file src="contentFiles/any/any/wwwroot/js/bd.js" target="contentFiles/any/any/wwwroot/js/bd.js" />
<file src="bin\debug\BancaDigitalViewProvider.dll" target="lib\net47\BancaDigitalViewProvider.dll" />
</files>
</package>
This is nuget package:
https://1drv.ms/u/s!Ai1sp_yvodHfhTk5xutPpaBZLC-A
You can download it and test.
Then install it to the ASP.NET core MVC project:
Hope this helps.
I have this nuspec file:
<?xml version="1.0"?>
<package >
<metadata>
<id>My.Package</id>
<version>1.0.1.7</version>
<title>My Package</title>
<authors>My name</authors>
<owners>Mu author</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>My description</description>
<releaseNotes>Release note</releaseNotes>
<copyright>Copyright 2017</copyright>
<tags>tag1 tag2</tags>
<contentFiles>
<files include="myFile.config" copyToOutput="true" />
</contentFiles>
</metadata>
<files>
<file src="myFile.config" target=""/>
</files>
</package>
When I pack this nuspec file, my nupkg file is created and in the NuGet Package Explorer I see that myFile.config is included in my package. All well so far.
But, then, when I install this package, the dll is added to references, but myFile.config is not added to the solution.
Things I've tried without success:
- View the folder on Disk to see if myFile.config is created (not there)
- <file src="myFile.config" target="."/>
- <file src="myFile.config" target=".\"/>
I also want the file to set Copy to Output Directory: Copy always.
What am I missing here?
PS: I am working on a private nuget server.
The target needs to be content since files of the nugpkg's content subdirectory are copied to the consuming project.
Note that this only works for projects using the packages.config style of referencing NuGet packages. For PackageReference (available in VS 2017, default for .NET Core / ASP.NET Core / .NET Standard projects), there is a new contentFiles feature that includes files logically in the build process.
See NuGet's documentation on including content files for more information.
I'm trying to create a nuget package from a csproj file. This package will include an install.ps1 script in the tools folder and some files in the content folder.
However, it seems like when packing from a csproj file, nuget will pull the package information (description, tags, etc.) from the corresponding nuspec file, but not anything else. It ignores the tools folder that is in the same directory as the nuspec file as well as the content folder.
When packing this way nuget also seems to ignore files included in the contentFiles section of the nuspec file.
Is this expected behavior? If it is, is there a way for me to pack from a csproj and get the content and tools folders to be included in the package?
I realize I could just use only a nuspec file and this would work, but I have multiple packages I'm trying to build this way and managing the dependencies manually becomes a less than trivial task.
Running NuGet 3.4.4.1321
My nuspec file:
<?xml version="1.0"?>
<package>
<metadata>
<id>$id$</id>
<version>$version$</version>
<title>$id$</title>
<authors>authors</authors>
<owners>$owners$</owners>
<projectUrl>http://dummy.url</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>the description</description>
<copyright>$copyright$</copyright>
<releaseNotes>$releaseNotes$</releaseNotes>
<contentFiles>
<files include="content\App.config.install.xdt"/>
<files include="content\App.config.uninstall.xdt"/>
<files include="temp\App.config"/>
</contentFiles>
<tags>wpf testing</tags>
</metadata>
</package>
Turns out the documentation for this confused me a bit. Content Files are not a replacement for Files. In NuGet 3, both can be used simultaneously.
Using the Files tag outside of the metadata tag in my nuspec file allowed me to specify items that go into the tools and content folder.
Updated nuspec:
<?xml version="1.0"?>
<package>
<metadata>
<id>$id$</id>
<version>$version$</version>
<title>$id$</title>
<authors>authors</authors>
<owners>$owners$</owners>
<projectUrl>http://dummy.url</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>the description</description>
<copyright>$copyright$</copyright>
<releaseNotes>$releaseNotes$</releaseNotes>
<tags>wpf testing</tags>
</metadata>
<files>
<file src="App.config.install.xdt" target="content"/>
<file src="App.config.uninstall.xdt" target="content"/>
<file src="tools\install.ps1" target="tools"/>
</files>
</package>
Hopefully this helps in case anyone else gets tripped up by the docs here.
A bit more discussion can be found in this NuGet Issue.
You could do nuget pack -Tool
Tool - Specifies that the output files of the project should be placed in the tool folder.
https://learn.microsoft.com/en-us/nuget/tools/cli-ref-pack
I am currently creating a nuget package with a nuspec file but getting the following error:
An item with the same key has already been added.
My command I am using is:
nuget pack "MyProject.csproj" -o "..\Packages"
This is my nuspec file:
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>$id$</id>
<version>$version$</version>
<title>$title$</title>
<authors>$author$</authors>
<owners>$author$</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>$description$</description>
<language>$language$</language>
</metadata>
<files>
<file src="bin\MyLibrary*.dll" target="lib\net45" />
</files>
</package>
The nuspec file I am using is also used when packaging other packages withing the same library. Could this be the reason why the above error is occurring? Any ideas?
It might be that you are adding files in nuspec that are also getting added when you call pack on the .csproj (files/dlls referenced by the csproj). If so you can remove the file references from the nuspec file and give it a try.
how does your nuspec file look like?
I created it with the following steps
1. create the Mylibrary project
2. let me add a dependency, I installed ninject package to the project
3. build
4. nuget spec mylibrary.csproj
5. nuspec file generated, I didn't add any file or dependency manually to the file
6. nuget pack mylibrary.csproj
7. nuget pack would automatically add ninject as a dependency and also add mylibrary.dll into the correct folder
8. http://npe.codeplex.com/ is a nice tool to open the nupkg file and see what got generated inside the package.
I had this error trying to use the package visualizer and it ended up that my packages.config had the same package name in it more than once with different versions.
I created a nuspec file:
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>MyPackage.dll</id>
<version>3.5</version>
<authors>Me</authors>
<owners>Me</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>The description</description>
<tags>tag1</tags>
</metadata>
</package>
In a directory with the file strucutre
MyPackage
Assemblies
MyPackage.dll
MyPackage.nuspec
I created my package using nuget.exe pack myPackage.nuspec and placed it in my local sources. I can find and install it from visual studio at which point
The dll is copied into the packages directory
But the reference is not added to the project
No repositories.config is created
No packages.config is created
What am I missing?
In case someone runs into this in the future. The solution was that the 'Assemblies' directory needed to be renamed 'lib'. The documentation was wrong (been updated, now it's correct).
Big thanks to dotnetjunky over on codeplex