.net - nuget 内容/内容文件在嵌套的 nuget 包中重复
问题描述
我有 3 个相互嵌套的项目。
HtAlarms.Common
- 有配置文件
HtAlarms.config
- 从这里创建一个 nuget 包:
HtAlarms.Common.nuget
- 有配置文件
HtOpc.Alarms
- 包含
HtAlarms.Common.nuget
包 - 从这里创建一个 nuget 包:
HtOpc.Alarms.nuget
- HtAlarms.config 错误地包含在 nuget 包中!
- 包含
HtServer.ServerTemplate
- 包含
HtOpc.Alarms.nuget
包- HtAlarms.config 错误地包含在根目录中!
- 包含
HtAlarms.Common
具有以下目录结构:
+-- Config
| +-- NLog
| | +-- HtAlarms.config
HtAlarms.Common.csproj
...
<ItemGroup>
<Content Include="Config\NLog\HtAlarms.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
...
HtOPC.Alarms
具有以下目录结构:
+-- Config
| +-- NLog
| | +-- HtAlarms.config
HtOPC.Alarms.csproj
...
<ItemGroup>
<PackageReference Include="HtAlarms.Common" Version="6.0.0-fix-htalarms-config.68" />
</ItemGroup>
...
但是nuget
包裹已经有副本了!?
HtServer.ServerTemplate
具有以下目录结构:
+-- Config
| +-- NLog
| | +-- HtAlarms.config
+-- HtAlarms.config
HtAlarms.Common.nuget
安装Config/NLog/HtAlarms.config
-对
HtOPC.Alarms.nuget
安装HtAlarms.config
-错误
也是。CopyToOutputDirectory
_ false
应该是true
。
有人可以告诉我这里有什么问题吗?谢谢!!
解决方案
有人可以告诉我这里有什么问题吗?谢谢!!
据我所知,在Net Standard项目中(新的sdk格式项目,打包时只能通过PackageReference格式安装),当你把Build Action
文件改成Content
,然后点击打包,nuget会把这些文件放入nuget 包文件夹 ( HtAlarms.Commonxxxxx.nupkg
) 中的 ContentFiles 文件夹。
当您将此软件包安装到新项目时,文件ContentFiles
夹中的这些文件将自动复制到新项目中。
它与Build Action
而不是有关CopyToOutputDirectory
。
有了您的进一步信息,您不希望此文件出现在第二个 nuget 包中HtOpc.Alarms
,我建议您可以试试这个:
解决方案
1)将项目中文件的Build Action
改为first然后在 item 下输入要添加的文件。HtAlarms.config
HtAlarms.Common
None
HtAlarms.Common.csproj
<pack>true</pack>
HtAlarms.config
然后单击打包并重新打包名为HtAlarms.Common.nuget
.
2)然后进入第二个项目HtOpc.Alarms
,首先卸载旧的nuget包HtAlarms.Common.nuget
。其次,在将新的 nuget 包安装到项目之前清理 nuget 缓存。如果您安装了错误的 nuget 版本,这一点很重要。此步骤不添加,您可以直接重新打包这个新项目。HtAlarms.Common.nuget
HtOpc.Alarms
<pack>true</pack>
HtOpc.Alarms.csproj
HtOpc.Alarms.nuget
3)对于第三个项目,您可以尝试与第二步相同的步骤安装HtOpc.Alarms.nuget
到HtServer.ServerTemplate
项目中。
更新 1
经过我的测试,我发现当我将nuget包安装到一个新项目中 Copy to Output Directory
时,新导入的文件的默认HtAlarms.config
设置为Never
。我认为这是打包按钮的效果,使用默认设计的xxxx.nuspec
文件来打包你的项目,当你使用xxx.nuspec
文件时,它会忽略Copy to Output Directory
文件的。
所以当你打包nuget包时HtAlarms.Common.nuget
,打开它然后添加copyToOutput="true"
到HtAlarms.Common.nuspec
文件中(使用zip工具打开HtAlarms.Common.1.0.0.0.nupkg
)
1)将此节点添加到文件中HtAlarms.Common.nuspec
copyToOutput="true"
另外,保存这个操作,清理nuget缓存,然后重新安装HtAlarms.Common.nuget
到HtOpc.Alarms
项目中。
更新 2
由于您以后无法修改 nuget 包,因此我认为您使用nuget.exe cli将项目与xxxx.nuspec
文件一起打包,而不是使用打包按钮。
首先,您应该下载nuget.exe,然后按照此指南将其配置到环境变量中,然后您可以在CMD中调用nuget。
1)打包项目时,不要使用Pack,打开CMD,先输入HtAlarms.Common.csproj存在的路径(如cd xxxxxxxx\HtAlarms.Common
)。
2)键入 nuget spec 生成包描述文件HtAlarms.Common.nuspec
。在此文件中,您可以根据需要命名包 id、版本、作者、描述等来打包包。
例如,就像这样:
请记住将contentfiles
节点和files
节点添加到其中:
<?xml version="1.0"?>
<package >
<metadata>
<id>$id$</id>
<version>$version$</version>
<title>$title$</title>
<authors>$author$</authors>
<owners>$author$</owners>
<licenseUrl>http://LICENSE_URL_HERE_OR_DELETE_THIS_LINE</licenseUrl>
<projectUrl>http://PROJECT_URL_HERE_OR_DELETE_THIS_LINE</projectUrl>
<iconUrl>http://ICON_URL_HERE_OR_DELETE_THIS_LINE</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>1</description>
<releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
<copyright>Copyright 2020</copyright>
<tags>Tag1 Tag2</tags>
<contentFiles>
<files include="Config\NLog\HtAlarms.config" buildAction="None" copyToOutput="true" flatten="true" />
</contentFiles>
</metadata>
<files>
<file src="Config\NLog\HtAlarms.config" target="ContentFiles\any\Config\NLog" />
<file src="Config\NLog\HtAlarms.config" target="Content\Config\NLog" />
</files>
</package>
3)在输入nuget pack HtAlarms.Common.csproj
CMD 生成新的 nuget 包之前HtAlarms.Common.nuget
,你应该重建你的项目。
当您在其他项目中重新安装它时,请记住先清理 nuget 缓存。
然后您也可以将此 nuget 包格式用于HtOpc.Alarms
项目。
之后,您可以直接将这个 nuget 包安装到项目中。
推荐阅读
- java - 有没有办法以编程方式启动 eclipse 导出功能
- typescript - 如何“说服” TypeScript 该属性存在?
- javascript - React - 无法设置未定义的属性
- r - igraph、POSIX 和 data.table
- python - 将 DataFrame 索引设置为 DateTime
- excel - 对于每个无法打印 pdf 的工作表
- node.js - mongodb数据库保存成功后如何过滤
- python - 在 pyCharm 上编译 pcapy
- python-3.x - 在 gunicorn 工作人员之间共享单个文件
- python - 仅散点图 绘制最后一次迭代