繁体   English   中英

Cake 构建脚本在 Azure Devops 中使用混合 .NET 核心和框架解决方案失败

[英]Cake build script fails in Azure Devops with mixed .NET core and framework solution

我的 .NET 服务器项目有一个蛋糕构建脚本,需要在我们新的 Azure DevOps 管道上构建。 该脚本在本地运行,但不适用于 Azure DevOps。 可能的原因是我有一个基于 .NET Core 2.1 构建的单元测试项目 (xUnit),而解决方案中的所有其他项目都是 .NET Framework 4.6.1(我们刚刚在 xUnit 项目中添加了我唯一的选项是将其添加为 .NET Core 项目)。

我写了我在本地工作的蛋糕脚本。 它看起来像这样:

//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 脚本相同的文件夹中运行它,但它似乎在我的存储库的根文件夹中运行,该文件夹是此解决方案的解决方案文件夹的一个文件夹。

我的脚本哪里出错了? 我的恢复步骤似乎很好,只是它只恢复了 UnitTests 项目(它依赖于所有其他项目)。 是还原吗? 构建或还原中是否缺少我的设置? 提前致谢。

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

我还发现我应该清理包目录,因为这是一种很好的做法。

正如您在如何使用 msbuild 命令仅恢复特定解决方案文件夹中所见,您可以使用特定的.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 框架或 .Net 核心如何,您都可以恢复所有项目

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM