I am trying to unpack the nuget package that gets created during the build.
My Directory.Build.props file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<RestorePackagesPath>C:\packages</RestorePackagesPath>
</PropertyGroup>
<PropertyGroup>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageOutputPath>C:\LocalNuGetPackages</PackageOutputPath>
</PropertyGroup>
<PropertyGroup>
<Version>1.0.0.1</Version>
</PropertyGroup>
<Target Name="UnPack" AfterTargets="Pack">
<Exec Command="nuget install $(PackageId) -Version $(PackageVersion) -Source C:\LocalNuGetPackages -OutputDirectory C:\packages" />
</Target>
</Project>
But this gives a different directory structure as Visual Studio is doing it.
Visual Studio produces the following directories
C:\packages\$(PackageId)\$(PackageVersion)\
But the used command (nuget install) produces
C:\packages\$(PackageId).$(PackageVersion)\
Is there a way to call the Visual Studio "internal" nuget to get the same directory structure, or am I missing an argument that enables this structure?
Currently I am using the nuget.exe from here https://www.nuget.org/downloads which I have added to PATH in my system variables.
Side note, I am not trying to install the NuGet package to any project, I am just looking to unpack it like Visual Studio does.
That is designed by that. global nuget caches always make the nuget path like package_id/version/xxx. That's the way VS IDE unpacks NuGet packages into the global cache mechanism.
And when you use nuget install or packages folder under the solution folder by packages.config, it actually likes package_id.version/xxx.
This difference is not what we can handle and can belong to the design itself. So I have reported the issue to the Team.
You could vote it and add any comments if I did not describe it in detail. Hope it could solve your confusion as soon as possible.
As was pointed out in the github issue created by #Perry the command I was actually looking for was
nuget add "C:\LocalNuGetPackages\castle.core.4.4.0.nupkg" -Source C:\packages -expand
Instead of
nuget install castle.core -Version 4.4.0 -Source C:\LocalNuGetPackages -OutputDirectory C:\packages
The add command lacks the ability to fetch the nuget package from a "source" (eg. NuGet.org or C:\LocalNuGetPackages) and you already need the *.nupkg downloaded and ready to unpack.
But this lack of ability is irrelevant in my situation, since upon build I am creating the *.nupkg and they are ther for me to unpack.
Don't get confused with the argument being called -Source in the add it is actually the equivalent of -OutputDirectory for the install command.
So for other users/cases it would be desirable to have the install command do the different directory structure, but for me add was what I wanted.
Related
I apologize for the vague title, but I am not sure how to phrase it.
I am working with a "custom" appsettings.json folder. The json file lives in the class library that is using it. I want to create a nuget package to install this class library but also make sure that appsettings.json is copied into the correct directory (if I am installing it in a console app, the build output directory).
I have seen one "answer"
How can I set the 'copy to output directory' property in my nuspec file?
but I am using VS 2019 and .NET Standard 2.0. I am pretty frustrated so any help (even if told not possible) is appreciated! Thanks in advance.
If you just want to install this nuget package only on net core projects, you could just add these node under the net standard project's csproj file:
<ItemGroup>
<None Update="xxx\appsettings.json" Pack="true" PackagePath="contentFiles\any\any;content">
<PackageCopyToOutput>true</PackageCopyToOutput>
</None>
</ItemGroup>
Then, repack the net standard project into a nuget package, before install it under a new project, you should clean nuget caches first.
If you want to install this nuget package into a net framework project, you should try to use <package_id>.props file on the net standard2.0 lib project.
Please try the function under this link.
How to easily associate a new project and already downloaded NuGet packages?
An example scenario:
I created a Visual Studio solution and project, named mylib. And I installed Nuget packages, like C++ boost library. I can use the boost library right away without setting header/linker directories manually. This is very convenient.
Now I create a new project (or add an existing project) under the same solution, named executable. I also want to use the boost library in this project.
Unfortunately, there is no graphical or IDE interface to link the dependency for the new project.
The above picture shows NuGet packages are installed, but newly added project executable still don't have links.
To correct this, I have to manually modify the project (e.g., .vcxproj) XML file. I copied from mylib and pasted it to executable.
<ImportGroup Label="ExtensionTargets">
<Import Project="packages\boost.1.65.1.0\build\native\boost.targets" Condition="Exists('packages\boost.1.65.1.0\build\native\boost.targets')" />
<Import Project="packages\boost_regex-vc141.1.65.1.0\build\native\boost_regex-vc141.targets" Condition="Exists('packages\boost_regex-vc141.1.65.1.0\build\native\boost_regex-vc141.targets')" />
</ImportGroup>
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('packages\boost.1.65.1.0\build\native\boost.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\boost.1.65.1.0\build\native\boost.targets'))" />
<Error Condition="!Exists('packages\boost_regex-vc141.1.65.1.0\build\native\boost_regex-vc141.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\boost_regex-vc141.1.65.1.0\build\native\boost_regex-vc141.targets'))" />
</Target>
It works then.
Or, uninstalling already downloaded NuGet packages and reinstalling them also works. But that's obviously not a good solution.
I'm wondering if there is a nice way to re-associate dependency between already downloaded NuGet packages and projects. I was unable to find such feature in the project property pages in Visual Studio 2017.
To correct this, I have to manually modify the project (e.g., .vcxproj) XML file. I copied from mylib and pasted it to executable
According to your description, that seems the package boost has not been installed properly to the project executable. So you can use the NuGet command line in the Package Manager Console:
Update-Package -reinstall
to force reinstall the package to the executable project.
I need my NuGet package to add a file to a project and set its "Copy to Output Directory" flag.
So far, I'm using install.ps1 script, as seen in the answer by #workabyte to Set content files to “copy local : always” in a nuget package.
But I've understood that NuGet 3.3 and newer has a native support using its contentFiles model.
My understanding was that I create a directory structure like:
contentFiles\
any\
any\
image.png
And then in my .nuspec:
<?xml version="1.0"?>
<package>
<metadata minClientVersion="3.3.0">
...
<contentFiles>
<files include="**/*.png" buildAction="None" copyToOutput="true" />
</contentFiles>
</metadata>
</package>
When the package is compiled, the image.png with its directory structure is added to the .nupkg.
But, when I install the package in Visual Studio (2017), the image.png is not even added to the project.
What am I doing wrong?
The contentFiles feature is supported only when using the PackageReference "style" of referencing NuGet packages. It does not work for projects using packages.config (not that there is no migration path at the time of writing other than uninstalling all packages, changing the type and installing all of them again).
See NuGet's blog post "NuGet is now fully integrated into MSBuild", especially the section "What about other project types that are not .NET Core?" for instructions on how to set the preferred way of referencing NuGet packages.
I'm using .NET core on a mac with Visual Studio Code. i'm trying to install NewtonSoft.Json to use as a Json parser. The command I use is:
nuget install CoreCompat.NewtonSoft.Json -Pre
I use this command in my top level project folder. This leaves me with two problems. Firstly, and most glaring, I am still not able to use the package in my code.
using NewtonSoft.Json;
will not compile. Secondly, although this does download the package, it downloads a whole bunch of other stuff too - files like System.Threading, and puts them in my project directory. There are about 40 of these extra files. I already have these dependencies (which I'm assuming they must be) in my .nuget folder (and I'm able to include them in any project). I don't want to clutter up my project folder. How do I properly use nuget to install this package?
The command you want is
dotnet add package NewtonSoft.Json
This will add the following to your csproj file, which you could also do manually.
<ItemGroup>
<PackageReference Include="NewtonSoft.Json" Version="10.0.2" />
</ItemGroup>
Then you can use dotnet restore, dotnet build and friends to continue developing.
I also believe that you didn't mean to use the CoreCompat. prefixed package as this is not the original JSON.net library.
I've noticed that new .csproj projects without these nuget directives appear to work fine.
Are these a relic of the past that can be removed from projects?
I scanned the nuget FAQ but did not find anything related to this.
I'm running Visual Studio 2013 and also a recent build of Xamarin Studio.
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>
Those particular targets are added if you have used the Enable NuGet Package Restore menu option. In this case these targets can be removed. Visual Studio will automatically restore NuGet packages, if you have a recent version of NuGet installed, without these extra targets. This menu option has been deprecated by the NuGet team and I believe it will be removed at some point.
Note that if you are building your project on a build server you will need to create an extra step to restore the NuGet packages, using NuGet.exe restore, that would have automatically been restored when the solution was built on the build server by MSBuild.
Also note that NuGet will add very similar targets if you add a NuGet package that includes its own MSBuild .targets file, such as Microsoft.Bcl.Build. The project will work without the EnsureNuGetPackageBuildImports Target in this case, but would need the Import element that imports the MSBuild .targets file. Leaving the EnsureNuGetPackageBuildImports target in the project does give you a perhaps more useful error message as to why a build may be failing if the NuGet package is not restored.