通过每个框架一个解决方案在 Visual Studio 中构建多个框架支持

塔莫皮卡罗

我想在我的 Visual Studio Microsoft.NET.Sdk 项目中支持多个 TargetFrameworks,但我想制作它以便我有几个解决方案,其中解决方案将确定它将编译哪个目标框架。

理论上,这可以像这样完成:

Directory.Build.props:

<Project>
    <!-- 
        TargetFramework is not defined at nuget restore phase - but we can define it.
        If solution name contains "netcore" - then solution aims to .net core.
    -->
    <PropertyGroup Condition="$(SolutionName.Contains('netcore'))">
        <TargetFramework>netcoreapp3.1</TargetFramework>
    </PropertyGroup>

    <PropertyGroup Condition="!$(SolutionName.Contains('netcore'))">
        <TargetFramework>net472</TargetFramework>
    </PropertyGroup>

    <PropertyGroup>
        <!-- nuget restore path must be different for different target frameworks, otherwise it will create mess between projects -->
        <BaseIntermediateOutputPath>$(MSBuildThisFileDirectory)obj\nuget\$(TargetFramework)\$(MSBuildProjectName)\</BaseIntermediateOutputPath>
    </PropertyGroup>
</Project>

然后有两种解决方案,一种用于 .net framework 4.7.2,一种用于 .net core,其中第二个解决方案文件名应包含“netcore”字符串。

但是,如果TargetFramework开始在netstandard2.0诸如此类之间发生变化,这将无法正常工作...

然后我想到 - 也许我可以为多个框架执行 nuget restore,但只是为一个框架编译项目。我认为BeforeBuild事件可用于区分 nuget restore 和 build 之间的构建阶段 - 所以我写了这样的东西:

test.csproj:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>netcoreapp3.1;net472</TargetFrameworks>
  </PropertyGroup>

  <Target Name="BeforeBuild">
    <PropertyGroup>
      <TargetFrameworks>net472</TargetFrameworks>
    </PropertyGroup>
    <Message Text="TargetFrameworks: $(TargetFrameworks)" />
  </Target>

但这不起作用,构建发生在两个框架上,而不是一个,就像我最初想要的那样。

当然BeforeBuild最终应该执行解决方案,这netcore在它的名字中是没有的。

也许有人可以为此提出更好的解决方案?

塔莫皮卡罗

想通了。

所以多个解决方案不会容忍彼此的存在,并且需要覆盖nuget restore目录。覆盖可以根据解决方案进行,只是为了简单起见 - 只需使用解决方案名称来定义 nuget 还原路径。

Directory.Build.props:

<Project>
    <PropertyGroup>
        <BaseIntermediateOutputPath>$(MSBuildThisFileDirectory)obj\nuget\$(SolutionName)\$(MSBuildProjectName)\</BaseIntermediateOutputPath>
    </PropertyGroup>
</Project>

然后各个项目确定他们想要使用的目标框架,例如:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
  </PropertyGroup>

  <PropertyGroup Condition="$(SolutionName.Contains('netcore'))">
        <TargetFramework>netcoreapp3.1</TargetFramework>
    </PropertyGroup>

    <PropertyGroup Condition="!$(SolutionName.Contains('netcore'))">
        <TargetFramework>net472</TargetFramework>
    </PropertyGroup>

  <PropertyGroup>
    <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
    <IntermediateOutputPath>$(SolutionDir)obj\$(TargetFramework)-$(Configuration)\$(AssemblyName)\</IntermediateOutputPath>
    <OutputPath>$(SolutionDir)bin\$(TargetFramework)-$(Configuration)</OutputPath>
  </PropertyGroup>

  <ItemGroup>
    <Compile Remove="obj\**" />
    <EmbeddedResource Remove="obj\**" />
    <None Remove="obj\**" />
  </ItemGroup>

</Project>

这将允许有单独的解决方案,每个目标框架一个,但也可以正常用于 nuget restore + build。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从解决方案中构建一个Web项目,在Visual Studio Online Build Definition中使用多个Web项目

我可以让 Visual Studio 只在解决方案中构建每个项目一次吗?

如何一次在一个解决方案中编写多个项目中的代码?[Visual Studio/Xamarin]

SonarQube / SonarLint / Visual Studio:在解决方案中的所有项目中使用一个规则集

Visual Studio构建解决方案

在Visual Studio解决方案中仅构建特定项目

一个Visual Studio解决方案中可以有两个不同类型的项目(即,桌面项目和Web项目)吗?

在Visual Studio中查看多个项目/解决方案

如何从同一解决方案访问另一个项目?(Visual Studio 2012)

在 Visual Studio 2015 中安装实体框架缺少支持的标签

Visual Studio在解决方案资源管理器中的项目名称中添加一个数字

如何从Visual Studio解决方案项目中选择Google工作表中的下一个单元格?

我在Visual Studio 2013中已有一个解决方案。如何将其导入到VS Online工作区?

相对于一个空项目,使用Visual Studio中的模板创建解决方案的实际作用是什么?

创建一个批处理文件以打开Visual Studio 2010解决方案

Visual Studio 2015-如何从另一个解决方案引用项目?

启动Visual Studio时自动打开最后一个解决方案

多项目Visual Studio解决方案无法调试一个项目dll

在Visual Studio中的同一解决方案中运行多个Web窗体应用

在1个解决方案中的多个项目之间共享资源。Visual Studio 2010

Visual Studio 2017:通过命令行调用打开并构建解决方案

Visual Studio 2019 拒绝构建解决方案 C++

如何在Linux下构建Visual Studio解决方案?

如何使Visual Studio使用FAKE构建我的解决方案?

Visual Studio:构建解决方案时输出时间戳

Visual Studio在启动时自动构建解决方案

有没有办法在 Visual Studio 2017 中查看解决方案/项目构建使用的每个文件?

Visual Studio解决方案添加多个项目

如何在Visual Studio的同一解决方案上运行多个项目