How to run the target MSBuild package in Visual Studio 2017

My question is similar to this and this .

I want to pack the .Net Framework library in Visual Studio 2017 RC. In VS 2015 with the project.json build system, I was able to accomplish this by importing a custom goals file into a .xproj file. This target file took care of creating the nuspec file if it does not exist, then run the nuget package, copy the resulting packages to a local folder, etc.

Do I need to do something like this in 2017 (have not taken any serious efforts yet), or can I somehow include the package target in my .csproj file?

The docs here show only to launch the package target from the command line.

EDIT: I'm trying to use the following custom target referencing a Nuget 4.0 exe ​​from nightly collections ...

<Target Name="PackNugets" AfterTargets="Build"> <PropertyGroup> <NugetV4Path>$([System.IO.Path]::GetFullPath('path to nuget.exe'))</NugetV4Path> </PropertyGroup> <Exec Command="&quot;$(NugetV4Path)\nuget.exe&quot; pack &quot;$(MSBuildProjectDirectory)\$(PackageId).csproj&quot; -Symbols -OutputDirectory bin -Properties Configuration=Release"/> </Target> 

But I get the following error

 System.InvalidCastException: Unable to cast object of type 'System.String' to type 'NuGet.Frameworks.NuGetFramework'. at NuGet.ProjectManagement.NuGetProject.GetMetadata[T](String key) at NuGet.ProjectManagement.PackagesConfigNuGetProject..ctor(String folderPath, Dictionary`2 metadata) at CallSite.Target(Closure , CallSite , Type , Object , Dictionary`2 ) at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2) at NuGet.CommandLine.ProjectFactory.AddDependencies(Dictionary`2 packagesAndDependencies) at NuGet.CommandLine.ProjectFactory.ProcessDependencies(PackageBuilder builder) at NuGet.CommandLine.ProjectFactory.CreateBuilder(String basePath, NuGetVersion version, String suffix, Boolean buildIfNeeded, PackageBuilder builder) at NuGet.Commands.PackCommandRunner.BuildFromProjectFile(String path) at NuGet.CommandLine.PackCommand.ExecuteCommand() at NuGet.CommandLine.Command.ExecuteCommandAsync() at NuGet.CommandLine.Command.Execute() at NuGet.CommandLine.Program.MainCore(String workingDirectory, String[] args) 

Something related to one of the properties in csproj? Does NugetFramework describe the value of TargetFramework ?

I am targeting net452 in my csproj, in case this helps.

EDIT: This exception is really related to nuget trying to parse TargetFramework , but it is not clear if it works on my csproj or on the dependency ...

+5
source share
4 answers

EDIT: Recent updates for VS2017 have added the Pack action to the project’s context menu, so the action below can be changed as follows:

  • AfterTargets=Pack
  • Delete Exec Element
  • If you target multiple frameworks, you may need to insert \$(Configuration) after \bin in NugetPackages Include .

Well, this question solved it for me. For now, we should use dotnet instead of msbuild or nuget .

So my custom target in the imported .targets file becomes

 <Project ToolsVersion="15.0"> <Target Name="PackNugets" AfterTargets="AfterBuild"> <!-- Swap out nuget for dotnet and update the cli args --> <!-- Might actually be able to leave out the csproj path, since this target should run in the project directory. Test it first. --> <Exec Command="dotnet pack &quot;$(MSBuildProjectDirectory)\$(PackageId).csproj&quot; --no-build --include-symbols -o bin -c Release"/> <!-- If you want to copy to a local feed --> <ItemGroup> <NugetPackages Include="$(MSBuildProjectDirectory)\bin\$(PackageId).$(PackageVersion)*.nupkg"/> </ItemGroup> <Copy SourceFiles="@(NugetPackages)" DestinationFolder="path to local feed" /> </Target> </Project> 
+5
source

I wanted to know the same thing, so I went to the MSBuild files, namely: C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Sdks\NuGet.Build.Tasks.Pack\build and searched The goal is "Pack" . "Pack"

Also, I would like to refer to the general MSBuild command-line syntax here , but with a few examples:

 msbuild.exe -t:Pack -p:IncludeSymbols=true msbuild.exe -t:Pack -p:Configuration=Release -p:VersionSuffix="alpha" # or "-alpha" - not sure 

EDIT : A bit more research and work on my script, and I found this documentation on important properties.

+4
source

Note that dotnet pack or msbuild /t:Pack does not currently support .NET Framework projects. They work only with NETCore projects.

+1
source

You can even improve Ken G 's answer if you want to click nuget using this:

 <Target Name="PushNugetPackage" AfterTargets="Pack" Condition="'$(Configuration)' == 'Release'"> <Exec Command="nuget.exe push -Source &quot;mysource&quot; -ApiKey VSTS $(OutputPath)..\$(PackageId).$(PackageVersion).nupkg" /> </Target> 

It will work only after you select a package from the context menu, and the setting is released, so you will not push debug packages, remove this condition if you really do not need it

A source

0
source

Source: https://habr.com/ru/post/1263631/


All Articles