在Visual Studio中构建项目时如何安装Nuget包 [英] How install Nuget package when building project in Visual Studio

查看:125
本文介绍了在Visual Studio中构建项目时如何安装Nuget包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目使用名为wkhtmltopdf的可执行文件,该可执行文件将HTML转换为pdf.我创建了一个NuGet包,该包仅将exe输出到项目的根目录中,以便我的应用程序可以引用和使用它.

My project uses an executable called wkhtmltopdf that converts an HTML into a pdf. I've created a NuGet package that simply output the exe in the root of the project so that it can be referenced and used by my application.

如果我安装nuget软件包,则一切正常,这意味着该可执行文件已添加到我的应用程序中,并且可以使用它.

If I install the nuget package everything works fine, meaning that the executable is added into my application and I can work with it.

问题是我不想提交可执行文件,但我希望如果另一个开发者拉下我的项目并构建它,该可执行文件也将添加到他的项目中(而无需再次安装Nuget软件包).

The problem is that I don't want to commit the executable but I want that if another dev pull down my project and build it, the executable will be added to his project as well (without installing again the Nuget package).

我尝试仅提交对程序包的引用

I've tried by committing only the reference to the package

<package id="Wkhtmltopdf" version="0.12.5" targetFramework="net45">

但是没有用.

.nuspec文件非常简单,仅包含此行(默认行除外)

The .nuspec file is very simple and contains only this line (besides the default ones)

...   
<files>
    <file src="wkhtmltopdf.exe" target="Content\wkhtmltopdf.exe" />
</files>
...

知道我要问的事情是否可能吗?

Any idea if what I'm asking is possible?

推荐答案

知道我的要求是否可行?

Any idea if what I'm asking is possible?

此问题的原因是,您将文件定位到.nuspec文件target="Content\wkhtmltopdf.exe"中的content.

The reason for this issue is that you are target the file to the content in the .nuspec file target="Content\wkhtmltopdf.exe".

根据来自基于约定的工作目录:

我们可以知道基于约定的工作目录content中的文件:内容已复制到项目根目录..它修改了项目文件.

We could to know file in the convention-based working directory content: Contents are copied to the project root.. It modified the project file.

当另一个开发人员下拉您的项目并进行构建时,nuget会将此程序包还原到\packages文件夹. 但是,NuGet不会修改您的项目,就像将程序包下载到\packages文件夹一样,而不是将该程序包安装到您的项目中.

When another dev pull down your project and build it, nuget will restore this package to \packages folder. However, NuGet does not modify your project, it just like download that package to the \packages folder not install this package to your project.

检查我以前的主题以了解更多信息详细信息.

Check my previous thread for some more details.

这就是为什么您看到.nupkg以及在\packages文件夹中看到可执行文件的Content文件夹的原因,而不是在项目文件夹中,而不是您想要的正确位置.

That is the reason why you see the .nupkg and a Content folder with the executable in the \packages folder not in the project file folder, which is not the the right location where you want.

要解决此问题,最简单的方法是在程序包管理器控制台中使用NuGet命令行:

To resolve this issue, The easiest way is using the NuGet command line in the Package Manager Console:

Update-Package -reinstall

在另一个开发人员撤消您的项目后,将软件包引用强制重新安装到项目中.

to force reinstall the package references into project after another dev pull down your project.

当然,您也可以从根本上解决此问题,就像无光注释一样,将可执行文件定位到工具,例如target="Tools\wkhtmltopdf.exe",并将.targets文件目标添加到\buildtarget="Build\NuGetPackageName.targets" ,将可执行文件复制到您的项目文件中.如果您对此感兴趣或有任何问题,我想分享您的一些更多详细信息.

Of course, you can also resolve this issue from the root cause, just like matt comment, target your executable file to the Tools, like target="Tools\wkhtmltopdf.exe" and add a .targets file target to the \build, target="Build\NuGetPackageName.targets", to copy executable file to your project file. If you are interested in it or you have any issue about this, I would like share your some more details info about this.

更新:

现在,我向您展示如何使用Tools文件夹和.targets文件.

Now I show you how to do it with Tools folder and .targets file.

这是我的.nuspec文件:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>MyToolPackage</id>
    <version>1.0.0</version>
    <authors>Tester</authors>
    <owners>Tester</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Package description</description>
    <releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
    <copyright>Copyright 2018</copyright>
    <tags>Tag1 Tag2</tags>
  </metadata>

  <files>
    <file src="wkhtmltopdf.exe" target="Tools\wkhtmltopdf.exe" />
    <file src="MyToolPackage.targets" target="Build\MyToolPackage.targets" />
  </files>

</package>

注意:.targets文件的名称应与您的软件包名称相同,例如,我的软件包名称为 MyToolPackage ,因此.targets的名称应为MyToolPackage.targets.

Note: The name of your .targets file should be same as your package name, like, my package name is MyToolPackage, so the name of the .targets should be MyToolPackage.targets.

MyToolPackage.targets文件的内容:

The content of the MyToolPackage.targets file:

<!--
***********************************************************************************************
Copy the wkhtmltopdf.exe to the bin folder of project.
***********************************************************************************************
-->
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <Target Name="CopyTool" BeforeTargets="Build">
    <Message Text="Copy tool to bin folder."></Message>
    <Copy
    SourceFiles="$(SolutionDir)packages\MyToolPackage.1.0.0\Tools\wkhtmltopdf.exe"
    DestinationFolder="$(ProjectDir)bin"
        />
  </Target>

</Project>

打包此.nuspec文件,您将获得以下软件包:

Pack this .nuspec file, you will get following package:

然后将此软件包安装到您的项目中,当您构建项目时,Visual Studio将在构建开始之前在MyToolPackage.targets文件中执行复制任务,当然,wkhtmltopdf.exe将被复制到bin文件夹中. ,您可以将其复制到任何其他文件夹,只需更改MyToolPackage.targets文件中的DestinationFolder.

Then install this package to your project, when you build your project, Visual Studio will execute the copy task in the MyToolPackage.targets file before the start of the building, wkhtmltopdf.exe will be copied to the bin folder, of course, you can copy it to any other folder, just need to change the DestinationFolder in the MyToolPackage.targets file.

希望这会有所帮助.

这篇关于在Visual Studio中构建项目时如何安装Nuget包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