visual studio單項目一次生成多框架類庫、多框架項目合並


不同平台框架項目使用同一套代碼,一次編譯生成多個框架類庫

需要先了解的東西

  • msbuild
  • .net framework遷移至.net core,或者合並,單個項目編譯不同的框架 ——官方文檔

分析

  • 使用.NET 可移植性分析器工具分析項目依賴
  • .NET 可移植性分析器工具有關博客
  • 根據分析結果以及建議,到這個網站可以根據fx框架下類型名查找對應的nuget包,來替代不兼容的引用
  • 重新組織項目依賴,整理各個框架公共的代碼文件和其他平台不支持的功能的代碼文件,必要時可將一個文件分成多個多個文件
  • 由於我這里已經是多個分開的項目,就省了分析代碼這一步,直接分析文件應該歸屬哪些項目,哪些獨有的

添加PropertyGroup

多目標平台

   <Project Sdk="Microsoft.NET.Sdk">
        <PropertyGroup>
            <TargetFrameworks>net40;net45;netstandard2.0</TargetFrameworks>
        </PropertyGroup>
    </Project>

編譯符號和輸出目錄設置

  <PropertyGroup Condition="'$(Configuration)|$(Platform)|$(TargetFramework)' == 'Debug|AnyCPU|net45'">
    <OutputPath>..\..\Bin\</OutputPath>
    <DefineConstants>TRACE;DEBUG</DefineConstants>
    <DocumentationFile>..\..\Bin\net45\XCode.xml</DocumentationFile>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)|$(TargetFramework)' == 'Release|AnyCPU|net45'">
    <OutputPath>..\..\Bin\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <DocumentationFile>..\..\Bin\net45\XCode.xml</DocumentationFile>
  </PropertyGroup>
  
    <PropertyGroup Condition="'$(Configuration)|$(Platform)|$(TargetFramework)'=='Debug|AnyCPU|netstandard2.0'">
    <OutputPath>..\..\Bin\</OutputPath>
    <DocumentationFile>..\..\Bin\netstandard2.0\XCode.xml</DocumentationFile>
    <DefineConstants>TRACE;DEBUG;NETSTANDARD2_0;__CORE__</DefineConstants>
  </PropertyGroup>
      <PropertyGroup Condition="'$(Configuration)|$(Platform)|$(TargetFramework)'=='Release|AnyCPU|netstandard2.0'">
    <OutputPath>..\..\Bin\</OutputPath>
    <DocumentationFile>..\..\Bin\netstandard2.0\XCode.xml</DocumentationFile>
    <DefineConstants>TRACE;RELEASE;NETSTANDARD2_0;__CORE__</DefineConstants>
  </PropertyGroup>
  
    <PropertyGroup Condition="'$(Configuration)|$(Platform)|$(TargetFramework)' == 'Debug|AnyCPU|net40'">
    <OutputPath>..\..\Bin\</OutputPath>
    <DefineConstants>TRACE;DEBUG;NET4</DefineConstants>
    <DocumentationFile>..\..\Bin\net40\XCode.xml</DocumentationFile>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)|$(TargetFramework)' == 'Release|AnyCPU|net40'">
    <OutputPath>..\..\Bin\</OutputPath>
    <DefineConstants>TRACE;NET4</DefineConstants>
    <DocumentationFile>..\..\Bin\net40\XCode.xml</DocumentationFile>
  </PropertyGroup>

添加依賴

  <ItemGroup Condition=" '$(TargetFramework)' == 'net45' Or '$(TargetFramework)' == 'netstandard2.0' ">
  </ItemGroup>
  <ItemGroup Condition="'$(TargetFramework)' == 'net45'">
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System" />
    <Reference Include="System.configuration" />
    <Reference Include="System.Data" />
    <Reference Include="System.Management" />
    <Reference Include="System.Security" />
    <Reference Include="System.Web" />
    <Reference Include="System.Web.Extensions" />
    <Reference Include="System.Windows.Forms" />
    <Reference Include="System.XML" />
  </ItemGroup>
  
  <ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
    <PackageReference Include="Microsoft.AspNetCore.Http" Version="2.0.2" />
    <PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.0.2" />
    <PackageReference Include="Microsoft.AspNetCore.Session" Version="2.0.2" />
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Xml" Version="2.0.0" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.0.0" />
    <PackageReference Include="System.Data.Common" Version="4.3.0" />
  </ItemGroup>
  
  <ItemGroup Condition="'$(TargetFramework)' == 'net40'">
     <Reference Include="Microsoft.CSharp" />
    <Reference Include="System" />
    <Reference Include="System.configuration" />
    <Reference Include="System.Data" />
    <Reference Include="System.Management" />
    <Reference Include="System.Security" />
    <Reference Include="System.Web" />
    <Reference Include="System.Web.Extensions" />
    <Reference Include="System.Windows.Forms" />
    <Reference Include="System.XML" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\NewLife.Core\NewLife.Core.csproj">
      <Name>NewLife.Core</Name>
    </ProjectReference>
  </ItemGroup>
  • 如果依賴項那里顯示感嘆號,關掉項目重新打開即可

代碼文件處理

  • 將前面分析好的代碼文件分情況做排除,由於項目里面的文件自動顯示在解決方案,不用顯式包含文件,只需要根據目標平台做好排除即可

主副平台項目文件處理

全部平台都包含

  • MSBuild項目文件會自動包含項目文件下面的所有文件,不必處理

一個或多個平台包含

  • 在所有不包含的此文件的平台的ItemGroup中移除

沒有平台包含

  • 沒有項目包含此文件為什么還有放在項目文件夾?我也不知道,在全局ItemGroup移除即可

最后

  • 生成看看還有什么錯誤,看情況解決,比如PropertyGroup設置了文件版本信息,同時引用了/Properties/AssemblyInfo.cs導致報錯等等。
  • 合並之前的項目文件
  • 合並之后的項目文件
  • 合並第一個項目的時候,簡直一團糟,本來所有項目都不包含的代碼都涌進來,有的文件只是一個項目有。合並到第二個,先做好分析,什么文件該引用還是排除,有條理很快就完成,處理好文件引用之后一次性生成成功,沒有第一個項目合並的時候一千多個錯誤

補充

自動生成內部版本號

nuget包相關


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM