Alo*_*low 112 nuget nuget-package nuget-spec
我正在尝试为.Net程序集创建NuGet程序包,它会对原生的win32 dll进行pinvoke.我需要将程序集和本机dll打包,并将程序集添加到项目引用中(此部分没有问题),并且应将本机dll复制到项目输出目录或其他相关目录中.
我的问题是:
kjb*_*tel 119
使用Copy目标文件中的目标来复制所需的库不会将这些文件复制到引用项目的其他项目,从而产生一个DllNotFoundException.这可以通过使用None元素更简单的目标文件来完成,因为MSBuild会将所有None文件复制到引用项目.
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<NativeLibs Include="$(MSBuildThisFileDirectory)**\*.dll" />
<None Include="@(NativeLibs)">
<Link>%(RecursiveDir)%(FileName)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)
将目标文件build与所需的本机库一起添加到nuget包的目录中.目标文件将包括dll目录的所有子目录中的所有文件build.因此,要添加x86并x64通过使用本机库的版本Any CPU管理组件,你最终会得到类似以下的目录结构:
同样x86和x64目录将在项目的输出目录生成时创建.如果你不需要子目录,则**和%(RecursiveDir)可以被删除,而不是包括在所要求的文件build直接目录.其他所需的内容文件也可以以相同的方式添加.
None在Visual Studio中打开时,在目标文件中添加的文件将不会显示在项目中.如果你想知道我为什么不使用Content在nupkg文件夹,那是因为没有办法来设置CopyToOutputDirectory元素,而无需使用PowerShell脚本(这只会在Visual Studio中运行,而不是在命令提示符下,在构建服务器或其他IDE,并且在project.json/xproj DNX项目中不受支持),我更喜欢使用Link文件而不是项目中的文件的附加副本.
更新:
虽然这也应该可以使用,Content而不是None看起来msbuild中有一个错误,所以文件不会被复制到引用项目多个步骤被删除(例如proj1 - > proj2 - > proj3,proj3将无法获取文件来自proj1的NuGet包,但是proj2会).
buy*_*ush 30
我最近遇到了同样的问题,当我尝试构建一个EmguCV NuGet包时,包括托管程序集和非托管共享库(也必须放在x86子目录中),每次构建后都必须自动复制到构建输出目录.
这是我提出的解决方案,仅依赖于NuGet和MSBuild:
将托管程序集放在/lib程序包的目录中(显而易见的部分),并将非托管共享库和相关文件(例如.pdb软件包)放在/build子目录中(如NuGet文档中所述).
将所有非托管*.dll文件结尾重命名为不同的名称,例如,*.dl_以防止NuGet抱怨所谓的程序集被放置在错误的位置("问题:在lib文件夹外部组装.").
<PackageName>.targets在/build子目录中添加一个自定义文件,其中包含以下内容(请参阅下面的说明):
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<AvailableItemName Include="NativeBinary" />
</ItemGroup>
<ItemGroup>
<NativeBinary Include="$(MSBuildThisFileDirectory)x86\*">
<TargetPath>x86</TargetPath>
</NativeBinary>
</ItemGroup>
<PropertyGroup>
<PrepareForRunDependsOn>
$(PrepareForRunDependsOn);
CopyNativeBinaries
</PrepareForRunDependsOn>
</PropertyGroup>
<Target Name="CopyNativeBinaries" DependsOnTargets="CopyFilesToOutputDirectory">
<Copy SourceFiles="@(NativeBinary)"
DestinationFiles="@(NativeBinary->'$(OutDir)\%(TargetPath)\%(Filename).dll')"
Condition="'%(Extension)'=='.dl_'">
<Output TaskParameter="DestinationFiles" ItemName="FileWrites" />
</Copy>
<Copy SourceFiles="@(NativeBinary)"
DestinationFiles="@(NativeBinary->'$(OutDir)\%(TargetPath)\%(Filename).%(Extension)')"
Condition="'%(Extension)'!='.dl_'">
<Output TaskParameter="DestinationFiles" ItemName="FileWrites" />
</Copy>
</Target>
</Project>
Run Code Online (Sandbox Code Playgroud)上述.targets文件将注入目标项目文件中NuGet包的安装,并负责将本机库复制到输出目录.
<AvailableItemName Include="NativeBinary" /> 为项目添加一个新项目"Build Action"(也可以在Visual Studio的"Build Action"下拉列表中找到).
<NativeBinary Include="...添加放置在/build/x86当前项目中的本机库,并使其可供自定义目标访问,该目标将这些文件复制到输出目录.
<TargetPath>x86</TargetPath>向文件添加自定义元数据,并告诉自定义目标将本机文件复制到x86实际输出目录的子目录中.
该<PrepareForRunDependsOn ...块将自定义目标添加到构建所依赖的目标列表中,有关详细信息,请参阅Microsoft.Common.targets文件.
自定义目标CopyNativeBinaries包含两个复制任务.第一个负责将任何*.dl_文件复制到输出目录,同时将其扩展名更改回原始文件*.dll.第二个只是将其余的(例如任何*.pdb文件)复制到同一位置.这可以由单个复制任务和install.ps1脚本替换,该脚本必须在程序包安装期间重命名所有*.dl_文件*.dll.
但是,此解决方案仍然不会将本机二进制文件复制到引用最初包含NuGet包的项目的另一个项目的输出目录中.您仍然需要在"最终"项目中引用NuGet包.
Ben*_*hon 27
下面是使用替代.targets以项目注入机DLL具有以下属性.
Build action = NoneCopy to Output Directory = Copy if newer此技术的主要好处是本机DLL可传递地复制到依赖项目的bin/文件夹中.
查看.nuspec文件的布局:

