Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create nuget package with msbuild without rebuilding project?

I would like to setup a CI/CD pipeline with build, test and deploy stages. I can build my project in build stage with

msbuild src\MyProject.csproj /t:Restore 
msbuild src\MyProject.csproj /p:Configuration=Release /p:OutputPath=../BuildOutput

Next I will build and run tests against ..\BuildOutput\MyProject.dll that has already been built.

msbuild tests\MyProject.Tests.csproj /t:Restore
msbuild tests\MyProject.Tests.csproj /p:Configuration=Release /p:OutputPath=../BuildOutput /p:BuildProjectReferences=false
vstest.console BuildOutput\MyProject.Tests.dll

Up to this point it seems to work.

Now I would like to generate nuget package. I can call:

msbuild src\MyProject.csproj /t:Pack /p:Configuration=Release /p:OutputPath=../BuildOutput /p:VersionPrefix=1.2.3

And that would create MyProject.1.2.3.nupkg in BuildOutput folder. However it re-builds that project.

I'm looking something similar to dotnet cli.

dotnet pack --no-build

But I cannot use dotnet because my project has a COM reference.

I also looked into Nuget.exe, but it seems to throw an error when I call nuget pack

Unable to cast object of type 'System.String' to type NuGet.Frameworks.NuGet.Frameworks1051960.NuGetFramework'.

Does msbuild have a property that can skip build?

like image 721
Siim Haas Avatar asked Oct 27 '17 11:10

Siim Haas


People also ask

Does MSBuild restore NuGet packages?

msbuild -t:Restore will restore nuget packages for projects with PackageReference nuget management format.

What is restore in MSBuild?

MSBuild -t:restore (which nuget restore and dotnet restore use with . NET Core projects), restores packages referenced in the project file as follows: Read all project to project references. Read the project properties to find the intermediate folder and target frameworks. Pass MSBuild data to NuGet.


1 Answers

It depends on whether this is acceptable in your workflow, but you could use the <GeneratePackageOnBuild> property in a <PropertyGroup> of your .csproj file to get the package whenever you build your project. In case your tests do not go through, you can then discard the .nupkg file, but at least there is no additional rebuild for getting the NuGet package.

There is a more complete example of how to use this setting at the end of the article Easily supporting multiple target frameworks (TFMs) with VS2017 and Nuget1, but an exemplary very simple snippet would look like this:

<PropertyGroup>
    <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
</PropertyGroup>

1: While the original link does not report a 404, it appears as blank in October 2021. The same article can, however, be found on another URL.

like image 190
O. R. Mapper Avatar answered Nov 05 '22 23:11

O. R. Mapper