首页 > 解决方案 > 如何在不调用每个项目的情况下让 dotnet build 选择正确的框架?

问题描述

所以情况是我试图在竹子上建立一个构建,它将构建这个包含许多项目的解决方案(它是一个共享库解决方案),每个项目都是一个 nuget 包。Bamboo 目前在 Ubuntu 16.04 上运行。该解决方案包含库项目 (netstandard2.0) 和测试 (netcoreapp2.0)。每个库都针对 net461 和 netstandard2.0,因为它们在我们较新的 .net core 2.0 应用程序以及我们的 4.6.1 旧平台中都使用。

问题是,如果我运行,dotnet build mysolution.sln那么 cli 会尝试在 net461 中构建所有内容,这显然会失败(linux 机器)。但是如果我运行,dotnet build mysolution.sln -f netstandard2.0那么测试将无法构建,因为它们是 netcoreapp2.0。

我唯一能想到的就是写入构建脚本,该行使用正确的框架构建每个单独的项目,这对我来说似乎有点傻。

幸运的是,所有的测试项目都带有后缀,.Tests所以我觉得可能有一种方法可以做一些find /path -regex 'match-csproj-where-not-tests' and so forth...伏都教来让这不那么烦人。我想知道那里是否有人可能知道一些我不知道的关于 dotnet cli 的事情,这可以帮助解决这个问题,甚至提供正则表达式解决方案。

TIA

标签: .netbashubuntu.net-core

解决方案


当我在等待更好的选择时,我想出了这个:

#!/bin/bash

# build netstandard2.0
projects=($( find . -name '*.csproj' -print0 | xargs -0 ls | grep -P '(?![Tests])\w+\.csproj' ))
BUILDCODE=0
for proj in ${projects[@]}
do
    dotnet build $proj -f netstandard2.0
    BUILDCODE=$?
    if (($BUILDCODE != 0)); then
        echo "Failed to build $proj"
        break
    fi
done
(exit $BUILDCODE)

# build netcoreapp2.0
projects=($( find . -name '*.csproj' -print0 | xargs -0 ls | grep -P '\w+\.Tests\.csproj' ))
BUILDCODE=0
for proj in ${projects[@]}
do
    dotnet build $proj -f netcoreapp2.0
    BUILDCODE=$?
    if (($BUILDCODE != 0)); then
        echo "Failed to build $proj"
        break
    fi
done
(exit $BUILDCODE)

这将搜索无Test后缀的项目,然后构建为 netstandard2.0,那些有Test后缀的项目并将它们构建为 netcoreapp2.0。我会将它们作为两个不同的构建任务插入,以确保退出代码导致失败并且不要尝试继续。

我可能不得不做同样的事情来运行 xUnit 测试,因为dotnet test solution.sln由于库项目不包含测试而失败 ::eye_roll::


推荐阅读