简体   繁体   中英

Using nuget package to deploy single file

I want to createa nuget package with a single file. Is there a way to package a single file and then instruct the file as to where it should be placed within a Visual Studio project?

I was able to make a nuspec file and package a nuget package which contains the file in question. However, it is not possible to be installed inside of a package.

More specifically: I have a configuration file which should be the same across many projects. I want to be able to install a nuget package which can them be installed to place the configuration file in the correct location.

The nuspec file right now just specifies the basics about the metadata. I then run nuget pack with that nuspec file and the configuration file in the directory. This results in a nuget package with the configuration file in it, which is uninstallable.

Here is what I have in the nuget package now:

在此输入图像描述

and the nuspec file:

<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
  <metadata>
    <id>StyleCopSettings</id>
    <version>1.0.1</version>
    <title>StyleCopSettings</title>
    <authors>Clearspan</authors>
    <owners>Clearspan</owners>
    <description>StyleCopSettings</description>
  </metadata>
</package>

The problem is that you are not referencing the file in question in your nuspec. I have edited your nuspec as follows.

<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
  <metadata>
    <id>StyleCopSettings</id>
    <version>1.0.1</version>
    <title>StyleCopSettings</title>
    <authors>Clearspan</authors>
    <owners>Clearspan</owners>
    <description>StyleCopSettings</description>
  </metadata>
  <files>
        <file src="$pathToYourFile$\styleCopSettings.txt" target="content\Settings" /> 
   </files>
</package>

In order to add a file to a project via a package you must add it to the content directory of your package target="content\\Settings" . The content directory of a nuget package acts like the root directory of the project the package will be installed in ( source ). So, by specifying further directories in our target we can place the file in a specific place. In the above example the styleCopSettings.txt file will be placed in the Settings directory of any project consuming this package. The settings directory will be added as part of the install.

After you have called nuget pack on your nuspec you should end up with something like this

nupkg视图

When you consume the package you will end up with the following.

例

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM