MSBuild:如何根据生成项目的名称有条件地导入 .csproj 文件中的另一个项目?

发布于 2025-01-18 08:42:17 字数 388 浏览 6 评论 0原文

上下文:我有一个 .Net 标准 2.0 项目,例如 ProjectA,其中我使用 语句从目标文件导入项目。该解决方案有许多项目(ProjectA、ProjectB、ProjectC 和 ProjectD)。使用项目引用在项目 B 和项目 C 中引用项目 A。

问题是:如果构建/重建请求源自特定项目(例如 ProjectC),我想忽略此导入语句并正常构建 ProjectA。如果构建请求源自项目 B 或项目 A,我想包含此导入。我了解 可以有一个 “条件” 属性。但我如何实现这个想法呢?有什么建议吗?

Context: I have a .Net standard 2.0 project, say ProjectA in which I use an <Import Project="xxxx.targets" /> statement to import a project from a target file. This solution has many projects (ProjectA, ProjectB, ProjectC and ProjectD). Project A is referenced in Project B and Project C using project references.

Here is the problem: If the build/rebuild request originated from a specific project, say ProjectC, I want to ignore this import statement and build the ProjectA normally. If the build request is originated from Project B or from Project A, I want to include this import.I understand that <Import Project="xxxx.targets" /> can have a "Condition" attribute. But how do I implement this idea? Are there any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凝望流年 2025-01-25 08:42:17

如果尚未构建引用的项目,则 ProjectReference 将导致构建该项目。不支持根据需要该项目的项目来更改引用项目的构建。如果可以的话,就会产生各种各样的问题。当从解决方案构建时,ProjectB 或 ProjectC 可能会触发构建 ProjectA,并且由于 ProjectA 将构建一次,因此将为其中一个项目“错误”构建它。

对于给定的配置/平台,项目应该始终构建相同的。

但您可以创建新的单独配置。

例如:

  1. 在默认的“发布”和“调试”配置中,禁用 ProjectC。
  2. 创建新的“ProjectC-Release”和“ProjectC-Debug”配置。
  3. 在“ProjectC-Release|Debug”配置中,禁用 ProjectB。
  4. 在 ProjectA(ProjectC 和 ProjectB 引用的项目)中,根据 Configuration 属性的值对 Import 进行条件化。

(但是,我怀疑您提出的问题并不是您真正想要解决的问题。)

The ProjectReference will cause a referenced project to be built if it is not already built. There is no support for changing the build of a referenced project based on the project that needs the project. And if you could that would create all sorts of issues. When building from the solution either ProjectB or ProjectC could trigger building ProjectA and, since ProjectA will be built once, it will be built 'incorrectly' for one of the projects.

For a given configuration/platform a project should always build the same.

But you can create new separate configurations.

For example:

  1. In the default 'Release' and 'Debug' configurations, disable ProjectC.
  2. Create new 'ProjectC-Release' and 'ProjectC-Debug' configurations.
  3. In the 'ProjectC-Release|Debug' configurations, disable ProjectB.
  4. In ProjectA (the project referenced by both ProjectC and ProjectB), conditionalize the Import based on the value of the Configuration property.

(However, I suspect that the question you have asked is not really the issue you are trying to solve.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文