简体   繁体   中英

BeforeBuild target never gets executed in some projects

This is .csproj instructions I'd like to be executed:

  <PropertyGroup>
    <DisableFastUpToDateCheck>true</DisableFastUpToDateCheck>
  </PropertyGroup>
  <Target Name="BeforeBuild">
    <Exec Command="powershell.exe -executionpolicy bypass -command &quot;&amp; 'C:\Users\user\Desktop\Build Test\testScript.ps1' &quot;" />
  </Target>

I've tested it in multiple project. In all but one of them it works -> it starts the powershell script as expected.

This is the slightly truncated .csproj for the project where it fails:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <Authors>me</Authors>
    <Company>Company</Company>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
    <PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
    <Copyright></Copyright>
    <PackageLicenseUrl></PackageLicenseUrl>
    <PackageProjectUrl></PackageProjectUrl>
    <PackageIconUrl />
    <RepositoryUrl></RepositoryUrl>
    <PackageTags></PackageTags>
    <PackageReleaseNotes>Release message</PackageReleaseNotes>
    <Title>MyProjectLib</Title>
    <Summary>sTUFF</Summary>
    <Description>My Project library</Description>
    <Version>4.2.5</Version>
    <AssemblyVersion>1.0.0</AssemblyVersion>
    <FileVersion>1.0.0</FileVersion>
    <RootNamespace>MyProjectLib</RootNamespace>
    <PackageId>MyProjectLib</PackageId>
    <Product>MyProjectLib</Product>
    <IsPackable>false</IsPackable>    
  </PropertyGroup>

  <ItemGroup Label="dotnet pack instructions">
    <Content Include="build\*.targets">
      <Pack>true</Pack>
      <PackagePath>build\</PackagePath>
    </Content>
  </ItemGroup>

  <Target Name="CollectRuntimeOutputs" BeforeTargets="_GetPackageFiles">
    <!-- Collect these items inside a target that runs after build but before packaging. -->
    <ItemGroup>
      <Content Include="$(OutputPath)\*.pdb;">
        <Pack>true</Pack>
        <PackagePath>build\</PackagePath>
      </Content>
    </ItemGroup>
  </Target>


  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
    <DocumentationFile>bin\Debug\netstandard2.0\MyProject.xml</DocumentationFile>
    <DefineConstants>TRACE;VERBOSE_WARNINGS</DefineConstants>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
    <DocumentationFile>bin\Release\netstandard2.0\MyProject.xml</DocumentationFile>
    <DefineConstants>TRACE;FINAL</DefineConstants>
    <DebugType>none</DebugType>
  </PropertyGroup>

  <ItemGroup>
    <Compile Remove="Contrib\**" />
    <Compile Remove="Folder2\**" />
    <EmbeddedResource Remove="Contrib\**" />
    <EmbeddedResource Remove="Folder2\**" />
    <None Remove="Contrib\**" />
    <None Remove="Folder2\**" />
  </ItemGroup>
  
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
    <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" />
    <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
    <PackageReference Include="Microsoft.Win32.Registry" Version="4.5.0" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
    <PackageReference Include="Portable.BouncyCastle" Version="1.8.5.2" />
    <PackageReference Include="System.Management" Version="5.0.0-preview.1.20120.5" />
    <PackageReference Include="System.Memory" Version="4.5.1" />
  </ItemGroup>
  
  <ItemGroup>
    <ProjectReference Include="ExternalProject.csproj" />
  </ItemGroup>
  
  <ItemGroup>
    <Reference Include="mscorlib" />
  </ItemGroup>

  <!--START OF IMPORTANT STUFF-->
  <PropertyGroup>
    <DisableFastUpToDateCheck>true</DisableFastUpToDateCheck>
  </PropertyGroup>

  <Target Name="BeforeBuild">
    <Exec Command="powershell.exe -executionpolicy bypass -command &quot;&amp; 'C:\Users\user\Desktop\Build Test\testScript.ps1' &quot;" />
  </Target>

  <!--END OF IMPORTANT STUFF-->
  
</Project>

I've spent the last day figuring out why it fails to execute powershell script.. What could be the reason for the failure?

If you are using the Sdk import, it will add content before and after your project file and overwriting the BeforeBuild target (this is why in non-sdk projects this target should be at the very bottom below any <Import> s.

I suggest creating a target without a conflicting name and hooking it into the build:

<Target Name="RunCustomScript" BeforeTargets="BeforeBuild">

This is also what the Project Property Pages dialog in VS should generate when you try to edit build scripts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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