如何在Visual Studio 2017中运行MSBuild Pack目标 [英] How to run MSBuild pack target in Visual Studio 2017

查看:138
本文介绍了如何在Visual Studio 2017中运行MSBuild Pack目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题类似于.

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

I want to package a .Net Framework library in Visual Studio 2017 RC. In VS 2015 with project.json build system, I was able to accomplish this by importing a custom targets file into the .xproj file. This custom targets file took care of creating the nuspec file if it didn't exist, then running nuget pack, copying the resulting packages to local feed location, etc.

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

Do I need to do something similar in 2017 (haven't made a serious effort yet) or can I somehow enable the pack target in my .csproj file?

仅在此处显示从命令行运行包目标.

The docs here only show to run the pack target from the command line.

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

I'm trying the below custom target referencing a Nuget 4.0 exe from the nightly builds...

<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>

但是我收到以下错误

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)

与csproj中的属性之一有关吗? NugetFramework是否引用TargetFramework?

Something to do with one of the properties in the csproj? Does NugetFramework refer to the TargetFramework?

如果有帮助,我将在csproj中定位net452.

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

该异常确实与nuget尝试解析TargetFramework有关,但是尚不清楚它是在我的csproj还是在依赖项上失败了.

That exception is indeed about nuget attempting to parse the TargetFramework, but it is not clear whether it is failing at my csproj or at a dependency...

推荐答案

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

The latest updates to VS2017 have added the Pack action to the project context menu, so the below action can be changed as follows:

  • AfterTargets=Pack
  • 删除Exec元素
  • 如果要针对多个框架,则可能必须在NugetPackages Include中的\bin之后插入\$(Configuration).
  • AfterTargets=Pack
  • Remove the Exec element
  • If you are targeting multiple frameworks, you may have to insert a \$(Configuration) after the \bin in the NugetPackages Include.

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

Ok, this issue solved it for me. For now, we have to use dotnet instead of msbuild or nuget.

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

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>

这篇关于如何在Visual Studio 2017中运行MSBuild Pack目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