如何在Visual Studio 2017中运行MSBuild包目标

Ken*_*n G 8 c# msbuild nuget visual-studio-2017

我的问题是类似这个这个.

我想在Visual Studio 2017 RC中打包.Net Framework库.在带有project.json构建系统的VS 2015中,我能够通过将自定义目标文件导入.xproj文件来实现此目的.此自定义目标文件负责创建nuspec文件(如果该文件不存在),然后运行nuget pack,将生成的包复制到本地Feed位置等.

我是否需要在2017年做类似的事情(还没有认真努力)或者我能以某种方式在我的.csproj文件中启用包目标吗?

此处的文档仅显示从命令行运行pack目标.

编辑: 我正在尝试从夜间构建中引用Nuget 4.0 exe的以下自定义目标...

<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>
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误

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)
Run Code Online (Sandbox Code Playgroud)

与csproj中的一个属性有关?NugetFramework是指参考TargetFramework吗?

我的目标net452是我的csproj,以防万一.

编辑: 这个例外确实是关于nuget试图解析TargetFramework,但不清楚它是在我的csproj失败还是依赖...

Ken*_*n G 7

编辑: VS2017的最新更新已将Pack操作添加到项目上下文菜单,因此可以按如下方式更改以下操作:

  • AfterTargets=Pack
  • 删除Exec元素
  • 如果你的目标多框架,您可能需要插入\$(Configuration)\binNugetPackages Include.

好的,这个问题为我解决了.现在,我们必须使用dotnet而不是msbuildnuget.

因此,导入的.targets文件中的自定义目标变为

<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>
Run Code Online (Sandbox Code Playgroud)


Ada*_*itt 5

我想知道同一件事,所以我在MSBuild文件中摸索,即:C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Sdks\NuGet.Build.Tasks.Pack\build并寻找"Pack"目标。

除此之外,我会在这里参考一般的MSBuild命令行语法,但下面是一些示例:

msbuild.exe -t:Pack -p:IncludeSymbols=true
msbuild.exe -t:Pack -p:Configuration=Release -p:VersionSuffix="alpha" # or "-alpha" - not sure
Run Code Online (Sandbox Code Playgroud)

编辑:在我的方案上进行了更多的研究和工作,我发现了此重要属性的文档