The structure of my folder & source codes.
Areas/**/*.cshtml
Views/**/*.cshtml
And those cshtml files were compiled into dlls by generators. So that it does not need to be included into nuget packages. (Only the dll is needed)
So the Nuspec file will be like this.
<files>
</files>
Now I want to include additional dlls into Nuget packages.
So i added the following the Nuspec file
<files>
<file src="bin\Release\**\xxxxx.dll" target="lib\net451" />
</files>
But this will bring the *.cshtml files into nuget packages.
Is there any way that i could exclude those files?
Thanx!
Thanx to #rohit21agrawal and #jainaashish.
I create an issue in github
https://github.com/NuGet/Home/issues/6265
If you are packing your csproj with a .nuspec next to it, then the content files from your project will be added if one of the condition is met:
There is no nuspec file
There is a nuspec file but no files node.
There is a nuspec file with non-empty files node.
The only way to prevent content files from your project to be added into the package is to specify an empty files node.
Related
I need to create a nuget package package with stylecop.json and something.ruleset with referencing to stylecop analyzer.
This package will be used across all the teams to standardize the rules for .net.
I have read this:
How to ship the stylecop.json and custom.ruleset files with a NuGet package in VS2017
https://ngeor.com/2018/03/03/how-to-use-stylecop-analyzers.html
But unfortunately I did not properly understand how to achieve this. I have stylecop.json and .ruleset file. I have done the following steps:
Added nuget package for Stylecop Analyzers
Added the stylecop.json file to the solution
Added the .ruleset file to the solution
Added a nuspec file to the solution
May I please know what need to be done now to create the nuget package including the stylecop.json and .ruleset file?
May I please know what need to be done now to create the nuget package
including the stylecop.json and .ruleset file?
You should add some nodes about these files in the xxx.nuspec and then use nuget.exe cli with xxxx.nuspec file to pack your project.
Since you have generated the xxxx.nuspec file, you should add these in nuspec file.
Solution
1) You should add these nodes into xxx.nuspec file and with it, these files will be added into the new projects by nuget.
<?xml version="1.0" encoding="utf-8"?>
<package >
<metadata>
.......
<contentFiles>
<files include="any/any/stylecop.json "(the relativepath of the file under the ContentFiles folder in the xxx.nupkg) buildAction="None" copyToOutput="true" flatten="false" />
<files include="any/any/xxxx.Ruleset "(the relativepath of the file under the ContentFiles folder in the xxx.nupkg) buildAction="None" copyToOutput="true" flatten="false" />
</contentFiles>
</metadata>
<files>
<file src="stylecop.json(the relativePath of the file under the project)" target="contentFiles/any/any" />
<file src="xxxx.Ruleset(the relativePath of the file under the project)" target="contentFiles/any/any" />
<file src="stylecop.json(the relativePath of the file under the project)" target="content" />
<file src="xxxx.Ruleset(the relativePath of the file under the project)" target="content" />
</files>
</package>
2) Remember to use this function, you should use nuget.exe and you should download it from the link and then set its path into PATH of system environment variable. For an example, the downlaod path is C:\nuget\nuget.exe, you should set C:\nuget in PATH and then yo can call nuget in CMD.
Besides, when you modify xxxx.nuspec file just I said above, you should first cd the path of your project and check whether the xxxx.csporj file exists under that path.
After that, you can use nuget pack in CMD to pack your project and then you will obtain a xxx.nupkg which is the nuget package.
Desired output
We want to distribute a .dll (NetStandard project) and some files through the NuGet package installation. When installing it in a Xamarin.Android project:
A file (Directory.Buil.props) is copied to the solution folder
An executable (config.exe) is copied to the project folder
A directory (Files) and its contents are copied in the project folder
Problems
Projects using PackageReference will not get the files copied (content not supported)
For some reason, when using a .nuspec file; source files, obj, bin etc. are packed too
Solution
Ideally, we would like to:
only use a .csproj file (without .nuspec)
not have both content and contentFiles packed in the .nupkg
easily access the .dll from the .csproj
when installing a newer .nupkg version, old files will be overwritten
Questions
(1) Is this doable with PackageReference and contentFiles ?
(2) What's the best approach you can think of ?
Thanks.
Responses
Leo:
When installing the package in an Android project, the files don't appear in the project. Not to mention that the files are just referenced and not copied (even if I had copyToOutput="true"):
Leo (edit):
I cannot use the new SDK csproj format. Taken from your link:
Disclaimer: this only works for a small set of project types.
class library projects
console apps
ASP.NET Core web apps
.NET Core
If you are building ASP.NET 4 (i.e not ASP.NET Core), WPF, Universal Windows, or Xamarin projects, you’ll have to stick with the old format
(1) Is this doable with PackageReference and contentFiles ?
I am afraid you could not add those files to the Android project, but I would like provide an alternative solution here, add those files to the output folder.
You could use PackageReference and contentFiles directly for the latter two requirements, config.exe and Files. But for the first requirement Directory.Buil.props, we need do more things, since it is copied to the solution folder rather than project folder.
For the latter two requirements, config.exe and Files, we could use .nuspec file with contentFiles to including them, need set copyToOutput="true", like:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>MyTestCore</id>
<version>4.0.0</version>
<authors>TestContentFile</authors>
<owners>TestContentFile</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Package Description</description>
<contentFiles>
<files include="any/any/config.exe" buildAction="content" flatten="true" copyToOutput="true"/>
<files include="any/any/Files/1.txt" buildAction="content" flatten="true" copyToOutput="true"/>
<files include="any/any/Files/2.txt" buildAction="content" flatten="true" copyToOutput="true"/>
</contentFiles>
</metadata>
<files>
<file src="contentFiles/any/any/config.exe" target="contentFiles/any/any/config.exe" />
<file src="contentFiles/any/any/Files/1.txt" target="contentFiles/any/any/Files" />
<file src="contentFiles/any/any/Files/2.txt" target="contentFiles/any/any/Files" />
</files>
</package>
After packing this .nuspec and install the generated package to the project.
However, we could not find those files under the References node. That because the project still use the old csproj with packagereference not using the new sdk csproj.
Old csproj to new csproj: Visual Studio 2017 upgrade guide
Besides, copying files into the project's source directory is not supported and has been a discouraged practice for classic projects. The contentFiles section controls the msbuild items that are generated for these files into the obj\projectname.csproj.nuget.g.props file. And check the project.assets.json file you can find:
"targets": {
"MonoAndroid,Version=v7.1": {
"MyTestCore/5.0.0": {
"type": "package",
"contentFiles": {
"contentFiles/any/any/Files/1.txt": {
"buildAction": "Content",
"codeLanguage": "any",
"copyToOutput": true,
"outputPath": "1.txt"
},
"contentFiles/any/any/Files/2.txt": {
"buildAction": "Content",
"codeLanguage": "any",
"copyToOutput": true,
"outputPath": "2.txt"
},
"contentFiles/any/any/config.exe": {
"buildAction": "Content",
"codeLanguage": "any",
"copyToOutput": true,
"outputPath": "config.exe"
}
}
See: nuspec contentFiles not added to a project
Then we need build this project, those files will copied to the output folder.
For the first requirement, in order to add the Directory.Buil.props to the solution folder, we need create a custom copy target in the YourPackageName.targets file, then add this .targets file into the \build folder, the .targets file looks like:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<MySourceFiles Include="<FilePath>\Directory.Buil.props"/>
</ItemGroup>
<Target Name="CopyFiles" BeforeTargets="Build">
<Copy
SourceFiles="#(MySourceFiles)"
DestinationFolder="<SolutionFolder>"
/>
</Target>
</Project>
The .nuspec file like:
<files>
<file src="<>\xxx.targets" target="build" />
</files>
Hope this helps.
I've run into a problem when upgrading a .NET 4.6 project to .NET Core 2.0. All our projects use a custom StyleCop ruleset which is provided by a NuGet package. The ruleset is in a file called custom.ruleset and lives in the content folder inside the package. All our projects consume this package and so get a copy of custom.ruleset.
However, in Core 2.0 and Standard 2.0 projects this doesn't work. Files are no longer copied from the content folder of a package, and we're told to use the contentFiles folder instead.
I have a nuspec that now looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<version>1.0.11</version>
<metadata>
...
<contentFiles>
<files include="content\*.ruleset" buildAction="None" copyToOutput="false" flatten="true"/>
</contentFiles>
</metadata>
<files>
<file src="content\**" target="contentFiles/any/any" />
</files>
</package>
With this structure the ruleset appears in Visual Studio under the project, but trying to reference it from the project's .csproj file with <CodeAnalysisRuleSet>custom.ruleset</CodeAnalysisRuleSet> silently fails and reverts to using the default ruleset. I can force it to work by adding <CodeAnalysisRuleSet>$(NuGetPackageRoot)CustomRuleset\1.0.11\contentFiles\any\any\custom.ruleset</CodeAnalysisRuleSet> but this means the csproj will need updating whenever the ruleset changes, so it may as well be a manual process. Any ideas how to fix this?
The idea is to not try to deploy the file as content but add build logic to the NuGet package.
Make sure that the package is structured in the following way:
build\
build\custom.ruleset
build\{YourPackageName}.targets (e.g. CustomRuleset.targets)
This structure causes the .targets file to be automatically imported into the consuming project by convention.
The .targets file should then contain:
<Project>
<PropertyGroup>
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)custom.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
</Project>
This will cause the project's ruleset property to be overwritten to the location relative to the .targets file.
Note that this also applies to .net framework projects using the new PackageReference style of NuGet packages (replacement of packages.config) which is opt-in in VS 2017 (15.2+).
Simple question. I have a NuGet package project. This project references other class library projects (they are not NuGet Packages). I would like my NuGet package to load its references DLL's into the project installing the package. Is this possible, or do all my referenced class libraries need to be NuGet packages in order to specify them as dependencies?
TIA
When performing a nuget pack command, you can specify the option IncludeReferencedProjects.
From the docs:
Indicates that the built package should include referenced projects either as dependencies or as part of the package. If a referenced project has a corresponding .nuspec file that has the same name as the project, then that referenced project is added as a dependency. Otherwise, the referenced project is added as part of the package.
You can add your referenced dlls as files to nuspec and can set immediately source path file and target path file inside a nuget package. Next you should add references to this files in nuspec. It looks like this (I removed other metadatas):
.nuspec
<?xml version="1.0" encoding="utf-8"?>
<package>
<metadata>
...
<references>
<reference file="First.dll" />
<reference file="Second.dll" />
</references>
</metadata>
<files>
<file src="SomePath\First.dll" target="lib\First.dll" />
<file src="SomePath\Second.dll" target="lib\Second.dll" />
</files>
</package>
Is there any possibility to create a NuGet package containing the source code that can be referenced as library?
When I use the .nuspec for packing the created .nupkg contains the source code but cannot be referenced. I have already tried out to add a library node within the .nuspec as some suggested on SO but the resulting .nuspec does not match the standard and thus cannot be created.
When using .csproj for packing the .nupkg does only contain the .dll. It can be referenced but cannot be debugged because it does not contain any source code.
How could I achieve both? A referanceable library that contains source code.
In advance thank you for your time.
Here's a .nuspec file I use to package sources from multiple directories that get used as a library in other projects:
<?xml version="1.0"?>
<package >
<metadata>
<id>Your.Package</id>
<version>1.0.14</version>
<authors>me</authors>
<owners>me</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Class library.</description>
<releaseNotes>Initial release.</releaseNotes>
<copyright>Copyright 2017</copyright>
<tags>some tags</tags>
</metadata>
<files>
<file src="*.cs" target="content/App_Packages/<YourPackage>.Sources" />
<file src="Configuration/*.cs" target="content/App_Packages/<YourPackage>.Sources" />
</files>
</package>
Just change the "file" tags to reference the files you're trying to include.
When you're ready to create the .nupkg file, cd to the directory containing your .nuspec file and run:
nuget pack .nuspec
There's now the csproj tag EmbedAllSources. What that does is embed your source code into your NuGet package.
Usage is like this:
<EmbedAllSources>True</EmbedAllSources>
Embedding the source code will allow people to, for example, navigate to your method definitions and see the code just the way you've written it, as opposed to, not being able to navigate to definitions, at all, or rely on decompiled code.
Adding the debug symbols to the above will allow your users to put breakpoints in your code and step through it during debugging.
If that is what you want, add this to your csproj:
<DebugType>Embedded</DebugType>
<EmbedAllSources>True</EmbedAllSources>