簡體   English   中英

如何在Visual Studio 2017中運行MSBuild包目標

[英]How to run MSBuild pack target in 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>

但是我收到以下錯誤

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或依賴...

編輯: VS2017的最新更新已將Pack操作添加到項目上下文菜單,因此可以按如下方式更改以下操作:

  • AfterTargets=Pack
  • 刪除Exec元素
  • 如果您要定位多個框架,則可能必須在NugetPackages Include\\bin之后插入\\$(Configuration)

好的, 這個問題為我解決了。 現在,我們必須使用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>

我想知道同樣的事情,所以我去了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

編輯 :對我的場景進行更多的研究和工作,我找到了重要屬性的文檔

請注意, dotnet packmsbuild /t:Pack目前不支持.NET Framework項目。 他們只工作NETCore項目。

如果你想用這個推動nuget,你甚至可以改進Ken G的答案

<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后運行,因此您不要推送調試包,如果您確實不需要它,請刪除該條件

資源

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM