2013年12月24日 星期二

NuGet Multiple Framework 建置

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

Step2 每一個版本建立一個專案檔



Step3 每一個專案檔選擇不同的Framework,以及條件式編譯符號和修改輸出路徑
注意每個組態都要設定喔
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檔

<?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


瀏覽結果