在Maven构建期间以管理员身份运行批处理脚本

问题描述 投票:1回答:2

我正在尝试建立一个构建过程,在此过程中,必须启动和停止Windows服务。我尝试使用exec-maven-plugin这样做:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.6.0</version>
    <executions>
        <execution>
            <id>startServer</id>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <executable>${project.basedir}/bin/startService.cmd</executable>
                <workingDirectory>${project.basedir}/bin</workingDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

我遇到的问题是,为了能够控制服务,您需要具有管理权限。我的用户是本地管理员,因此如果我在提升的提示符下运行它(右键单击 - >'以管理员身份运行'),脚本将起作用。

我尝试过使用runas /user:administrator,但它提示输入密码。我可以运行Maven构建本身作为管理员,但我想从可能无法实现的环境(Eclipse,Jenkins)运行它。

有没有人知道如何实现所描述的场景?

windows maven batch-file exec-maven-plugin
2个回答
2
投票

这是我提升mkdir in batch file as admin解决方案的另一半。上半部分是控制台特定的。这是更通用的非控制台解决方案。 WshShell.Run作为控制台程序运行不佳,因此使用VB / VBA shell命令。 WshShell.Run有一个窗口样式(隐藏0)和一个等待应用程序的标志。

将文件放在桌面上。它们必须是ANSI。

runasadmin.vb

imports System.Runtime.InteropServices 
Public Module MyApplication  

Public Sub Main ()
        Dim wshshell as object
        WshShell = CreateObject("WScript.Shell")
            '8 is non active window, true means wait for exit
        WshShell.Run("""C:\Windows\Notepad.exe""",8, true)
    End Sub 
End Module 

RunAsAdmin.manifest

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
    version="1.0.0.0"
    processorArchitecture="*"
    name="Color Management"
    type="win32"
/>
<description>Serenity's Editor</description>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2"> 
<security> 
    <requestedPrivileges> 
        <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/> 
    </requestedPrivileges> 
</security> 
</trustInfo> 

</assembly>

和编译的命令。

C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc "%userprofile%\Desktop\RunAsAdmin.vb" /win32manifest:"%userprofile%\Desktop\RunAsAdmin.manifest" /out:"%userprofile%\Desktop\RunAsAdmin.exe" /target:winexe

0
投票

感谢@ACatInLove我有一个有效的解决方案。这是基于他们对mkdir in batch file as admin的回答

问题是它正在启动一个新的shell而不是等待批处理脚本完成。我必须修改脚本才能这样做。我发现在How to wait for a shell process to finish before executing further code in VB6有一个这样做

这是脚本:

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

我将生成的.exe复制到我的项目文件夹中,并将exec-maven-plugin配置更改为

<executable>..\RunAsAdminConsole.exe startService.cmd</executable>

唯一的限制是UAC弹出,但我期待着。

我现在将其问题留给其他建议。请在mkdir in batch file as admin给@ACatInLove一些信用

© www.soinside.com 2019 - 2024. All rights reserved.