[英]How do I call my script before all the builds for only once in a multi-targeting project
I would like to run my powershell script for only one time before the build process. 在构建过程之前,我只想运行一次Powershell脚本。 In my mind this should be easily done, simply calling the script before the PreBuildEvent would be OK. 在我看来,应该很容易做到,只需在PreBuildEvent确定之前调用脚本即可。 Well, it does work for normal projects. 好吧,它确实适用于普通项目。
However, for multi-targeting projects,the script would be called multiple times before each build for all the targeting framework. 但是,对于多目标项目,在针对所有目标框架进行每次构建之前,都会多次调用该脚本。
Here is my project file, in which I target at 3 frameworks: 这是我的项目文件,其中针对3个框架:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net40;net45;netstandard1.4</TargetFrameworks>
<AssemblyName>abc</AssemblyName>
<Version>1.0.0</Version>
</PropertyGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net40' ">
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="PowerShell -ExecutionPolicy Unrestricted -File script.ps1/>
</Target>
</Project>
And when I build the project, the PreBuild target was called 3 times. 当我构建项目时,PreBuild目标被调用了3次。
So far, I have tried: 到目前为止,我已经尝试过:
1) At first, I guessed that the builds was going in a sequence, so I added a condition to my target: 1)首先,我猜想构建是按顺序进行的,所以我向目标添加了一个条件:
<Target Name="PreBuild" BeforeTargets="PreBuildEvent" Condition=" '$(TargetFramework)' == 'net40' ">
<Exec Command="PowerShell -ExecutionPolicy Unrestricted -File script.ps1/>
</Target>
, which did not work. ,这没有用。 It turned out that mutli-target builds were going concurrently. 事实证明,多目标构建是同时进行的。 Sometimes, net40's build would be later, so my script was not running before all the builds. 有时,net40的构建会晚一些,所以我的脚本在所有构建之前都没有运行。
2) And then I tried to use environment variables to do the synchronization, but it did not work out, either. 2)然后,我尝试使用环境变量进行同步,但是也没有成功。 It seemed the builds didn't share the environment variables. 似乎这些构建没有共享环境变量。
3) At last, I turned to other MSBuild targets to replace the PreBuildEvent, but I have found no proper one. 3)最后,我转向其他MSBuild目标来替换PreBuildEvent,但没有找到合适的目标。
So how do I call my scripts before all the builds and for only once in a multi-targeting project. 因此,在多目标项目中,如何在所有构建之前以及仅一次调用脚本。 Please help me. 请帮我。
You can hook in your target into the multi-targeting msbuild target that will call the TargetFramework-specific builds like this: 您可以将目标连接到多目标msbuild目标中,该目标将调用TargetFramework特定的构建,如下所示:
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard1.4</TargetFrameworks>
</PropertyGroup>
<Target Name="OuterPreBuild" BeforeTargets="DispatchToInnerBuilds">
<Message Importance="high" Text="Outer before build" />
</Target>
</Project>
In this case BeforeTargets="Build" Condition="'$(IsCrossTargetingBuild)' == 'true'"
will not work because the multi-targeting Build
target depends on the "inner projects" being built already. 在这种情况下, BeforeTargets="Build" Condition="'$(IsCrossTargetingBuild)' == 'true'"
将不起作用,因为多目标Build
目标取决于已经Build
的“内部项目”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.