简体   繁体   English

在多目标项目中,如何在所有构建之前只调用一次脚本

[英]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.

相关问题 .NETStandard多目标库软件包在构建时不会还原 - .NETStandard multi-targeting library packages do not get restored when building 多目标`net461`和`netcoreapp2.0` - Multi-targeting `net461` and `netcoreapp2.0` 本地多目标化会以何种方式影响构建的输出 - In what ways does native multi-targeting affect the output of a build 我的项目是使用MSBuild 4构建的,但不是MSBuild 3.5,即使我的目标是相同版本的.NET Framework(3.5)? - My project builds with MSBuild 4 but not with MSBuild 3.5 even though I'm targeting the same version of the .NET Framework (3.5)? 如何在Visual Studio中启用多处理器构建 - How do I enable multi-processor builds in Visual Studio 多目标.NET程序集在.NET 4.5和.NET 4上使用async / await - Multi-targeting .NET assembly to use async/await on .NET 4.5 and .NET 4 如何在构建服务器上管理自动构建的依赖项? - How do I manage dependencies for automated builds on my build server? 确保在项目其余部分之前建立子目录 - Ensure subdirectory builds before rest of project 如何在我的 .NET 项目传输到内存并构建之前运行 PowerShell 脚本? - How to run a PowerShell script before my .NET project is transfered into memory and built? 为什么说我的项目针对MSIL? - Why does it say my project is targeting MSIL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM