Run batch script as admin during Maven build

后端 未结 2 597
野趣味
野趣味 2020-12-07 04:27

I\'m trying to set up a build process during which, a Windows service has to be started and stopped. I tried doing that by using the exec-maven-plugin:

2条回答
  •  隐瞒了意图╮
    2020-12-07 04:59

    Thanks to @ACatInLove I have one working solution. It is based on their answer to mkdir in batch file as admin

    The problem was that it was starting a new shell and not waiting for the batch script to finish. I had to modify the script to do so. I found a was to do that at How to wait for a shell process to finish before executing further code in VB6

    This is the script:

    imports System.Runtime.InteropServices 
    Public Module MyApplication  
        Public Sub Main ()
            Dim hProcess As Long
            Dim taskId As Long
            Dim wshshell as object
            WshShell = CreateObject("WScript.Shell")
            taskId = Shell("cmd /c " & Command())
            hProcess = OpenProcess(&H100000, True, taskId)
            Call WaitForSingleObject(hProcess, 10000)
            CloseHandle(hProcess)
        End Sub
        Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
        Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
        Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long 
    End Module
    

    I copied the resulting .exe into my project folder and changed the exec-maven-plugin configuration to

    ..\RunAsAdminConsole.exe startService.cmd
    

    The only restriction is that UAC pops up but I was anticipating that.

    I will leave my question open to other suggestions for now. Please give @ACatInLove some credit over at mkdir in batch file as admin

提交回复
热议问题