简体   繁体   中英

How to run 'nuget add' as a post-build event in Visual Studio?

I have added a command to the AfterBuild section of my project's .csproj file which automatically creates a NuGet package if it is a Release configuration. This part, as specified in the code snippet below, is working well.

<Target Name="AfterBuild" Condition=" '$(Configuration)' == 'Release'">
    <Exec Command="nuget pack $(ProjectFileName) -IncludeReferencedProjects -Prop Configuration=Release"></Exec>
</Target>

I would now like to add an additional nuget add $(NugetFileName) -source c:\\NugetLocal command to copy the NuGet package to my local repository. Unfortunately, the $(NugetFileName) macro does not exist. I could use the $(TargetName) macro combined with .nupkg but the package name includes the assembly version number, for which it doesn't seem there's a handy macro. Is there a way to do this without using MSBuild scripts?

There is an answer to a previous question which shows how to expose the version number in the after build event. Combining that with what you had already, you can execute the nuget add command with the version number included:

<Target Name="AfterBuild" Condition=" '$(Configuration)' == 'Release'">
    <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
        <Output TaskParameter="Assemblies" ItemName="AssemblyVersion" />
    </GetAssemblyIdentity>
    <Exec Command="nuget pack $(ProjectFileName) -IncludeReferencedProjects -Prop Configuration=Release"></Exec>
    <Exec Command="nuget add $(TargetName).%(AssemblyVersion.Version).nupkg -Source C:\NugetLocal"></Exec>
</Target>

In Visual Studio 2019 has been added the automatic creation of the Nuget Package when building the Project, so you don't have to create it in a post build event. Adding the package to the local repository it's just a line of code in the .csproj file

<Target Name="NugetAdd" AfterTargets="Pack">
    <exec command="nuget add $(OutputPath)$(PackageId).$(PackageVersion).nupkg -source d:\NuGetLocal" />    
</Target>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM