Pack nuspec file during build in Visual Studio - visual-studio

I have a VS2017 solution with multiple projects that generates a cross-platform nuget-package. The package is generated by running nuget pack on my specific nuspec file. I would like to automatically performs this action during Visual Studio build instead of doing it manually each time. Is this possible?

Pack nuspec file during build in Visual Studio. Is this possible?
The answer is yes. You can simple add a pro-build or post-build event (Project->properties->Build Events) to generate nuget package with below command line:
nuget.exe pack "$(ThePathOfYour .nuspec)\TestPackNupsec.csproj.nuspec"
Note: You should add the nuget.exe as a system variable, so that you can use it anywhere without specifying the specific path of nuget, or you need specify the full path of nuget in that command line, like:
C:\Users\<UserName>\nuget.exe
With this command line in the build-event, we could generate Pack nuspec file during build in Visual Studio:

Related

Publish NuGet package without source code

I have a .NET project targeting .NET Standard 2.0. When compiled, this project yields an assembly in the ./bin/Debug/netstandard2.0 directory. I want to package this assembly into a NuGet package. However, I do not want to include the source code. In an attempt to do this, I currently am running the following from the command line:
dotnet build MyProject.csproj
dotnet pack --configuration Debug MyProject.csproj
dotnet nuget push bin/Debug/MyProject.1.0.0.nupkg --source https://nuget.pkg.github.com/my-organization --api-key [personal-access-token] --skip-duplicate --no-symbols true
The commands listed above successfully publish a NuGet package to my package registry. I can also successfully install this package via Visual Studio. However, when I attempt to compile the code, I get an error that says the namespace in the package cannot be found. I can see the package listed under the "dependencies/packages" node in Visual Studio. However, I cannot (or don't know how to) explore which namespaces are actually in it. But, when I right-click on it, and choose "Open Folder in File Explorer", I see the source code. I also see a *.nupkg file, a *.nupkg.sha512 file, and a .nupkg.metadata file.
My question is, how do I publish my NuGet package such that the source code is not included?
The dotnet pack would pack the code into a NuGet package. We recommend you use nuget.exe tool with .nuspec file, it could only pack your generated files.

How can I get MSBuild to restore any needed NuGet packages

I've looked at a ton of articles on this including here and here. None provide a solution that I can get to work.
I have everything turned on for automatically doing this in VisualStudio. But on our build machine we build the .sln files using MSBuild, not by opening up VisualStudio. So in MSBuild how can I best do this?
All of our .sln files are for VisualStudio 2019 and set to defaults on handling NuGet (I believe).
msbuild -t:Restore will restore nuget packages for projects with PackageReference nuget management format.
And your situation looks like packages.config nuget management format which you have used it.
If so, you only should use nuget restore to restore nuget packages with msbuild.exe.
1) first download nuget.exe cli from the website and config it. You can check this about it.
2) then use MSBuild command line, type
nuget xxx\xxx.sln restore
msbuild xxx\xxx.sln -t:build
Another is to use msbuild -t:restore with -p:RestorePackagesConfig=true.
msbuild xxx\xxx.sln -t:restore,build -p:RestorePackagesConfig=true

Generate 'project.lock.json' as generated by Visual Studio using command line for Xamarin.iOS portable class library - Mac

Summary: Mac - OSX
Project: Portable library for Xamarin.iOS project
I have added newtonsoft.json package, so when I open project in visual studio it generates project.lock.json and the build works fine. But when I try to run from command line it is not working.
Can you please suggest what command should I use to generate this file?
The project.lock.json file is created during the NuGet package restore. This will be done automatically by Visual Studio.
From the command line you can generate the project.lock.json by one of the following:
nuget restore /path/to/yoursolution.sln
msbuild /t:restore /path/to/yoursolution.sln
Both of the above will recreate the project.lock.json file. Then you can build the solution.

Automatic NugetPackage upload to Nuget feed

Is it possible to automatically upload a Nuget package to a feed when building a project using Visual Studio 2017 and .net core?
I can check the option to automatically generate the package, but I don't know how to automate the publish process.
Thanks!
Is it possible to automatically upload a Nuget package to a feed when building a project using Visual Studio 2017 and .net core?
The answer is yes. Since NuGet Command Line Interface (CLI) provides the full extent of NuGet functionality to install, create, publish, and manage packages.
https://learn.microsoft.com/en-us/nuget/tools/nuget-exe-cli-reference#push
We could add a target to execute the publish command line after Visual Studio generate package. In addition, when you check the option to automatically generate the package, you can notice that the target which generate the package is "GenerateNuspec" (MSBuild project build output verbosity->Detailed). So we could a target after the target "GenerateNuspec"
<Target Name="PushNuGetPackage" AfterTargets="GenerateNuspec">
Right click project->Select Edit projectname.csproj->Add below scripts:
<Target Name="PushNuGetPackage" AfterTargets="GenerateNuspec">
<Message Text="Push NuGet Package to NuGet Feed" Importance="high"></Message>
<Exec Command="D:\nuget.exe push $(ProjectDir)bin\Debug\AutoPushNuGetPackageTest.1.0.0.nupkg -Source D:\LocalServer"></Exec>
</Target>
Note: The exec command should be:
<Exec Command="PathOfYourNuGet\nuget.exe push PathOfYourPackage\PackageName.nupkg -Source NuGetFeedPath"></Exec>
With this target, Visual Studio will automatically upload a Nuget package to a feed when building a project using Visual Studio:

How to change the Resgen command line in a VS.NET project?

Having an issue with a ResGen call during a build of a .NET 2.0 project from within Visual Studio .NET 2010, this thread suggests:
Add this to your MSBUILD command-line:
/p:ResGenExecuteAsTool=true;ResGenToolArchitecture=ManagedIL;ResGenTrackerSdkPath="%programfiles(x86)%\Microsoft SDKs\Windows\v7.0A\bin\NETFX 4.0 Tools\x64"
I've searched for hours without success trying to find a way to do this from within my Visual Studio .NET project.
Therefore my question is:
How can I change the MSBUILD command line of a .NET 2.0 (class library) project in Visual Studio 2010?
I have no fear changing my ".csproj" file, I would prefer not to change MSBuild files that were shipped with Visual Studio, if this is possible.
Update and solution:
Thanks to Oded's comment and answer, I added the following two lines:
<ResGenTrackerSdkPath>%programfiles(x86)%\Microsoft SDKs\Windows\v7.0A\bin\NETFX 4.0 Tools\x64</ResGenTrackerSdkPath>
<TrackFileAccess>false</TrackFileAccess>
right inside the very first <PropertyGroup> section of my ".csproj" file (i.e. after line 3). This made the file compile successfully!
I now check and see whether it also runs successfully.
Like all properties in MSBuild, what you can pass in through the command line can be added in the project file in XML elements instead.
For example:
<ResGenTrackerSdkPath>%programfiles(x86)%\Microsoft SDKs\Windows\v7.0A\bin\NETFX 4.0 Tools\x64</ResGenTrackerSdkPath>
And:
<ResGenExecuteAsTool>true</ResGenExecuteAsTool>
etc...

Resources