3

I am currently working on a .NET Core library that I am going to use as a NuGet package in another project.

I have been able to successfully package the project using the "dotnet pack" command in the project directory, and upload that package to MyGet.

I would prefer to automate this process of pushing the NuGet package by using the "nuget push" command.

My issue is that the "scripts" property defined in the project.json file does not seem to be executed on pack or build. I expected that these scripts would be executed when the corresponding event occurs, but they seem to have no effect as I do not see anything output to console when I build, with or without he verbose tag.

I understand that MyGet is able to update a package feed based on a Git repository, but I would like to understand if there is some issue with executing a script currently using a project.json. Ideally I want to use the nuget push command after pack successfully executes.

Here is my project.json file :

{
  "version": "0.0.1",
  "scripts": {
    "postbuild": [ "echo build" ],
    "prebuild": "echo build",
    "postpack": "echo build",
    "postpublish": "echo build",
    "postrestore": "echo build",
    "prepack": "echo build",
    "prepare": "echo build",
    "prepublish": "echo build",
    "prerestore": "echo build"
  },

  "dependencies": {
    "NETStandard.Library": "1.5.0-rc2-24027"
  },

  "frameworks": {
    "netstandard1.5": {
    }
  },
  "buildOptions": {
    "allowUnsafe": false,
    "debugType": "portable",
    "emitEntryPoint": false,
    "xmlDoc": false
  },
  "commands": { },
  "packOptions": {
    "files": {
      "include": "%project:Directory%/bin/release/*.nupkg"
    }
  },
  "configurations": {
    "Debug": {
      "buildOptions": {
        "define": [ "DEBUG", "TRACE" ]
      }
    },
    "Release": {
      "buildOptions": {
        "define": [ ]
      }

    }
  }

}
Miek
  • 1,190
  • 7
  • 18

1 Answers1

8

RC2 replaced prebuild and postbuild with precompile and postcompile.

You can use the postcompile to automatically generate the nupkg and to push the package to a nuget server using

"scripts": {
    "postcompile": [
  "dotnet pack --no-build",
  "\"%project:Directory%\\..\\..\\nuget.exe\" push \"%project:Directory%\\bin\\%compile:Configuration%\\%project:Name%.%project:Version%.nupkg\" -source nugetserver -ApiKey key"
]
  }

This will automatically call the dotnet pack using the project.json file that exists in the project directory. Then it will push the nuget package to the specified nuget server.

Unfortunately there is no variable to specifiy the build configuration therefore in the above path you will have to manually change it when you switch between debug and release configuration.

The above uses %compile:Configuration% to specify the current build configuration.

The answer to the current build configuration comes from How to run scripts based on solution configuration in ASP.NET Core RC2

Visual Studio 2017

In Visual Studio 2017 you can use the dotnet nuget push command by editing the csproj file and using the following command

<Target Name="PushPackage" AfterTargets="Pack">
    <Exec Command="dotnet nuget push &quot;$(MSBuildProjectDirectory)\bin\$(Configuration)\$(AssemblyName).$(Version).nupkg&quot; -s nugetserver -k apikey" />
</Target>
DazFahy
  • 181
  • 9
  • How can you use this and get round multiple frameworks? If you have multiple frameworks, the postcompile runs after each framework's compilation, so the first one runs, packs (all frameworks but only first framework has been recompiled) and pushes, the second one runs, packs (all frameworks) but now won't push because that version package already exists... – Adam Jun 15 '16 at 12:45
  • I've got a number of libraries targeting net461 and netstandard1.5 frameworks and it is pushing the package. The only caveat to this is that I'm pushing to a local shared folder rather than to nuget, so I'm note sure what would happen if I was pushing to nuget. – DazFahy Jun 21 '16 at 11:04
  • You'll want to use a script file and test for the correct framework before running the nuget push. The context variable you're looking for is %compile:TargetFramework% or %compile:FullTargetFramework% I've got a full list of the context variables here: http://stackoverflow.com/a/38339463/1112558 – Erikest Jul 12 '16 at 21:54
  • Getting this error: The system cannot find the file specified C:\Program Files(x86)\MSBuild\Microsoft\VisualStudio\v14.0\DotNet\Microsoft.DotNet.Common.Targets and if I double click on it it opens the file and goes to this line: Any suggestions? – stibay Jul 14 '16 at 13:01
  • If anyone needs to create the package for the current configuration without publishing to the feed, use: "dotnet pack --no-build --configuration %compile:Configuration%" – miraco Jan 04 '17 at 11:02