【问题标题】:How to run MSBuild pack target in Visual Studio 2017如何在 Visual Studio 2017 中运行 MSBuild 包目标
【发布时间】:2017-01-31 19:18:50
【问题描述】:

我的问题类似于this 和this。

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

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

文档here 仅显示从命令行运行打包目标。

编辑: 我正在尝试以下自定义目标,该目标引用来自每晚构建的 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>

但我收到以下错误

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?

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

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

【问题讨论】:

    标签: c# msbuild nuget visual-studio-2017


    【解决方案1】:

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

    • AfterTargets=Pack
    • 删除 Exec 元素
    • 如果您针对多个框架,您可能需要在NugetPackages Include 中的\bin 之后插入\$(Configuration)。

    好的,this issue 为我解决了这个问题。现在,我们必须使用dotnet 而不是msbuild 或nuget。

    所以,我在导入的 .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>
    

    【讨论】:

    • 运行 dotnet pack -c Release 或根据您的环境更改 RELEASE。为我工作。
    • 有关最新版 Visual Studio 中 nuget 集成的更多详细信息,请访问此处:blog.nuget.org/20170316/…
    【解决方案2】:

    我也想知道同样的事情,所以我深入研究了 MSBuild 文件,即:C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Sdks\NuGet.Build.Tasks.Pack\build 并寻找 "Pack" 目标。

    除此之外,我会参考通用的 MSBuild 命令行语法here,但举几个例子:

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

    编辑:对我的场景进行更多研究和工作,我发现了 documentation 的重要属性。

    【讨论】:

    • 到目前为止,我可能只是要做我在 VS2015 上所做的事情,并使用自定义目标在项目根目录上运行 nuget pack,确保引用 v4 nuget。我曾短暂尝试从自定义目标调用 msbuild cli,但这似乎不起作用。
    • 所以我使用 nuget pack prerelease= msbuild pack 支持 prerelease 吗?
    【解决方案3】:

    请注意,dotnet pack 或 msbuild /t:Pack 目前不支持 .NET Framework 项目。他们只处理 NETCore 项目。

    【讨论】:

    • 可能不受支持,但dotnet pack 确实适用于 .NET Framework 项目。
    • 我希望通过 msbuild 打包一个 .net 类库目标 = .net 4.6。 “pack”目标不存在,所以我可能需要返回到 .nuspec 文件和 nuget.exe
    • @MikeJansen 如果它是一个基于 .NETCore 且目标 framework = net46 的项目,那么您可以使用 msbuild 打包一个 .NET 框架项目的唯一方法。您可以执行文件 -> 新建项目 -> .NET Core 类库,然后编辑 csproj 以获得目标框架 net46。然后,您甚至可以右键单击项目并使用 VS 打包。
    【解决方案4】:

    如果你想使用这个来推送 nuget,你甚至可以改进 Ken G answer:

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

    它只会在你从上下文菜单中选择 pack 并且配置为 Release 后运行,所以你不要推送调试包,如果你真的不需要它,请删除该条件

    Source

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-04
      • 2017-04-01
      • 2019-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-19
      • 1970-01-01
      相关资源
      最近更新 更多