我正在尝试为其他解决方案/项目/等中使用的一对内部C#项目(最终更多,但从这两个开始)生成NuGet包。这两个内部C#项目都引用了其他NuGet包(特别是Enterprise Library Logging)。我正在使用Nuget的功能,它允许它从CSPROJ文件构建包,以简化我的生活。问题是Nuget.Exe工具不识别项目引用的包作为依赖项。为了给出解决方案的完整布局:
Solution:
Project 1
Refs to some NuGet packages off the global repository (Enterprise Library, etc etc).
(has a packages.config file that explicity lists them all)
Project 2
Refs to some NuGet packages off the global repository (Enterprise Library, etc. etc).
Project ref to Project 1
(has a packages.config file that explicity lists them all)
我使用的命令行是:
nuget pack Project2.csproj -IncludeReferencedProjects -verbosity detailed
命令行输出的相关部分是:
Found packages.config. Using packages listed as dependencies
Id: Project2
Version: 1.0.0.0
Authors: abc
Description: abc
Dependencies: Project1 (= 1.0.0.0)
Added file 'lib\net35\project2.dll'.
我原本期望“依赖项:Project1,Enterprise Libray Logging等等”
* 注意:我必须向Project1和Project2添加一个nuspec文件,以使项目间依赖项部分正常工作。除了占位符值之外,nuspec文件基本上是空白的:
<?xml version="1.0"?>
<package >
<metadata>
<id>$id$</id>
<version>$version$</version>
<title>$title$</title>
<authors>abc</authors>
<owners>abc</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>defr</description>
<copyright>Copyright 2013</copyright>
</metadata>
</package>
我在NuGet问题跟踪器上发现的最接近的事情是http://nuget.codeplex.com/workitem/3097,但这有点不同,并且那里提出的解决方案似乎都没有。
任何人都有类似的情况正常工作吗?
我在Visual Studio 2012中使用NuGet 2.7。
答案 0 :(得分:8)
我最终查看了NuGet源代码,发现了我们遇到的问题。
在我们的案例中,它最终成为两个独立的问题:
这些导致问题的原因是因为NuGet查找解决方案级别包文件夹以确定要引入哪些包依赖项(不太确定如何进行此确定)。如果该包文件夹的路径不正确(如果NuGet使用错误的解决方案文件那样),则无法正确解析依赖关系。此外,如果packages文件夹为空,则它也无法正确解析依赖项。
答案 1 :(得分:3)
我的解决方法是解决这个问题,我在postbuild中运行了这个脚本:
param([string]$projectNuspec, [string]$projectName, [string]$projectPath)
Function DisplayInfo([string] $text)
{
Write-Host ""
Write-Host "****************************"
Write-Host $text
Write-Host "****************************"
Write-Host ""
}
$csprojMustBeSaved = $false
$packageFileName = [System.IO.Path]::Combine($projectPath, "packages.config")
If(Test-Path $packageFileName)
{
$csprojMustBeSaved = $false
Write-host "Project is:" $projectName
Write-Host "Nuspec file is:" $packageFileName
$projectFullName = [System.IO.Path]::Combine($projectPath, $projectName)
$nuspecFullName = [System.IO.Path]::Combine($projectPath, $projectNuspec)
Write-Host "Open Nuspec File: " $packageFileName
$nuspecStream = [System.IO.File]::OpenText($nuspecFullName)
$nuspecDoc = New-Object System.Xml.XmlDocument
$nuspecDoc.Load($nuspecStream)
$nuspecStream.Dispose()
Write-Host "Open project File: " $projectFullName
$csprojStream = [System.IO.File]::OpenText($projectFullName)
$csprojDoc = New-Object System.Xml.XmlDocument
$csprojDoc.Load($csprojStream)
$csprojStream.Dispose()
$xnm = New-Object System.Xml.XmlNamespaceManager(New-Object System.Xml.NameTable);
$xnm.AddNamespace("x", "http://schemas.microsoft.com/developer/msbuild/2003");
[System.Xml.XmlNode]$metaData = $nuspecDoc.package.metadata
if($metaData.dependencies -ne $null)
{
DisplayInfo("Clear dependencies in nuspec")
foreach($dependencies in $metaData.dependencies)
{
$metaData.RemoveChild($dependencies)
}
DisplayInfo("Dependencies Cleared")
}
DisplayInfo("Open packages.config File: $packageFileName")
$packageFile = New-Object System.Xml.XmlDocument
$packageFile.Load($packageFileName)
$FirstTime = $true
foreach($package in $packageFile.packages.ChildNodes)
{
if(!$package.id.Equals(""))
{
if($FirstTime -eq $true)
{
$dependencies = $null
$dependencies = $nuspecDoc.CreateElement('dependencies', "http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd")
$nuspecDoc.package.metadata.AppendChild($dependencies)
$FirstTime = $false
}
Write-Host "Create dependency for: "$package.id"version: "$package.version
$dependency = $nuspecDoc.CreateElement('dependency', "http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd")
$dependency.SetAttribute('id',$package.id)
$packageVersion = [string]::Format("[{0}]",$package.version)
$dependency.SetAttribute('version',$packageVersion)
$dependencies.AppendChild($dependency)
}
}
if($csprojMustBeSaved -eq $true)
{
Write-Host "Save Csproj File: "$projectFullName
$csprojDoc.Save($projectFullName)
Write-host "File Saved successfuly"
}
Write-Host "Save Nuspec File: " $projectPath$projectNuspec
$nuspecDoc.Save($nuspecFullName);
Write-host "File Saved successfuly"
}
else
{
Write-Host "There is no " $packageFileName " skip this step"
}
答案 2 :(得分:0)
几周前我遇到了完全相同的问题,在版本2.7之前它正在运行。我的所有依赖项都将添加到包清单中。当我从2.6版本更新到2.7时它不再工作了,所以我创建了一个powershell任务,在构建之前将package.config的内容复制到nuspec。