简体   繁体   English

使用dotnet pack创建带有外部.exe文件的NuGet包

[英]Create NuGet package with external .exe files using dotnet pack

Edit: What I did to solve this: 编辑:我做了什么来解决这个问题:

Created a new NuGet package with the following nuspec: 使用以下nuspec创建了一个新的NuGet包:

<package>
  <metadata>
    <id>VIPSNuget</id>
    <version>1.0.0.0</version>
    <title>$title$</title>
    <authors>Devedse</authors>
    <owners>Devedse</owners>
    <licenseUrl>https://github.com/jcupitt/libvips/blob/master/COPYING</licenseUrl>
    <projectUrl>https://github.com/jcupitt/libvips</projectUrl>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Just the EXE file for VIPS</description>
    <releaseNotes>First release</releaseNotes>
    <copyright>Copyright 2017</copyright>
    <tags>VIPS exe file</tags>
  </metadata>
  <files>
    <file src="VIPS\*.*" target="tools" />
  </files>
</package>

Placed all the .exe files in that folder. 将所有.exe文件放在该文件夹中。

Now from my ImageOptimizer I use the following code: 现在从我的ImageOptimizer我使用以下代码:

    var userProfileDir = System.Environment.GetEnvironmentVariable("USERPROFILE");
    string pathVips = Path.Combine(userProfileDir, Constants.VipsDir, "vips.exe");

Some Background 一些背景

I'm currently working on an Image Processing library that needs to convert an Image from JPEG to PNG using the external tool named VIPS. 我目前正在开发一个图像处理库,需要使用名为VIPS的外部工具将图像从JPEG转换为PNG。 ( https://github.com/jcupitt/libvips ). https://github.com/jcupitt/libvips )。

Since I didn't want to include the binaries from VIPS in my own project I thought, let's make a NuGet package of it where the .exe file can be called from my code. 由于我不想在我自己的项目中包含来自VIPS的二进制文件,我想,让我们创建一个NuGet包,可以从我的代码中调用.exe文件。

Steps I took 我采取的步骤

I create a new dotnet core solution using the 'dotnet new console' command. 我使用'dotnet new console'命令创建了一个新的dotnet核心解决方案。 After that I created a directory named "VIPS" and pasted all binaries in there. 之后我创建了一个名为“VIPS”的目录,并在那里粘贴了所有二进制文件。

I then created a nuspec file containing the following XML: 然后我创建了一个包含以下XML的nuspec文件:

<?xml version="1.0"?>
<package>
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>Devedse</authors>
    <owners>Devedse</owners>
    <licenseUrl>https://github.com/jcupitt/libvips/blob/master/COPYING</licenseUrl>
    <projectUrl>https://github.com/jcupitt/libvips</projectUrl>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Just the EXE file for VIPS</description>
    <releaseNotes>First release</releaseNotes>
    <copyright>Copyright 2017</copyright>
    <tags>VIPS exe file</tags>
  </metadata>
  <files>
    <file src="VIPS\*.*" target="tools" />
  </files>
</package>

I edited my .csproj file so that it only contained this: 我编辑了我的.csproj文件,只包含这个:

<Project Sdk="Microsoft.NET.Sdk">    
  <PropertyGroup>
    <TargetFramework>netstandard1.0</TargetFramework>
    <Version>1.0.4.0</Version>
  </PropertyGroup>    
</Project>

Problem 问题

Whenever I run the 'dotnet pack' command now, everything seems to go fine: 每当我现在运行'dotnet pack'命令时,一切似乎都很顺利: 创建了NuGet包

But all the files from the VIPS directory are not included in my created nupkg: 但是VIPS目录中的所有文件都不包含在我创建的nupkg中: 没有工具目录

