C# 在一个解决方案中从多个项目创建一个 NuGet 包
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15882770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating one NuGet package from multiple projects in one solution
提问by Logarr
I have a solution that I'm working on that contains 4 class library projects (A
, B
, C
, D
). A
and B
could be considered the top level projects in the solution. Both A
and B
reference C
, and D
stands alone.
我有一个正在处理的解决方案,其中包含 4 个类库项目(A
、B
、C
、D
)。A
并且B
可以被认为是解决方案中的顶级项目。既A
和B
参考C
,又D
独立。
These four projects represent a group of services that I have made that handle an automated workflow. They are all closely related, and will only be used in one location (the service manager) so I don't want to split them into different solutions.
这四个项目代表了我制作的一组处理自动化工作流的服务。它们都是密切相关的,并且只会在一个位置(服务管理器)使用,所以我不想将它们分成不同的解决方案。
My problem is that I want to create a single NuGet package that will contain all 4 libraries, without having to build them all and gather up their DLLs manually. I know that I could technically achieve this by having either A
or B
reference the remaining projects, but that's not a true relationship and I feel it should be avoided.
我的问题是我想创建一个包含所有 4 个库的 NuGet 包,而不必手动构建它们并收集它们的 DLL。我知道我可以通过拥有A
或B
引用其余项目在技术上实现这一点,但这不是真正的关系,我觉得应该避免这种关系。
I've done a lot of searching on this problem and I can't find a solution other than manually collecting the DLLs and building the package myself. Is there a way to achieve the result that I want using NuGet's features/abilities?
我已经对这个问题进行了大量搜索,除了手动收集 DLL 并自己构建包之外,我找不到其他解决方案。有没有办法使用 NuGet 的特性/能力来实现我想要的结果?
NOTE: In case the tags don't make it clear I'm currently using VS2010 with a TeamCity build server. In case it's relevant I'm also using Git through a Stash server.
注意:如果标签没有说清楚,我目前正在使用 VS2010 和 TeamCity 构建服务器。如果相关,我还通过 Stash 服务器使用 Git。
EDIT: I just realized this might be important enough to mention. These projects do reference other NuGet packages that I will need to mark as dependencies.
编辑:我刚刚意识到这可能很重要,值得一提。这些项目确实引用了我需要标记为依赖项的其他 NuGet 包。
回答by Ale? Roubí?ek
You have to define your own nuspec manifest. You can list containing assemblies in files
section:
您必须定义自己的 nuspec 清单。您可以在files
节中列出包含程序集:
<file src="A\bin\Release\A.dll" target="lib\net40" />
<file src="B\bin\Release\B.dll" target="lib\net40" />
...
For more details read NuSpec reference.
有关更多详细信息,请阅读NuSpec 参考。
Then reference that nuspec file in NuPack build step instead of proj.
然后在 NuPack 构建步骤而不是 proj 中引用该 nuspec 文件。
回答by Que
If you have downloaded NuGet.exeYou can run: nuget pack Myproject.csproj -IncludeReferencedProjects
and this should include all of your projects. Here's a note from the NuGet docs:
如果您已下载NuGet.exe您可以运行:nuget pack Myproject.csproj -IncludeReferencedProjects
这应该包括您的所有项目。这是 NuGet文档中的注释:
If the project references other projects, you can add the referenced projects as part of the package, or as dependencies with -IncludeReferencedProjects option. This is done recursively. For example, suppose you have project A.csproj, which references B.csproj and C.csproj, while B.csproj references D.csproj & E.csproj, C.csproj references F.csproj & G.csproj. Then, when you run:
如果项目引用了其他项目,您可以将引用的项目添加为包的一部分,或者使用 -IncludeReferencedProjects 选项作为依赖项添加。这是递归完成的。例如,假设您有项目 A.csproj,它引用 B.csproj 和 C.csproj,而 B.csproj 引用 D.csproj & E.csproj,C.csproj 引用 F.csproj & G.csproj。然后,当您运行时:
nuget pack A.csproj -IncludeReferencedProjects
the generated package will contain files from projects B, C, D, E, F & G, in addition to files from project A.
生成的包将包含来自项目 B、C、D、E、F 和 G 的文件,以及来自项目 A 的文件。
If a referenced project has a corresponding nuspec file with the same name, then that referenced project is added as a dependency instead. Using the same example, suppose now there is file C.nuspec in the same directory as project file C.csproj. When you run:
如果引用的项目具有相应的同名 nuspec 文件,则将该引用的项目添加为依赖项。使用相同的示例,假设现在在与项目文件 C.csproj 相同的目录中有文件 C.nuspec。当你运行时:
nuget pack A.csproj -IncludeReferencedProjects
the generated package will contain files from projects B, D, E, in addition to files from project A, and the package has dependency on C.
生成的包将包含来自项目 B、D、E 的文件,以及来自项目 A 的文件,并且该包依赖于 C。
Please also see the Command line reference.
另请参阅命令行参考。
回答by C.Frank
this probably will solve this exact: nuget.exe pack proj.csproj -IncludeReferencedProjects
这可能会解决这个问题:nuget.exe pack proj.csproj -IncludeReferencedProjects
you can see: Create nuget package for a solution with multiple projects
您可以看到: 为具有多个项目的解决方案创建 nuget 包