Automatic NugetPackage upload to Nuget feed - visual-studio

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:

Related

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

Xamarin vstool build cannot find NuGet MSBuild targets

Background
I recently converted my Xamarin.Forms app from PCL to Net Standard format.
All of my projects now use PackageReference in the csproj file. Which means no more package.config or package.json.
We use TFS 2015 to build, sign, package our .ipa and .apk files. After conversion, the default MSBUILD build steps do not work as they look for mdtool and the new Visual Studio has vstool instead. So, I updated the build steps to use new tools via command line.
All my projects are NetStandard now (including iOS and Android).
Issue
I can successfully restore NuGet packages using restore MySolution.sln -force on Mac build server. But when I run vstool build MySolution.sln after that, I get this error:
error: NuGet packages need to be restored before building. NuGet
MSBuild targets are missing and are needed for building. The NuGet
MSBuild targets are generated when the NuGet packages are restored.
I am able to successfully run the nuget restore and vstool build locally on the build machine. But only when TFS runs the command via agent, it shows that error message.
Setup
Builds: TFS 2015 on Mac agent running Visual Studio 7.5
According to the error and your description, you need also check if your build agent has corresponding capability to support vsbuild.
Take a look at this related question MacOS - Visual Studio Support and give a try with this workaround:
As a work around we set the Xamarin.iOS variable manually in the build
agent and changed the mdtool path in the Xamarin iOS Build step to
"/Applications/Visual Studio.app/Contents/MacOS/vstool".
Besides you could also try to use the suggestion from Matt in the comment above.
Ok. I was finally able to get a successful iOS build on Mac server. This is the setup that works,
Using PackageReference in iOS .csproj
No package.config, project.json, or AssemblyInfo.cs file.
Running nuget restore .sln before building the iOS project.
Build solution using <path-to-vstool>\vstool build .sln -c:<configuration>
Now, I am working on the Windows machine for Android setup. Once I have that working, I will post my findings here.

Pack nuspec file during build in 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:

Build VS 2015 extension on build server without VS installed?

Is it possible to build a Visual Studio 2015 extension project on a build server (TeamCity agent) without Visual Studio installed? What kind of SDK do we need to install?
At the moment we receive the following error message:
error MSB4019: The imported project "C:\Program Files (x86)\MSBuild\Microsoft\Portable\v4.6\Microsoft.Portable.CSharp.targets" was not found. Confirm that the path in the declaration is correct, and that the file exists on disk.
So there is definitely some kind of SDK missing.
Microsoft.VSSDK.BuildTools
Contains targets and tools to enable the building of managed VSIX
projects without VSSDK MSI installed. Only for VS 2015 and onwards
Additional packages that may be of interest:
https://www.nuget.org/profiles/VisualStudioExtensibility
Using #weir's answer almost worked - the project built successfully, but it failed to produce a VSIX container at the end. For some reason the Nuget package hadn't added the necessary Import to the .csproj to bring in the VsSDK.targets, so the VSIX targets were missing and never got executed.
Here are the steps which worked for me:
Edit the VSIX project .csproj file, and remove Import Project="$(VSToolsPath)\VSSDK\Microsoft.VsSDK.targets" Condition="'$(VSToolsPath)' != ''" />. This will fail on the build server where the VSSDK doesn't exist in the VSToolsPath.
In the VS2015 IDE, open the Nuget Package Manager for the project, and install Microsoft.VSSDK.BuildTools (I used v14.3.25407)
Back in the .csproj file, find the import which the Nuget package added, e.g. <Import Project="..\packages\Microsoft.VSSDK.BuildTools.14.3.25407\build\Microsoft.VSSDK.BuildTools.targets" .../> and add another one below it for the VsSDK.targets file (inside the tools directory), <Import Project="..\packages\Microsoft.VSSDK.BuildTools.14.3.25407\tools\vssdk\Microsoft.VsSDK.targets" .../>
It looks like you have to install the Portable Library Tools on the build agent. You can download them from the VS Gallery and install them without having VS on the build agent using the following parameter /buildmachine.
Download Microsoft Build Tools 2015

Does nuget still require these entries in Visual Studio projects?

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.

Resources