Nuget包还原 - 将参数传递给MsBuild c#API

时间:2013-03-20 12:25:41

标签: msbuild nuget

我希望为MSBuild C#api编写一个包装器。我的构建工作正常,但由于一些奇怪的原因,即使我传入参数,我也很难让Nuget包恢复运行。

我在运行 NETWORK SERVICE

的服务中运行此功能

The command ""..\.nuget\nuget.exe" install "C:\BuildTemp\application1\packages.config" -source "" -RequireConsent -o "..\packages"" exited with code 1.

我是否正确地传递了它们?

var pc = new ProjectCollection();
var buildProperties = new Dictionary<string, string>
{
    {"Configuration", "Release"},
    {"Platform", "Any CPU"},
    {"OutputPath", _outputPath},
    {"EnableNuGetPackageRestore", "true"}
};

var buildParameters = new BuildParameters(pc);
var buildRequest = new BuildRequestData("C:\myapplication.csproj",
                                        buildProperties,
                                        null,
                                        new[] { "Clean", "Rebuild" },
                                        null);

更新:这似乎适用于某些环境而非其他环境。为什么会这样?

1 个答案:

答案 0 :(得分:3)

所以我在本地机器上测试了这个命令:

.\.nuget\NuGet.exe install Akavache\packages.config -source "" -RequireConsent -o packages

我收到错误:

  

无效的URI:无法确定URI的格式。

我认为这与这一点有关:

-source ""

因为我可以放弃该值并让它再次运行而没有错误。

这引出了一个问题,包源定义在哪里?

NuGet.targets file内,有一个这样的部分:

<ItemGroup Condition=" '$(PackageSources)' == '' ">
    <!-- Package sources used to restore packages. By default, registered sources under %APPDATA%\NuGet\NuGet.Config will be used -->
    <!-- The official NuGet package source (https://nuget.org/api/v2/) will be excluded if package sources are specified and it does not appear in the list -->
    <!--
        <PackageSource Include="https://nuget.org/api/v2/" />
        <PackageSource Include="https://my-nuget-source/nuget/" />
    -->
</ItemGroup>

所以我建议当你说它“在某些环境而不是其他环境中”时,你在服务帐户的%APPDATA%\NuGet\NuGet.Config没有配置文件。

您可以尝试将源代码管理中的Nuget.targets文件中的此部分更改为:

<ItemGroup Condition=" '$(PackageSources)' == '' ">
    <PackageSource Include="https://nuget.org/api/v2/" />
</ItemGroup>

看看是否可以解决其他环境中的问题?