简体   繁体   English

如何在编译主项目之前编译另一个项目?

[英]How to compile another project before compiling main project?

I have a solution with two executing projects.我有一个包含两个执行项目的解决方案。 Users can execute project B from project A by pressing a button.用户可以通过按一个按钮从项目 A 执行项目 B。 Project A Debug folder contains the project B Debug folder, so it executes project B by the hardcoded path to that folder.项目 A Debug 文件夹包含项目 B Debug 文件夹,因此它通过该文件夹的硬编码路径执行项目 B。 If I want to make some changes in project B - I need to recompile project B and replace its Debug folder in project A Debug folder.如果我想在项目 B 中进行一些更改 - 我需要重新编译项目 B 并在项目 A 的 Debug 文件夹中替换它的 Debug 文件夹。 I think it's not a smart and comfortable way from the point of architecture view.我认为从架构的角度来看,这不是一种聪明而舒适的方式。 So I want to include such actions in project A build events, but I have never done it before, so I would be happy to hear your suggestions.所以我想在项目 A 构建事件中包含这样的操作,但我以前从未这样做过,所以我很高兴听到你的建议。

The easiest way to achieve this is to create a reference from A to B, so you coud access the (public) members of B from project A. This way, you could also run the code of B in the process of A, eliminating the need to start a separate process.最简单的实现方式是创建一个从A到B的引用,这样你就可以从项目A访问B的(公共)成员。这样,你也可以在A的过程中运行B的代码,消除了需要启动一个单独的进程。

If you do have the requirement that you need to have a separate exe for B that runs in a separate process, there is another approach that requires more effort:如果您确实需要为在单独进程中运行的 B 提供单独的 exe,则还有另一种方法需要更多努力:

  1. Do not create a reference from A to B, but use a Project Dependency* to make sure that B is compiled before A. You can configure project dependencies in a dialog that you access in the context menu of the solution or in the application menu Project -> Project Dependencies.不要创建从 A 到 B 的引用,而是使用Project Dependency*来确保 B 在 A 之前编译。您可以在解决方案的上下文菜单或应用程序菜单 Project 中访问的对话框中配置项目依赖项-> 项目依赖。 After setting the dependency, you can check the build order on the second tab of the dialog.设置依赖项后,您可以在对话框的第二个选项卡上检查构建顺序。
  2. Adjust the build of A to copy the output of B to the output folder.调整 A 的构建以将 B 的输出复制到输出文件夹。 There are several ways to do this, one of the more advanced options is to add Build Targets to the project file of A that perform the copy and clean for the files.有几种方法可以做到这一点,更高级的选项之一是将构建目标添加到 A 的项目文件中,以执行文件的复制和清理。 You have to unload project A and edit the project file (see context menu) and add code similar to the following:您必须卸载项目 A 并编辑项目文件(请参阅上下文菜单)并添加类似于以下内容的代码:

  <Target Name="AfterBuild">
    <ItemGroup>
      <AdditionalFiles Include="$(MSBuildProjectDirectory)\..\ProjectB\bin\$(Configuration)\ProjectB.*" />
    </ItemGroup>
    <Copy SourceFiles="@(AdditionalFiles)" DestinationFolder="$(OutputPath)" />
  </Target>
  <Target Name="AfterClean">
    <ItemGroup>
      <AdditionalFilesToDelete Include="$(OutputPath)\ProjectB.*" />
    </ItemGroup>
    <Delete Files="@(AdditionalFilesToDelete)" />
  </Target>

Please see the documentation on build targets and note that the code above is only a sample that you need to adjust to your specific needs.请参阅有关构建目标文档,并注意上面的代码只是您需要根据特定需求进行调整的示例。 For instance, it only works with the standard output paths.例如,它仅适用于标准输出路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建一个到主项目MVC到另一个项目MVC的路由? - How can create a route to main project MVC to another project MVC? 从另一个项目编译整个项目 - Compile entire project from another project 将一个C#项目编译成另一个 - Compiling one C# Project into another 编译项目中的resx文件或只使用resgen而不编译resx - Compile resx files in the project or just use resgen without compiling resx 如何以编程方式从当前项目中构建和编译另一个c#项目 - How to programatically build and compile another c# project from the current project 在Windows Phone解决方案中在主项目中启动另一个项目 - Starting another project within main project in a solution for windows phone Visual Studio可以将项目引用编译到与主.exe不同的文件夹中 - Can Visual Studio compile project references into a different folder then the main .exe 如何在不编译项目的情况下获取类的元数据 - How to get metadata of classes without compiling the project 在DLL中编译类并在C#中的另一个项目中引用它 - Compiling class in DLL and referencing it in another project in C# 编译内容项目时出错 - Error compiling content project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM