How to get version number in post-build event

后端 未结 4 2148
遥遥无期
遥遥无期 2021-02-01 22:44

I want to use post-build event to automatically create a nuget package and then copy it to a shared folder on our network, something like this (the version number 1.0.0.0. is sp

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-01 23:08

    Try the following:

    1) Rename you .nuspec to match the .csproj name:

    If your project is: MyLib.csproj

    Rename MyLib.nuspec to MyLib.csproj.nuspec

    2) Modify your .nuspec file as follows:

    
      
        $version$
        $author$
        $id$
        $id$
        false
        $description$
        $description$
      
    
    

    Here, the job is done by Nuget's "$version$" token (and others)

    Take a look to this Nuget article for more tokens and information.

    3) Use the following Post Build Event in every project you put a .csproj.nuspec file (please remove the Enter keys):

    mkdir "$(SolutionDir)GeneratedPackages"
    
    "$(SolutionDir).nuget\nuget.exe" pack "$(ProjectPath)" 
    -OutputDirectory "$(SolutionDir)GeneratedPackages" 
    -basepath "$(SolutionDir)$(ProjectName)\$(OutDir)"
    

    If we have a solution C:\MySolution\MyLib.sln and the project at c:\MySolution\MyLib\MyLib.csproj, our .nuspec file should be c:\MySolution\MyLib\MyLib.csproj.nuspec and the Post Build Event Command would look like:

    mkdir "C:\MySolution\GeneratedPackages"
    
    "C:\MySolution\.nuget\nuget.exe" pack "C:\MySolution\MyLib\MyLib.csproj" 
    -OutputDirectory "C:\MySolution\GeneratedPackages" 
    -basepath "C:\MySolution\MyLib\bin\debug\"
    

    Fix the path to the nuget.exe. In my case I am using the Nuget "auto-regenerate missing dependencies on build", and I have a local copy in the solution, under the .nuget folder.

    Please note that we're not calling nuget with the .nuspec file, we're calling it with the .csproj directly!!

    This strategy will also include all the Project dependendencies declared in the packages.xml that Nuget creates when you install packages. (automatically, every time you build)

    And you're done! Now change the Assembly and File Version in

    Project > Properties > Assembly information

    IMPORTANT: If you set the $author$ and/or $description$ tokens, fill also the Assembly's Company and Description fields here. If you have troubles, remove the $author$ and/or $description$ tokens and try to compile again.

    and build your Project, you'll see in /GeneratedPackages folder the MyLib.X.X.X.X.nupack with the version you set in the Project Properties. If you set dependencies, you'll see that it has been automatically included as Package Dependencies.

    We are using this strategy with a Jenkins CI server to build automaticaly the solution and copy the packages from the GeneratedPackages to our custom NugetServer.

    You can also complete the Post Build Event with a

    nuget push
    

    To send the generated package to a Nuget Server

    ISSUE IN RELEASE:

    As mentioned in comments, there is an issue that don't allow Nuget.exe to generate the packages in RELEASE mode.

    Do this in order to fix it:

    For every project with this script:

    1. Project Properties > Build
    2. In the "Configuration" dropdown choose "All configurations"
    3. In the "Ouput" section, change the "Output path:" field, for example, I am currently using "bin\" (without cuotes)

提交回复
热议问题