MSBuild - wait x seconds

I have an MSBuild script that deploys a web application. It stops the current web application by copying the file "app_offline.htm" to the server. After that, it deletes the other files and then copies them to the server.

I need to add a delay between copying "app_offline.htm" and deleting.

My current script throws errors because the files are still locked when the script tries to delete them.

What is the best way to do this in MSBuild?

My stop task looks like this:

  <Target Name="Stop">
    <WriteLinesToFile File="$(DeployDirectory)\app_offline.htm" Lines="Offline for maintenance" Overwrite="true" Encoding="Unicode"/>
  </Target>

My uninstall task looks like this:

  <Target Name="Clean">
    <ItemGroup>
      <Files Include="$(DeployDirectory)\**\*" Exclude="$(DeployDirectory)\app_offline.htm" />
      <Files Include="$(LogDirectory)\*" />
    </ItemGroup>
    <Delete Files="@(Files)" />
  </Target>
+4
source share
2 answers

Several options are available:

  • MSBuild Sleep. :

    <Import Project="$(MSBuildCommunityTasksPath)\MSBuild.Community.Tasks.Targets" />
    <Sleep Milliseconds="300" />
    
  • MSBuild Extension Pack Thread, . .

  • , cmd- Exec:

    • <Exec Command="ping -n 6 127.0.0.1 > nul" />
    • <Exec Command="sleep 5" /> .
  • MSBuild inline # :

    <UsingTask TaskName="Sleep" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
      <ParameterGroup>
        <!-- Delay in milliseconds -->
        <Delay ParameterType="System.Int32" Required="true" />
      </ParameterGroup>
      <Task>
        <Code Type="Fragment" Language="cs">
          <![CDATA[
    System.Threading.Thread.Sleep(this.Delay);
    ]]>
        </Code>
      </Task>
    </UsingTask>
    
    ...
    
    <Sleep Delay="5000"/>
    
+3

Exec 5 Windows? ( DOS), .. , . -

<Exec Command="ping -n 6 127.0.0.1 > nul"/>
<Delete Files="@(Files)" />

. https://github.com/Microsoft/msbuild/issues/199: , . , - , , , , , Delete:

<Copy SourceFiles="someFile" DestinationFile="$(DeployDirectory)\someFileInUse" 
      RetryDelayMilliseconds="1000" Retries="5"/>
<Delete Files="@(Files)" />

, , , , , .

+1

Source: https://habr.com/ru/post/1663978/


All Articles