(Reason why I chose tools is because in earlier versions of the dotnet framework / NuGet, I used tools like ILRepack that would be automatically installed in a tools directory which could then be called from a relative path of your deployed .exe file). (我选择工具的原因是因为在早期版本的dotnet框架/ NuGet中,我使用了像ILRepack这样的工具,它们会自动安装在工具目录中,然后可以从已部署的.exe文件的相对路径调用它。

(Also see for example the nuspec code in https://github.com/gluck/il-repack/blob/master/build.gradle at line 92 where they also include the created .exe file in the 'tools' target) (另请参阅第92行的https://github.com/gluck/il-repack/blob/master/build.gradle中的nuspec代码,其中它们还包含'tools'目标中创建的.exe文件)

Further investigation 进一步的调查

What I then tried was to add all files from the VIPS directory into my csproj file and set the build action to Content. 然后我尝试将VIPS目录中的所有文件添加到我的csproj文件中,并将构建操作设置为Content。

All binary files now get included in the following 2 directories (so all duplicates which is also strange): vipsnuget.1.0.0.nupkg\\content\\VIPS\\ vipsnuget.1.0.0.nupkg\\contentFiles\\any\\netstandard1.0\\VIPS\\ 所有二进制文件现在都包含在以下2个目录中(因此所有重复项也很奇怪):vipsnuget.1.0.0.nupkg \\ content \\ VIPS \\ vipsnuget.1.0.0.nupkg \\ contentFiles \\ any \\ netstandard1.0 \\ VIPS \\

However when I now include the NuGet package inside another project, and then pack that project it seems to also include all binaries in that package again too: 但是,当我现在将NuGet包包含在另一个项目中,然后打包该项目时,它似乎也会再次包含该包中的所有二进制文件:

大文件大小

(All VIPS binaries are now placed inside: DeveImageOptimizer.1.0.0.nupkg\\contentFiles\\any\\netstandard1.5\\ and again in content\\VIPS) (所有VIPS二进制文件现在放在:DeveImageOptimizer.1.0.0.nupkg \\ contentFiles \\ any \\ netstandard1.5 \\并再次放在content \\ VIPS中)

This isn't really desireable since the actual size of the DLL's in there should only be a few Kb's. 这不是真正需要的,因为那里的DLL的实际大小应该只有几个Kb。

Another issue with this approach 这种方法的另一个问题

I'm not actually sure if there's a good/better solution for this, but currently, when the NuGet package is restored, it will be extracted in %userprofile%.nuget\\packages\\vipsnuget... 我不确定是否有一个好的/更好的解决方案,但目前,当NuGet包恢复时,它将在%userprofile%.nuget \\ packages \\ vipsnuget中提取...

So whenever I want to run a command against the VIPS.exe I need to point the Process.Start arguments to %userprofile%... . 因此,每当我想对VIPS.exe运行命令时,我需要将Process.Start参数指向%userprofile%.... Is there a better way to do this? 有一个更好的方法吗? (Eg find the place where NuGet packages are restored in code?) (例如,找到代码中恢复NuGet包的地方?)

What I would like best 我最想要的是什么

Basically what I would like best, is to create a NuGet package where, if you include it, a certain directory will be copied to the build output of the project. 基本上我最想要的是创建一个NuGet包,如果包含它,某个目录将被复制到项目的构建输出中。

So something like: DeveImageOptimizer\\bin\\debug\\VIPS... 所以像:DeveImageOptimizer \\ bin \\ debug \\ VIPS ......

If that doesn't exist 如果那不存在

If there's no way to do that, I would like to hear what the normal approach would be for putting certain 'tools' inside a NuGet package and calling them from inside C# code. 如果没有办法做到这一点,我想听听将某些“工具”放入NuGet包并从C#代码中调用它们的正常方法。

When you call dotnet pack without any arguments, it uses the .csproj file in the current folder to create a nuget package and ignore any .nuspec files. 当您调用不带任何参数的dotnet pack ,它使用当前文件夹中的.csproj文件来创建nuget包并忽略任何.nuspec文件。 And if look into dotnet pack parameters you will not find how to specify .nuspec directly. 如果查看dotnet pack参数,您将无法找到如何直接指定.nuspec

But, dotnet-pack docs says we can provide MSBuild properties to the dotnet pack command for the packing process. 但是, dotnet-pack文档说我们可以为dotnet pack命令提供MSBuild属性以用于打包过程。 From NuGet metadata properties : NuGet元数据属性

NuspecFile NuspecFile

Relative or absolute path to the .nuspec file being used for packing. 用于打包的.nuspec文件的相对或绝对路径。 Note 注意

If the .nuspec file is specified, it's used exclusively for packaging information and any information in the projects is not used. 如果指定了.nuspec文件,则它专门用于打包信息,并且不使用项目中的任何信息。

NuspecBasePath NuspecBasePath

Base path for the .nuspec file. .nu​​spec文件的基本路径。

So modify your .csproj by adding the following 因此,通过添加以下内容来修改.csproj

<PropertyGroup>
  <NuspecFile>location to your nuspec file</NuspecFile>
</PropertyGroup>

Note: and you always could use nuget pack command directly. 注意:您始终可以直接使用nuget pack命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 dotnet pack可以在不添加其他nuget软件包的内容文件的情况下创建nuget软件包吗? - Can dotnet pack create a nuget package without adding content files from other nuget packages? 无法使用 dotnet CLI 和 nuspec 文件打包 NuGet 包 - Unable to pack a NuGet package using dotnet CLI and nuspec file do.net Pack vs Nuget.exe Pack,版本降级报错 - Dotnet Pack vs Nuget.exe Pack, version downgrade error Dotnet 包包括 nuget 包到 output nuget ZEFE90A8E604A7C840E8ZD03A - Dotnet Pack include nuget packages into output nuget package 使用 .csproj 和 dotnet pack 命令中的 MsBuild 将文件从 Nuget 包复制到输出目录 - Copy files from Nuget package to output directory with MsBuild in .csproj and dotnet pack command Linux 上的 Nuget 和 dotnet - 使用/打包提供的包 - Nuget & dotnet on linux - using/package a provided package 将多个平台打包成一个包,使用 dotnet pack - Pack multiple platforms into one package, using dotnet pack 使用 dotnet pack 创建具有依赖项但没有我自己的代码的 NUGET - Using dotnet pack creates NUGET with dependencies but without my own code 创建包含 .cshtml 文件的 Visual Studio 自定义模板并打包在 Nuget 包中 - Create Visual Studio Custom Template containing .cshtml files and pack in Nuget Package 在Nuget包中包含.exe文件是不好的做法吗? - Is it bad practice to include .exe files in a Nuget package?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM