简体   繁体   English

如何在msbuild内置目标(如Build / Rebuild)上设置条件?

[英]How do I put a condition on msbuild built-in targets like Build/Rebuild?

I am working differentially building a huge monolithic solution that includes about 80 projects. 我正在以不同的方式构建一个庞大的整体解决方案,其中包括约80个项目。 In my build pipeline right now I include a step to build the entire solution. 现在,在我的构建管道中,我包括构建整个解决方案的步骤。 But what I'd like to do is to build the solution but provide conditions as msbuild arguments so that I can exclude some of the projects that might not have any changes associated with them. 但是我想做的是构建解决方案,但提供条件作为msbuild参数,以便我可以排除一些可能没有任何更改的项目。 I already have scripts to go through my commits and realize what changed and which projects need to be built. 我已经有了脚本来进行提交,并意识到更改了哪些内容以及需要构建哪些项目。

I just need a way to send that info to MSBuild so that it does not build all projects everytime. 我只需要一种将该信息发送到MSBuild的方法,这样它就不会每次都构建所有项目。 I tried building projects separately but that takes a whole lot more time than just building the solution together. 我尝试单独构建项目,但是这比在一起构建解决方案要花费更多的时间。

So, I'm looking for any solutions out there through which I can specify to MSBuild that skip a specific project would help a lot. 因此,我正在寻找可用于指定MSBuild跳过特定项目的解决方案的所有解决方案。 Thanks much! 非常感谢!

I already have scripts to go through my commits and realize what changed and which projects need to be built. 我已经有了脚本来进行提交,并意识到更改了哪些内容以及需要构建哪些项目。

Since I could get clearly know that which script are you using to realize what changed and which projects need to be built. 因为我可以清楚地知道您正在使用哪个脚本来实现更改内容以及需要构建哪些项目。 I am assuming that you are using MSbuildTarget script which in the xx.csproj to do these judgement. 我假设你正在使用MSbuildTarget脚本在xx.csproj做这些判断。

=If I did not have misunderstanding, you can get help from this similar issue (See ilya's answer). =如果我没有误会,可以从这个类似的问题中获得帮助(请参见ilya的答案)。

See this document and you'll find the build action is performed by these three targets, BeforeBuild,CoreBuild and AfterBuild. 参阅本文档 ,您将发现构建动作是由这三个目标(BeforeBuild,CoreBuild和AfterBuild)执行的。 So assuming you have a target to go through my commits and realize what changed and if a project need to be built, you can add script like below to xx.csproj : 因此,假设您有一个目标可以完成我的提交并了解更改的内容,并且如果需要构建项目,则可以将以下脚本添加到xx.csproj

<PropertyGroup>
    <BuildWrapperDependsOn>$(BuildDependsOn)</BuildWrapperDependsOn>
    <BuildDependsOn>CheckIfBuildIsNeeded;BuildWrapper</BuildDependsOn>
  </PropertyGroup>

  <Target Name="CheckIfBuildIsNeeded">
    <!-- Execute command here that checks if proceed with the build and sets the exit code -->
    <Exec Command="exit /b 1" WorkingDirectory="$(SourcesPath)" IgnoreExitCode="true">   
      <Output TaskParameter="ExitCode" PropertyName="ExecExitCode"/>
    </Exec>
    <Message Text="Exit Code: $(ExecExitCode)" Importance="high" />
    <PropertyGroup Condition="'$(ExecExitCode)' == '1'">
      <DoBuild>false</DoBuild>
    </PropertyGroup>
  </Target>

  <Target Name="BuildWrapper" Condition=" '$(DoBuild)' != 'false' " DependsOnTargets="$(BuildWrapperDependsOn)" Returns="$(TargetPath)" />

Above is the script from ilys , and hope my description can help you understand it. 上面是ilys的脚本,希望我的描述可以帮助您理解它。 With this script, when we start a build target, it will firstly run the targets it depends on, so it will run the CheckIfBuildIsNeeded target and BuildWrapper target. 使用此脚本,当我们启动构建目标时,它将首先运行其依赖的目标,因此它将运行CheckIfBuildIsNeeded目标和BuildWrapper目标。 And only when the DoBuild property is true, the BuildWrapper will actually execute. 并且只有当DoBuild属性为true时,BuildWrapper才会真正执行。 And since buildwrapper depends on original $(BuildDependsOn), it will continue the real build process. 由于buildwrapper依赖于原始的$(BuildDependsOn),它将继续实际的构建过程。

The total logic is: Run CheckIfBuildIsNeeded script and output value to indicates whether need to build=>Try to Run BuildWrapper=>IF need to build, then run the real build success(BeforeBuild, Corebuild,Afterbuild), if the value is false, finish the build process. 总逻辑为:运行CheckIfBuildIsNeeded脚本并输出值以指示是否需要构建=>尝试运行BuildWrapper =>如果需要构建,则运行真实的构建成功(BeforeBuild,Corebuild,Afterbuild),如果该值为false,完成构建过程。 So I think you can do some little changes to this script then it can work for your situation. 因此,我认为您可以对此脚本进行一些小的更改,然后它就可以适合您的情况。 (Not sure what your script looks like, I can't complete it for you) (不确定您的脚本是什么样,我无法为您完成脚本)

And since you have many projects, you don't need to add this script to every project manually. 而且由于您有许多项目,因此无需手动将此脚本添加到每个项目中。 You can create a Directory.Build.props file, copy the script into it, and place the file in solution folder, then it will work for all projects in the solution. 您可以创建Directory.Build.props文件,将脚本复制到其中,然后将该文件放置在解决方案文件夹中,然后它将适用于解决方案中的所有项目。

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

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