简体   繁体   中英

How to include resx file in a Nuget package

I have a.Net Framework 4.6.1 project from which I make a nuget package. This nuget package simply installs 2 dlls and a couple of content files which is working fine.

The problem is that I now added resource string via a resx file that resides in:

~\App_Data\Global\Resources\resource-strings.resx

And I don't know how to make that file be part of the nuget package properly. When I create the.nupkg, I see the resx file there, but when I install it on another project, the resx file should be copied to the App_Data\Global\Resources folder, but it is not.

Is it possible?

From what I investigated, I think I also have to do something with the targets files + nuspec config, but nothing I have tried works.

I literally just need the resx file copied over. Nothing more complicated than that.

Sure . It is possible and can be done by nuget. Since you want this resource file be copied into your target asp.net project folder, you could try the following steps:

=====================================================

First , if you want to install this net461 nuget package into a net framework asp.net project, you should use content node in xxx.nusepc file

First, make sure that the Build Action of the resource-strings.resx is Embedded Resource rather than Content .

1) first , run cmd command: cd xxxx(project folder path) and then run nuget spec to generate the nuspec file. These are enough:

2) open the nuspec file and add the content node:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
    <metadata>
        <id>xxx</id>
        <version>xxx</version>
        <title>xxx</title>
        <authors>xxx</authors>
        ............       
    </metadata>
    <files>
        <file src="~\App_Data\Global\Resources\resource-strings.resx(the path of the file in net framework 4.6.1 project)" target="content\App_Data\Global\Resources\resource-strings.resx" />
    </files>
</package>

3) then save the nuspec file and run nuget pack to generate the nupkg .

Before you install the nuget package, you should first clean nuget caches to remove the old wrong versions of the nuget.

When you install this package, the file will be copied into the root path App_Data\Global\Resources\resource-strings.resx of the web project.

====================================================

If you want to install this package into new sdk project(Net Core or xxx with PackageReference nuget management format), you could should create a target file with a copy task.

1) add a folder called build in the.net framework 4.6.1 project and then add a file called <Package_id>.props file.

Note that you should make sure that the nuget package's id is the same as the <Package_id>.props . Hint from here .

2) add these in <Package_id>.props :

  <Project>
      <Target Name="CopyFilesToProject" BeforeTargets="Build">
        <Message Text="Copy resource-strings.resx  to project" />
        <ItemGroup>
          <SourceScripts Include="$(MSBuildThisFileDirectory)..\content\**\*.*"/>
        </ItemGroup>
        <Copy
           SourceFiles="@(SourceScripts)"
           DestinationFiles="$(MSBuildProjectDirectory)\%(RecursiveDir)%(Filename)%(Extension)"/>
      </Target>
      
    </Project>

3) Modify the xxx.nuspec file like this:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>xxx</id>
    <version>xxx</version>
    <title>xxx</title>
    <authors>xxx</authors>
    <owners>me</owners>
    ............
  </metadata>
 <files>
<file src="~\App_Data\Global\Resources\resource-strings.resx" target="content\App_Data\Global\Resources\resource-strings.resx" />
<file src="build\xxx(like package_id).props" target="build"/>
</files>

</package>

4) then you should use nuget pack command to pack this project. Before you install this package, you should first clean nuget caches first.

After you install this nuget package, you should build your project to run this custom copy target to copy the file into your main project.

Also , there is a similar issue about this.

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