2014-09-05 82 views
2

摘要的NuGet:請勿包括包

參考當我用NuGet包庫,而在另一個項目中涉及的項目將拉動到build目錄產生額外的文件引用它。

工作案例

 

Project: ReferenceLibrary 
    Output: ReferenceLibrary.dll 

Project: DerivedLibrary 
    Output: DerivedLibrary.dll 
    References: ReferenceLibrary (Copy Local = False) 

Project: ConsoleApplication 
    Output: ConsoleApplication.exe 
    References: DerivedLibrary 

編輯:參照庫是不可複製的,因爲它在運行時解決。有幾個版本取決於目標。在派生項目中的參考。是這樣我可以對它進行編碼。

如果我構建它,那麼只有DerivedLibrary.dll被複制到ConsoleApplication構建文件夾(即bin/Release)。

不能工作的情況

Project: ConsoleApplication 
    Output: ConsoleApplication.exe 
    Package: DerivedLibrary.nupkg (depends on ReferenceLibrary.nupkg) 

一個項目引用添加到DerivedLibray.dll。 DerivedLibrary.dll和ReferenceLibrary.dll都從它們的包中複製而來。

我可以看到它被複制到MSBUILD日誌中。

_CopyFilesMarkedCopyLocal: 
    Copying file from "c:\...\ReferenceLibrary.dll" to "bin\Debug\ReferenceLibrary.dll" 

即使它沒有在任何地方的.csproj中引用。

我不能告訴如果這是一個NuGet問題(由於它如何解包的東西)或Visual Studio項目(它如何複製引用的程序集和編碼其他程序集中的需求)。

+2

如果派生庫依賴於Reference庫,您真的很驚訝使用Derived庫的應用程序也需要Reference庫來運行嗎?還是我誤解了這個問題? – mason 2014-09-05 13:52:11

+0

對不起,我試圖消除這個複雜性,因爲問題變得非常不自信。參考庫在運行時解析(因此我不希望它複製到輸出文件夾)。我將把它重新加入。 – Karle 2014-09-05 13:53:31

+1

如果它在運行時被引用,那麼當應用程序手動將其放入'bin'文件夾或GAC時,您的應用程序是否會運行?這有什麼用途? – ashes999 2014-09-05 14:10:45

回答

2

我發現一個可能的解決方案是使用後期構建目標來刪除違規引用。

在派生庫中添加一個DerivedLibrary.targets文件。

<?xml version="1.0" encoding="utf-8" ?> 
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
    <Target Name="RemoveUnwantedReferences" AfterTargets="Build"> 
     <Message Text="Removing unwanted references"/> 
     <Delete Files="$(OutputPath)ReferenceLibrary.dll"/> 
    </Target> 
</Project> 

然後在.nuspec包括它

<package> 
    ... 
    <files> 
    <file src="Targets/DerivedLibrary.targets" target="/build/DerivedLibrary.targets" /> 
    </files> 
</package> 

然後當有人安裝包後生成掛鉤將被添加。當他們構建被複制的文件時,它們將被自動刪除。