簡體   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