这是.targets文件:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)\..\MyNativeLib.dll">
<Link>MyNativeLib.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)
这将插入MyNativeLib.dll原来项目的一部分(但奇怪的是文件在Visual Studio中不可见).
请注意在<Link>文件bin/夹中设置目标文件名的元素.
小智 17
如果其他人偶然发现了这一点.
该.targets文件名必须等于NuGet包标识
别的什么都不行.
积分转至:https: //sushihangover.github.io/nuget-and-msbuild-targets/
我应该仔细阅读,因为这里实际上已经注意到了.花了我很多时间..
添加自定义
<PackageName>.targets
小智 13
这有点晚了,但我为此创建了一个nuget包exaclty.
我们的想法是在您的nuget包中添加一个额外的特殊文件夹.我相信你已经了解了Lib和Content.我创建的nuget包查找名为Output的文件夹,并将其中的所有内容复制到项目输出文件夹中.
您唯一需要做的就是在包中添加一个nuget依赖项http://www.nuget.org/packages/Baseclass.Contrib.Nuget.Output/
我写了一篇关于它的博客文章:http: //www.baseclass.ch/blog/Lists/Beitraege/Post.aspx?ID = 6&mobile = 0
我无法解决你的具体问题,但我可以给你一个建议。
您的关键要求是:“并且不要自动注册参考”......
所以你必须熟悉“解决方案项目”
请参阅此处的参考:
您必须编写一些 powershell voodoo 才能将本机 dll 的副本放入其主目录(同样,因为您不希望自动添加引用 voodoo 触发)
这是我编写的 ps1 文件......将文件放入第三方引用文件夹中。
这里有足够的内容让您弄清楚如何将本机 dll 复制到某个“家”...而不必从头开始。
再说一遍,这不是直接打击,但总比没有好。
param($installPath, $toolsPath, $package, $project)
if ($project -eq $null) {
$project = Get-Project
}
Write-Host "Start Init.ps1"
<#
The unique identifier for the package. This is the package name that is shown when packages are listed using the Package Manager Console. These are also used when installing a package using the Install-Package command within the Package Manager Console. Package IDs may not contain any spaces or characters that are invalid in an URL.
#>
$separator = " "
$packageNameNoVersion = $package -split $separator | select -First 1
Write-Host "installPath:" "${installPath}"
Write-Host "toolsPath:" "${toolsPath}"
Write-Host "package:" "${package}"
<# Write-Host "project:" "${project}" #>
Write-Host "packageNameNoVersion:" "${packageNameNoVersion}"
Write-Host " "
<# Recursively look for a .sln file starting with the installPath #>
$parentFolder = (get-item $installPath)
do {
$parentFolderFullName = $parentFolder.FullName
$latest = Get-ChildItem -Path $parentFolderFullName -File -Filter *.sln | Select-Object -First 1
if ($latest -ne $null) {
$latestName = $latest.name
Write-Host "${latestName}"
}
if ($latest -eq $null) {
$parentFolder = $parentFolder.parent
}
}
while ($parentFolder -ne $null -and $latest -eq $null)
<# End recursive search for .sln file #>
if ( $parentFolder -ne $null -and $latest -ne $null )
{
<# Create a base directory to store Solution-Level items #>
$thirdPartyReferencesDirectory = $parentFolder.FullName + "\ThirdPartyReferences"
if ((Test-Path -path $thirdPartyReferencesDirectory))
{
Write-Host "--This path already exists: $thirdPartyReferencesDirectory-------------------"
}
else
{
Write-Host "--Creating: $thirdPartyReferencesDirectory-------------------"
New-Item -ItemType directory -Path $thirdPartyReferencesDirectory
}
<# Create a sub directory for only this package. This allows a clean remove and recopy. #>
$thirdPartyReferencesPackageDirectory = $thirdPartyReferencesDirectory + "\${packageNameNoVersion}"
if ((Test-Path -path $thirdPartyReferencesPackageDirectory))
{
Write-Host "--Removing: $thirdPartyReferencesPackageDirectory-------------------"
Remove-Item $thirdPartyReferencesPackageDirectory -Force -Recurse
}
if ((Test-Path -path $thirdPartyReferencesPackageDirectory))
{
}
else
{
Write-Host "--Creating: $thirdPartyReferencesPackageDirectory-------------------"
New-Item -ItemType directory -Path $thirdPartyReferencesPackageDirectory
}
Write-Host "--Copying all files for package : $packageNameNoVersion-------------------"
Copy-Item $installPath\*.* $thirdPartyReferencesPackageDirectory -recurse
}
else
{
Write-Host "A current or parent folder with a .sln file could not be located."
}
Write-Host "End Init.ps1"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
47199 次 |
| 最近记录: |