NuGet Multiple Framework 建置

Dec 24, 2013

4 mins read

NuGet使用一段時間之後,開始碰到共用的元件,在不同專案的Framework版本不同,會有引用上的困擾,在網路上找了幾個解決方案,實作之後個人覺的比較簡單的方法如下

Step1 每個版本建立一個方案檔 一般專案的命名為net20、net35、net40、net45… 其他類型的專案則再加上縮寫 Client Profile(client) WindowsPhone(wp) Silverlight(sl) CompactFramework(cf) 更多完整的資料請參考官網文件 http://docs.nuget.org/docs/creating-packages/creating-and-publishing-a-package

[![](http://3.bp.blogspot.com/-Z_7Nwh3E004/Urj3_IOzGmI/AAAAAAAAA3Q/JSdtbB7ne1Q/s1600/01.sln.png)](http://3.bp.blogspot.com/-Z_7Nwh3E004/Urj3_IOzGmI/AAAAAAAAA3Q/JSdtbB7ne1Q/s1600/01.sln.png)
[![](http://2.bp.blogspot.com/-w46datk00Fw/Urj4AWcam2I/AAAAAAAAA3Y/A-GmHxGvsY0/s1600/02.sln.png)](http://2.bp.blogspot.com/-w46datk00Fw/Urj4AWcam2I/AAAAAAAAA3Y/A-GmHxGvsY0/s1600/02.sln.png)
Step2 每一個版本建立一個專案檔
[![](http://1.bp.blogspot.com/-GcIzkWZiGz0/Urj4QuT3lII/AAAAAAAAA3c/IbIp_s_gt8g/s1600/03.csproj.png)](http://1.bp.blogspot.com/-GcIzkWZiGz0/Urj4QuT3lII/AAAAAAAAA3c/IbIp_s_gt8g/s1600/03.csproj.png)
[![](http://3.bp.blogspot.com/-wPPOHszWFcA/Urj4RmcGNvI/AAAAAAAAA3o/XBtd2ATXs14/s1600/04.csproj.png)](http://3.bp.blogspot.com/-wPPOHszWFcA/Urj4RmcGNvI/AAAAAAAAA3o/XBtd2ATXs14/s1600/04.csproj.png)
Step3 每一個專案檔選擇不同的Framework,以及條件式編譯符號和修改輸出路徑 **注意每個組態都要設定喔 **
[![](http://2.bp.blogspot.com/-B84By-NaVHE/Urj4ZL6TGpI/AAAAAAAAA3s/mZLbwfLGEHw/s1600/05.version.png)](http://2.bp.blogspot.com/-B84By-NaVHE/Urj4ZL6TGpI/AAAAAAAAA3s/mZLbwfLGEHw/s1600/05.version.png)
[![](http://2.bp.blogspot.com/-9Ka8GNPihf0/Urj4abt1ZLI/AAAAAAAAA34/jRp00XFmLv8/s1600/06.output.png)](http://2.bp.blogspot.com/-9Ka8GNPihf0/Urj4abt1ZLI/AAAAAAAAA34/jRp00XFmLv8/s1600/06.output.png)
Step4 依Framework版本,加上條件式編譯
using System;
using System.Collections.Generic;

#if NET35 || NET40 || NET45 using System.Linq; #endif

using System.Text;

#if NET40 || NET45 using System.Threading.Tasks; #endif

namespace ClassLibrary1 { public class Class1 { } }

Step5 手動建立nuget spec檔

[![](http://2.bp.blogspot.com/-_JkzDLOeeJc/Urj4jGQfcnI/AAAAAAAAA38/nPBreRhuDJA/s1600/07.spec.png)](http://2.bp.blogspot.com/-_JkzDLOeeJc/Urj4jGQfcnI/AAAAAAAAA38/nPBreRhuDJA/s1600/07.spec.png)
<?xml version="1.0"?>
<package >
  <metadata>
    <id>ClassLibrary1</id>
    <version>1.0.0</version>
    <authors>xian</authors>
    <owners>xian</owners>
    <licenseUrl>http://LICENSE_URL_HERE_OR_DELETE_THIS_LINE</licenseUrl>
    <projectUrl>http://PROJECT_URL_HERE_OR_DELETE_THIS_LINE</projectUrl>
    <iconUrl>http://ICON_URL_HERE_OR_DELETE_THIS_LINE</iconUrl>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Package description</description>
    <releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
    <copyright>Copyright 2013</copyright>
    <tags>Tag1 Tag2</tags>
    <dependencies>
      <dependency id="SampleDependency" version="1.0" />
    </dependencies>
  </metadata>
  <files>
        <file src="ClassLibrary1\**\*.cs" exclude="ClassLibrary1\obj\**\*.cs" target="src" />
  <file src="ClassLibrary1\bin\Release\net20\ClassLibrary1.dll" target="lib\net20\ClassLibrary1.dll" />
        <file src="ClassLibrary1\bin\Release\net20\ClassLibrary1.pdb" target="lib\net20\ClassLibrary1.pdb" />
  <file src="ClassLibrary1\bin\Release\net35\ClassLibrary1.dll" target="lib\net35\ClassLibrary1.dll" />
        <file src="ClassLibrary1\bin\Release\net35\ClassLibrary1.pdb" target="lib\net35\ClassLibrary1.pdb" />
        <file src="ClassLibrary1\bin\Release\net40\ClassLibrary1.dll" target="lib\net40\ClassLibrary1.dll" />
        <file src="ClassLibrary1\bin\Release\net40\ClassLibrary1.pdb" target="lib\net40\ClassLibrary1.pdb" />
  <file src="ClassLibrary1\bin\Release\net45\ClassLibrary1.dll" target="lib\net45\ClassLibrary1.dll" />
        <file src="ClassLibrary1\bin\Release\net45\ClassLibrary1.pdb" target="lib\net45\ClassLibrary1.pdb" />
    </files>
</package> 
Step6 加上測試用批次檔
@echo off
set MSBUILD=%WINDIR%\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe
if not exist %MSBUILD% set MSBUILD=%WINDIR%\Microsoft.NET\Framework\v4.0.30319\msbuild.exe
if not exist %MSBUILD% set MSBUILD=%WINDIR%\Microsoft.NET\Framework\v3.5\msbuild.exe
if not exist %MSBUILD% (
 echo MSBuild not found
 exit
) 

%MSBUILD% ClassLibrary1.net20.sln /property:Configuration=Release /m %MSBUILD% ClassLibrary1.net35.sln /property:Configuration=Release /m %MSBUILD% ClassLibrary1.net40.sln /property:Configuration=Release /m %MSBUILD% ClassLibrary1.net45.sln /property:Configuration=Release /m nuget pack ClassLibrary1.nuspec pause

瀏覽結果

[![](http://3.bp.blogspot.com/-Li3Okgdz5VM/Urj4nvEX7QI/AAAAAAAAA4E/6K0LQ1Bu9KY/s1600/08.packet.png)](http://3.bp.blogspot.com/-Li3Okgdz5VM/Urj4nvEX7QI/AAAAAAAAA4E/6K0LQ1Bu9KY/s1600/08.packet.png)

Sharing is caring!