简体   繁体   English

MSbuild构建订单问题 - 首先构建预先构建步骤或首先依赖项目

[英]MSbuild build order issue - pre-build steps first or dependent projects first

I have a project A depending on project B. Project A has some pre-build tasks that is dependent of some generated files from project B. When I build in Visual Studio, no problem. 我有一个项目A取决于项目B.项目A有一些预构建任务依赖于项目B中的一些生成的文件。当我在Visual Studio中构建时,没有问题。 But when using MSBuild.exe, then there is problem because the build order is: 但是当使用MSBuild.exe时,则存在问题,因为构建顺序是:

  • A's pre-build steps <- failed because B hasn't been compiled A的预构建步骤< - 失败,因为B尚未编译
  • B is compiled <- expected to be executed first B编译< - 预计首先执行
  • A is compiled A是编译的

Is it the expected behaviour using MSBuild? 是使用MSBuild的预期行为吗? Is there a way to tell MSBuild to do B first before A's prebuild steps? 有没有办法告诉MSBuild在A的预建步骤之前先做B?

I am using VS2010 C# and C++/CLI. 我正在使用VS2010 C#和C ++ / CLI。 I don't think if offeres additional info but here is how it is called: 我不认为是否提供额外的信息,但这是如何被称为:

Running process (C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBUILD.exe "..\..\..\dev\build\MyProj.sln" /t:Clean /p:Configuration=Release;Platform=Win32)

Short answer 简短的回答

Remove your build event, and add something like the following to your project (.csproj): 删除构建事件,并在项目中添加以下内容(.csproj):

<Target Name="AfterResolveReferences">
  <Exec Command="echo helloworld" />
</Target>

More info 更多信息

You can read about customising the build process here: http://msdn.microsoft.com/en-us/library/ms366724%28v=vs.110%29.aspx 您可以在此处阅读有关自定义构建过程的信息: http//msdn.microsoft.com/en-us/library/ms366724%28v=vs.110%29.aspx

The Before Build event is fired before ResolveReferences, so if the project you reference has not already been built by the time it comes to your project's BeforeBuild event, then your BeforeBuild event will fail. 在ResolveReferences之前触发Before Build事件,因此如果您在项目的BeforeBuild事件发生时尚未构建您引用的项目,则BeforeBuild事件将失败。

To overcome this, you should use a different entry point to customize the build process. 要解决此问题,您应该使用不同的入口点来自定义构建过程。 In the above example, I use AfterResolveReferences, since this will ensure that all projects which you reference are already built. 在上面的示例中,我使用AfterResolveReferences,因为这将确保您已经构建了所引用的所有项目。

Jack's answer seems to be working, but what I did not like about it is that support for editing the .csproj is not natively supported in the VS UI, other than the awkward "Unload project, Edit project (during which you can't click on your files as you normally would like to), Reload project" model. 杰克的答案似乎有效,但我不喜欢的是,除了尴尬的“卸载项目,编辑项目(在此期间无法点击”)外,VS UI中本身不支持编辑.csproj的支持你通常想要的文件,重新加载项目“模型。 What I wanted was for the pre-build event to trigger after dependent projects built, and have this work the same in VS as in MSBuild. 我想要的是在构建依赖项目之后触发预构建事件,并且在VS中和MSBuild中的工作相同。 After struggling with this problem, I found a solution that works for me in MSBuild 4.0. 在努力解决这个问题后,我找到了一个适用于MSBuild 4.0的解决方案。

No matter what I tried, I could not alter the PreBuildEvents target to trigger after the dependent projects finished building. 无论我尝试了什么,我都无法改变PreBuildEvents目标,以便在依赖项目完成构建后触发。 So what I did instead was to disable the PreBuildEvents target, and create a bastardized custom PreBuildEvents target that would run at the appropriate time: 所以我做的是禁用PreBuildEvents目标,并创建一个在适当的时间运行的私有自定义PreBuildEvents目标:

<ItemGroup>
  <ProjectReference Include="..\YourProjectPath\YourProject.csproj">
    <ReferenceOutputAssembly>false</ReferenceOutputAssembly>
  </ProjectReference>
</ItemGroup>
<Target Name="PreBuildEvent" AfterTargets="" BeforeTargets="" />
<Target Name="BastardPreBuildEvent" AfterTargets="ResolveReferences" BeforeTargets="CoreResGen">
  <Exec Command="$(PreBuildEvent)" />
</Target>

this is rather old issue but I've found this simple solution (it may apply for you too) Just add 这是一个相当老的问题,但我发现这个简单的解决方案(它也可能适用于你)只需添加

<PreBuildEventDependsOn>ResolveReferences</PreBuildEventDependsOn>

before 之前

<PreBuildEvent>

Unfortunately this must be done in editor, VS editor does not support it (at version 15.6.2). 不幸的是,这必须在编辑器中完成,VS编辑器不支持它(版本15.6.2)。

This may be old but I recently ran into the same problem. 这可能是旧的,但我最近遇到了同样的问题。 Jack's answer was what I was trying to use but the ResolveReferences target is called when loading the projects in visual studio and this was bad for me because I was doing a fairly long process during my target that I only wanted to do before the build. 杰克的回答是我试图使用的,但在Visual Studio中加载项目时会调用ResolveReferences目标,这对我来说很糟糕 ,因为我在目标期间做了一个相当长的过程,我只想在构建之前做。

The solution I ended up using was similar to VeeTheSecond but a bit less involved: 我最终使用的解决方案类似于VeeTheSecond,但涉及的有点少:

<Target Name="BeforeBuild" DependsOnTargets="ResolveReferences">
    ... exec something using a dependant project's output exe ...
</Target>

This would force the BeforeBuild target to wait until all dependant projects are actually done building so the exec command can use the dependant project's output. 这将强制BeforeBuild目标等待所有依赖项目实际构建完成,因此exec命令可以使用依赖项目的输出。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM