簡體   English   中英

使用MSBuid.exe編譯項目,但未使用nuget軟件包安裝Visual Studio

[英]Compile project with MSBuid.exe but without visual studio installed using nuget packages

如何構建使用nuget通過未安裝Visual Studio的MSBuild在計算機上重新打包軟件包的解決方案?

我嘗試創建一個.nuget目錄並將命令行nuget.exe存儲在其中,但這無濟於事。 還是不可能?

謝謝!

因此,您有一組正在使用NuGet軟件包的項目,並且希望能夠在使用MSBuild構建之前或構建過程中還原這些軟件包。

基本方法是在構建項目之前為您的解決方案運行NuGet.exe restore 因此,執行此操作的一個簡單方法是擁有一個批處理文件,該文件在構建解決方案之前便已執行該操作。

如果您希望MSBuild進行程序包還原,我知道三種方法。 其中兩個使用MSBuild .targets文件運行該解決方案的NuGet.exe還原。 另一種方法是簡單地執行一個預構建步驟,為您的解決方案運行NuGet.exe還原。

  1. Visual Studio的“啟用NuGet程序包還原”。
  2. 跨平台NuGet還原由Daniel Cazzulino創建。
  3. 運行NuGet.exe還原的簡單預構建步驟。

請注意,NuGet團隊已棄用Visual Studio中的“啟用NuGet軟件包還原”菜單。 如果使用的NuGet軟件包具有自己的MSBuild .targets文件,則在構建過程中執行nuget軟件包還原的方法存在問題。 選項3也可能會出現此問題。

Visual Studio的“啟用程序包還原”將NuGet.targets文件添加到您的項目。 此.targets文件運行NuGet.exe來恢復您的NuGet包,方法是定義一個RestoreCommand,然后讓MSBuild執行它。 此NuGet.targets文件的某些部分如下所示。

  <RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)"  $(NonInteractiveSwitch) $(RequireConsentSwitch) -solutionDir $(PaddedSolutionDir)</RestoreCommand>

 <!-- We need to ensure packages are restored prior to assembly resolve -->
 <BuildDependsOn Condition="$(RestorePackages) == 'true'">
    RestorePackages;
    $(BuildDependsOn);
</BuildDependsOn>

<Target Name="RestorePackages" DependsOnTargets="CheckPrerequisites">        
    <Exec Command="$(RestoreCommand)"
          Condition="'$(OS)' != 'Windows_NT' And Exists('$(PackagesConfig)')" />

    <Exec Command="$(RestoreCommand)"
          LogStandardErrorAsError="true"
          Condition="'$(OS)' == 'Windows_NT' And Exists('$(PackagesConfig)')" />
</Target>

Daniel Cazzulino的程序包還原方法具有與NuGet.targets文件類似的Before。[解決方案文件名] .targets文件。 使用在解決方案生成之前運行的Before.YourSolution.targets文件的一個優點是,如果您的NuGet軟件包使用自己的.targets文件,則與其他兩個選項不會有相同的問題。

最后一種方法與其他兩種方法類似,但是沒有.targets文件,而是在運行NuGet.exe的項目中添加了預構建步驟。 這基本上與將NuGet.targets文件的某些內容移入項目相同。

<Target Name="BeforeBuild">
  <PropertyGroup>
    <NuGet>$(SolutionDir)\.nuget\NuGet.exe</NuGet>
  </PropertyGroup>
  <Exec Command="$(NuGet) restore -SolutionDirectory $(SolutionDir)" />
</Target>

暫無
暫無

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

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