【问题标题】:Cake build script fails in Azure Devops with mixed .NET Core and framework solution使用混合 .NET Core 和框架解决方案的 Azure Devops 中的 Cake 构建脚本失败
【发布时间】:2019-08-18 11:54:04
【问题描述】:

我的 .NET 服务器项目有一个 Cake 构建脚本,它需要在我们的新 Azure DevOps 管道上构建。该脚本在本地工作,但不适用于 Azure DevOps。这可能是因为我有一个基于 .NET Core 2.1 的 unit tests 项目 (xUnit) 而解决方案中的所有其他项目都是 .NET Framework 4.6.1(我们刚刚在 xUnit 项目中添加我唯一的选择是将其添加为 .NET Core 项目)。

我编写了可以在本地运行的 Cake 脚本。它看起来像这样:

// Always lock down versions of tools so that we can ensure
// a tool works before upgrading instead of auto-upgrading.
#tool "nuget:?package=xunit.runner.console&version=2.4.1"
#tool "nuget:?package=ReportUnit&version=1.2.1"

var target = Argument("target", "Build");
var configuration = Argument("configuration", "Dev");
var solution = @".\MyCompany.MyProduct.IntegrationLayer.sln";
var report = Directory(@".\reports");
var xunitReport = report + Directory("xunit");

Task("Clean")
    .Does(() =>
{
    Information("Cleaning out directories {0} for {1}...", configuration, solution);
        CleanDirectories("./**/bin/" + configuration);
        CleanDirectories("./**/obj/" + configuration);
});

Task("Restore")
    .IsDependentOn("Clean")
    .Does(() =>
{
    // This can restore both .NET Core and Framework NuGet packages.
    Information("Restoring Nuget packages for {0}...", solution);
    DotNetCoreRestore();
});

Task("Build")
    .IsDependentOn("Restore")
    .Does(() =>
{
    // If we change all the projects and solution to .NET Core, then
    // we will need to change this build call to DotNetCoreBuild().
    //
    // Because we have a mixed solution (unit tests are .NET Core and all
    // other projects are .NET Framework), we need to still use MSBuild.
    Information("Building solution {0} using the {1} configuration...", solution, configuration);
    MSBuild(solution, new MSBuildSettings {
        Configuration = configuration
    });
});

Task("UnitTests")
    .IsDependentOn("Build")
    .Does(() =>
{
    Information("Unit testing solution {0} using the {1} configuration...", solution, configuration);
    var projects = GetFiles("./UnitTests/*.csproj");
    foreach(var project in projects)
    {
        DotNetCoreTest(
            project.FullPath,
            new DotNetCoreTestSettings()
            {
                Configuration = configuration,
                NoBuild = true
            });
    }
});

Task("UnitTestsOnly")
    .Does(() =>
{
    Information("Unit testing solution {0} using the {1} configuration...", solution, configuration);
    var projects = GetFiles("./UnitTests/*.csproj");
    foreach(var project in projects)
    {
        DotNetCoreTest(
            project.FullPath,
            new DotNetCoreTestSettings()
            {
                Configuration = configuration,
                NoBuild = true
            });
    }
});

RunTarget(target);

现在,当我在 Azure DevOps 中运行此程序时,每个项目都会出现 10 个错误,类似于以下示例:

"D:\a\1\s\PPIL\MyCompany.MyProduct.IntegrationLayer.sln" (Build target) (1) ->
"D:\a\1\s\PPIL\MC.MP.IL.DatabaseDataReset\MC.MP.IL.DatabaseDataReset.csproj" (default target) (11) ->
  D:\a\1\s\PPIL\MC.MP.IL.DatabaseDataReset\MC.MP.IL.DatabaseDataReset.csproj(123,5): error : This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is ..\packages\Microsoft.VisualStudio.SlowCheetah.3.1.66\build\Microsoft.VisualStudio.SlowCheetah.targets.

我不确定到底是什么问题。这可能是因为我们有一个混合项目。可能是因为文件结构。我不知道需要在 Cake 命令中设置哪些选项或设置以进行构建或恢复。也可能是当我在本地运行它时,我在与 Cake 脚本相同的文件夹中运行它,但它似乎在我的存储库的根文件夹中运行,这是该解决方案的解决方案文件夹的上一个文件夹。

我的脚本哪里出错了?我的恢复步骤似乎很好,只是它只恢复了单元测试项目(这确实取决于所有其他项目)。是还原吗?构建或还原中是否缺少我的设置?

【问题讨论】:

  • 您是否可能没有对 .Net 框架依赖项进行 Nuget 恢复?看起来你的“恢复”任务只是恢复 .Net Core 包......它也可以解释为什么它“在你的机器上工作” - 你已经安装了所有依赖项 - 也许你可以在干净的 VM 上尝试

标签: .net .net-core azure-devops azure-pipelines cakebuild


【解决方案1】:

所以答案是我已经获取了所有项目文件并在每个文件上运行“NuGetRestore”方法不包括 .NET Core UnitTests 项目。然后我可以运行 DotNetCoreRestore 方法来恢复 .NET Core UnitTests 项目。

我还发现我应该清理包目录,因为这是一个好习惯。

【讨论】:

  • 我会在 6 小时内...网站允许我这样做。 ;)
  • 你能提供更详细的答案吗?包含来自 azure devops 的屏幕截图会很有帮助。
【解决方案2】:

正如您在 How to restore only a specific solution folder with an MSBuild command 中所见,您可以使用特定的 .proj 文件来区分这些项目并非常轻松地使用它们。

但如果您有 Visual Studio 2019,我建议您使用以下命令毫无问题地恢复所有项目:

MSBuildSettings settings = new MSBuildSettings
{
    ArgumentCustomization = args =>
        .Append("-p:RestoreUseSkipNonexistentTargets=false")
        .Append("-p:RestorePackagesConfig=true"),
    ToolPath = msBuildPath
};

MSBuild("YourSolution.sln", settings.WithTarget("restore"));

使用上述代码,您可以恢复所有项目,无论项目类型是 .NET Framework 还是 .NET Core。

【讨论】:

    猜你喜欢
    • 2019-08-03
    • 1970-01-01
    • 2022-07-14
    • 2021-11-26
    • 2016-10-24
    • 1970-01-01
    • 2019-06-23
    • 2017-08-03
    • 1970-01-01
    相关资源
    最近更新 更多