简体   繁体   English

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

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

I have a cake build script with my .NET server project which needs to be built on our new Azure DevOps pipeline.我的 .NET 服务器项目有一个蛋糕构建脚本,需要在我们新的 Azure DevOps 管道上构建。 The script works locally but does not work on Azure DevOps.该脚本在本地运行,但不适用于 Azure DevOps。 There are possible reasons for this which may be that I have a Unit Tests project (xUnit) built on .NET Core 2.1 while all the other projects in solution are .NET Framework 4.6.1 (we just added in the xUnit project which my only option was to add it in as a .NET Core project).可能的原因是我有一个基于 .NET Core 2.1 构建的单元测试项目 (xUnit),而解决方案中的所有其他项目都是 .NET Framework 4.6.1(我们刚刚在 xUnit 项目中添加了我唯一的选项是将其添加为 .NET Core 项目)。

I wrote my cake script which works locally.我写了我在本地工作的蛋糕脚本。 It looks like this:它看起来像这样:

//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);

Now when I run this in Azure DevOps, I get 10 errors for each project similar to this example below:现在,当我在 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.

I am unsure of exactly the problem.我不确定到底是什么问题。 It could be because we have a mixed project.这可能是因为我们有一个混合项目。 It could be because the file structure.这可能是因为文件结构。 I don't know what options or settings I would need to set in the cake commands for build or restore.我不知道我需要在构建或恢复的 cake 命令中设置哪些选项或设置。 It could also be when I run this locally, I run it in the same folder as the Cake script, but it appears to be running in the root folder for my repo which is one folder up from the solution folder for this solution.也可能是当我在本地运行它时,我在与 Cake 脚本相同的文件夹中运行它,但它似乎在我的存储库的根文件夹中运行,该文件夹是此解决方案的解决方案文件夹的一个文件夹。

Where in my script am I going wrong?我的脚本哪里出错了? My restore step seems fine except it only restores the UnitTests project (which does depend on all other projects).我的恢复步骤似乎很好,只是它只恢复了 UnitTests 项目(它依赖于所有其他项目)。 Is it the restore?是还原吗? Is there a setting I am missing in the build or the restore?构建或还原中是否缺少我的设置? Thanks in advance.提前致谢。

So the answer was that I had get all of the project files and run "NuGetRestore" method on each of them excluding the .NET Core UnitTests project.所以答案是我已经获取了所有项目文件并在每个文件上运行“NuGetRestore”方法,不包括.NET Core UnitTests 项目。 Then I can run the DotNetCoreRestore method which just restores the .NET Core UnitTests project.然后我可以运行 DotNetCoreRestore 方法,它只是恢复 .NET Core UnitTests 项目。

I also found I should clean the packages directory as that is good practice.我还发现我应该清理包目录,因为这是一种很好的做法。

As you can see in How to restore only specific Solution Folder with msbuild command you can use specific .proj file two distinguish those projects and work with them very easily正如您在如何使用 msbuild 命令仅恢复特定解决方案文件夹中所见,您可以使用特定的.proj文件来区分这些项目并非常轻松地使用它们

But I suggest you use below command to restore all your projects without any problem, if you have visual studio 2019但我建议你使用下面的命令来恢复你的所有项目,如果你有 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"));

With above code you can restore all projects regardless of project type and .Net framework or .Net core使用上面的代码,无论项目类型和 .Net 框架或 .Net 核心如何,您都可以恢复所有项目

暂无
暂无

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

相关问题 在同一解决方案文件中同时包含 .NET Framework 和 .NET Core 项目的 Azure DevOps 中构建 .sln - Building .sln in Azure DevOps that has both .NET Framework and .NET Core projects in the same solution file 无法在 Azure Devops 上使用 DotNetCoreCLI 任务恢复.Net Core 解决方案 - Can't restore .Net Core solution with DotNetCoreCLI task on Azure Devops .net 核心单元测试项目未能在 Azure Devops 管道中构建 - .net core unit test project failing to build in Azure Devops pipeline 在 Teamcity 上构建混合解决方案(.NET Framework 和 .NET Core)时找不到包 - Package not found while building a mixed solution (.NET Framework and .NET Core) on Teamcity 无法命令行构建混合解决方案(.NET Framework与.NET标准) - Unable to Command-Line Build a Mixed Solution (.NET Framework with .NET Standard) Azure Devops - 针对解决方案中的多个 .NET 框架 - Azure Devops - Targeting Multiple .NET frameworks in solution 生成NET Framework项目,该项目使用Cake build引用NETStandard项目 - Build NET Framework project that references NETStandard project with Cake build Azure Devops 无法在同一 CI 任务中运行 .NET 框架和 dotnet 核心单元测试 - Azure Devops can't run .NET Framework and dotnet core unit test in the same CI task .Net Framework和.Net Core在同一解决方案中 - .Net Framework and .Net Core in same solution .net 标准库在 .net 核心中失败,但在框架中失败 - .net standard library fails in .net core but not in framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM