I have a Visual Studio project that I have made into a NuGet package. The project has a file in it (called SwaggerIndex.html). I have set that file to have the "Copy to Output Directory" property to "Copy if newer". (I need this file to copy to the build folder on build.)
In that solution it works just fine. (I created another project to test it, and when it references my project that has the SwaggerIndex.html, it outputs it to the build folder just fine.)
However, when I package it into a NuGet and pull it into another solution/project, the SwaggerIndex.html file is not output on build.
I am making my NuGet package by going to the project's "Package" properties tab and selecting "Generate NuGet package on build". All projects involved are running .Net Core 3.1.
How can I get my NuGet Package to create my SwaggerIndex.html file on build (like it does when it is just a normal project)?
Please try these:
I have two solutions for you to solve the issue.
Solution 1
If you just want to install this nuget package only in new sdk projects(Net Core and Net Standard projects), you could use this
1) add these node in your xxx.csproj file:
<ItemGroup>
<None Include="xxx\SwaggerIndex.html(the path of SwaggerIndex.html file in your project)" Pack="true" PackagePath="contentFiles\any\any">
<PackageCopyToOutput>true</PackageCopyToOutput>
</None>
</ItemGroup>
2) then repack your nuget project, and before you install the new version, you should first clean nuget caches first or just delete all cache files under C:\Users\xxx(current user)\.nuget\packages
Solution 2
If you want this nuget package to copy SwaggerIndex.html file in both Net Framework and Net Core projects, you should use this:
1) add a file called <package_id>.props file in your nuget project. You should note that if your nuget package named as SwaggerIndex.1.0.0.nupkg, you should name the file as SwaggerIndex.props file so that it will work.
In my side, I put it into a folder called build.
2) then add these content in SwaggerIndex.props file,
<Project>
<Target Name="CopyToOutputFolder" BeforeTargets="Build">
<ItemGroup>
<File Include="$(MSBuildThisFileDirectory)..\File\*.*"></File>
</ItemGroup>
<Copy SourceFiles="#(File)" DestinationFolder="$(TargetDir)"></Copy>
</Target>
</Project>
3) add these in xxx.csproj file of your nuget project.
<ItemGroup>
<None Include="SwaggerIndex.html" Pack="true" PackagePath="File"></None>
<None Include="build\SwaggerIndex.props" Pack="true" PackagePath="build"></None>
</ItemGroup>
4) then repack your nuget package, before installing this new version of the nuget package, you should first clean all nuget caches first.
When you finishing installing the new version of the nuget package, click Build and the file will be copied into the main project.
Besides, there is also a similar issue about this.
Related
I have a nuget package containing my SQL database project. How can I reference the dacpac file in the package in order to deploy it to my azure sql server? I would like to do this from my ASP.net core application.
I needed to do this too, so here is what worked for me:
If your application uses the new SDK format, you can reference content files in a referenced nuget package in your project. You need to add the <GeneratePathProperty> node with a value of true in the package reference:
<ItemGroup>
<PackageReference Include="My.Package" Version="1.0.1">
<GeneratePathProperty>true</GeneratePathProperty>
</PackageReference>
</ItemGroup>
Then you can include the file you want to reference using a content link. The VS variable name is by convention starting with the prefix "Pkg" and replacing periods with underscores. You can also optionally include a <Link> node which provides a "notional" path for the included file when viewing it in your project. Example below:
<ItemGroup>
<Content Include="$(PkgMy_Package)\contentFiles\any\net48\SomeDb.dacpac">
<Link>dacpac\SomeDb.dacpac</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Pack>true</Pack>
</Content>
</ItemGroup>
The content file will be copied to the build output folder during build just like any other content file.
This is supported from VS 2017 15.9. Credit to Georg Dangl from his blog post: https://blog.dangl.me/archive/accessing-nuget-package-paths-in-your-net-sdk-based-csproj-files/
I currently have a .NET Standard 2.0 library that is a repository against a sql database. I also have a .sqlproj project in the same solution for that particular database. The repository library gets built, packed, and pushed to our nuget repository from our build server (Azure DevOps) using the dotnet pack command on 2.2 cli. I would like to include the dacpac from the database project as part of the repository nuget package so that releases from consumers of that package can deploy the dacpac changes. I plan on making sure that the version being overwritten is later than the package version on deployment.
I am able to add the dacpac file to the build output of the repository project by using a before build target
<Target Name="mydb" AfterTargets="Build">
<Exec Command="XCOPY /Y /R ..\db\bin\$(ConfigurationName)\mydb.dacpac $(TargetDir)" />
</Target>
My current issue is that although the file copies, it is not included in the resulting nuget file from
dotnet pack repository.csproj
I found the answer in the depths of MS documentation:
In addition to the target, I needed to add a Content to include the file that I just copied.
<ItemGroup>
<Content Include="$(TargetDir)\mydb.dacpac" >
<Pack>true</Pack>
<PackagePath>\dacpacs\</PackagePath>
</Content>
</ItemGroup>
https://learn.microsoft.com/en-us/nuget/quickstart/create-and-publish-a-package-using-visual-studio?tabs=netcore-cli
I'm trying to setup a library nuget package for .net core with the dotnet pack command, however, instead of just having a dll included in the nuget package, a content file from another references nuget is added (which makes the nuget file size 9.6MB instead of 59KB).
Is there a way to avoid getting files and content from other nuget packages in a nuget library project?
to reproduce:
Create a .net core library
Add Hl7.Fhir.Specification.STU3 nuget reference
run dotnet pack
The nuspec file in the newly created nuget package, will reveal that the specification.zip file is regarded as content that must be added.
I've tried testing with a custom nuspec file which is basicly a copy from the dotnet output, but without the content reference. The problem I see, is that the nuspec file contains a lot of references which must be maintained somehow.
Peter Wurzinger's suggestion worked for me. It's a shame he posted as a comment, rather than an answer, since he deserves the rep points. Anyway, this is my csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Hl7.Fhir.Specification.STU3" Version="0.96.0" ExcludeAssets="contentFiles" />
</ItemGroup>
</Project>
when I pack, the bin\Debug\test.1.0.0.nuspec file does not contain the specification.zip file elements that exists when I don't use ExcludeAssets.
I'm trying to use custom ruleset file form our nuget package. I've added to the build folder of the package .props file:
<Project>
<PropertyGroup>
<CodeAnalysisRuleSet>
$(MSBuildThisFileDirectory)..\My.Shared.ruleset
</CodeAnalysisRuleSet>
<RunCodeAnalysis>true</RunCodeAnalysis>
</PropertyGroup>
</Project>
Rule set file is in the package root folder, the paths are correct, it's adding import of the .props file into csproj file.
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\My.Shared.Rulesets.1.0.0.7118\build\My.Shared.Rulesets.props" Condition="Exists('..\packages\My.Shared.Rulesets.1.0.0.7118\build\My.Shared.Rulesets.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
...
But Visual Studio is not seeing correct rule set. When I open active rule set from References -> Analyzers, it's pointing to different file: MinimumRecommendedRules.ruleset and it's using rules from this file not my custom one.
Visual Studio Comunity 2017 Version 15.5.0
Project Target framework 4.6.1
Code Analysis is not working with ruleset from nuget package (from .props)
You should set the Rule set file in the content folder in the package folder, so that VS/MSBuild will add this file to your project, then VS\MSBuild could change the default MinimumRecommendedRules.ruleset to your My.Shared.ruleset.ruleset file.
For example, your NuGet source folder structure might look like this ("My.Shared.ruleset" is your package ID):
build
My.Shared.ruleset.props
content
My.Shared.ruleset.ruleset
where the contents of My.Shared.ruleset.props are something like the following:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<RunCodeAnalysis>true</RunCodeAnalysis>
<CodeAnalysisRuleSet>My.Shared.Rulesets.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
</Project>
Step to create nuget package:
Open NuGet Package Explorer, select new a package, Edit->Edit
Metadata, change Id to My.Shared.Rulesets->Save.
Content->Add->Content Folder->Add Existing file->Select your My.Shared.Rulesets.ruleset
Content->Add->Build Folder->Add Existing file->Select your My.Shared.ruleset.props->Save.
Then add this package to the test project:
Then you will find the active ruleset was changed to the one from nuget package.
Update for comment:
We were trying to avoid copying ruleset file to each project. Developers tend to change it and push to repository
If you do not want to copy ruleset file to each project, you just need to change the path in the .props file, but you should make sure the path is correct in the .props file, for example. I set the .ruleset file in the local path: D:\ShareFolder\My.Shared.Rulesets.ruleset, then move the .ruleset from the nuget package and change the path to D:\ShareFolder\My.Shared.Rulesets.ruleset in the .props.
Hope this helps.
Consider a large existing codebase with approx. 150+ solutions and 800+ C# projects. Many are unit tests written using NUnit. All those projects references "nunit.framework.dll" from a "lib" folder that is checked in. There is also a number of 3rd party assemblies in the "lib" folder which has corresponding NuGet packages.
I could manually open 150+ solutions and migrate each reference to to NuGet. However this is proving to be tedious and error prone. I wrote a C# console application to parse csproj files and identify which packages needs to be installed for the respective project. So I know that 300+ projects requires the NUnit package to be installed.
How to I programmatically automate the installation of a package in a solution, matching the exact same behavior as doing so manually within Visual Studio 2013? I looked everywhere, and only found an extension however, it doesn't perform a full install with dependencies etc.
Create a packages.config file with just an entry for the NUnit packages
package.config should look something like this check for correct package name, version and target info
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="nunit.framework" version="2.6.3" targetFramework="net45" requireReinstallation="true" />
</packages>
extend the utility you wrote to parse .csproj files to edit the csproj file as well and add the below tags
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
packages.config should be copied to all project folders; else if your projects are going to have the same reference you can choose to Add the packages.config as a link
<ItemGroup>
<None Include="$(SolutionDir)packages.config">
<Link>$(SolutionDir)packages.config</Link>
</None>
</ItemGroup>
Once this is done open the solution in visual studio and go to the NuGet console and enter the below
command; NuGet will resolve missing dependencies and add them.
update-package
You can use the following snippet:
Get-Project -All | foreach-object {IF (Get-Content $_.FullName | Select-String 'Reference Include="XXX"') {Install-Package XXX -ProjectName $_.FullName}}
Replace XXX with your desired package name and run the snippet in Tools->NuGet Package Manager->Package Manager Console from within Visual Studio.