简体   繁体   English

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

[英]Run batch script as admin during Maven build

I'm trying to set up a build process during which, a Windows service has to be started and stopped. 我试图建立一个构建过程,在此过程中,必须启动和停止Windows服务。 I tried doing that by using the exec-maven-plugin : 我尝试通过使用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>

The problem that I'm running into is, that to be able to control services, you need to have adminsitrative rights. 我遇到的问题是,要能够控制服务,您需要具有管理权限。 My user is local admin so the script works if I run it in an elevated prompt (right click->'Run as Adminsitrator'). 我的用户是本地管理员,因此如果我在提升的提示符下运行脚本(右键单击->“以管理员身份运行”),该脚本将起作用。

I've tried using runas /user:administrator but it's prompting for a password. 我尝试使用runas /user:administrator但是提示输入密码。 I could run the Maven build itself as admin but I'd like to run it from environments where this might not be possible (Eclipse, Jenkins). 我可以以admin身份运行Maven构建本身,但我想在可能无法实现的环境(Eclipse,Jenkins)中运行它。

Does anyone have an idea on how to implement the described scenario? 是否有人对如何实现所描述的方案有想法?

This is the other half of my solution to elevation mkdir in batch file as admin . 这是我以admin批处理文件中海拔mkdir解决方案的另一部分。 The first half was console specific. 上半年是特定于控制台的。 This is the more general non console solution. 这是更通用的非控制台解决方案。 WshShell.Run didn't work well as a console program, hence the use of VB/VBA shell command. WshShell.Run不能作为控制台程序正常运行,因此使用VB / VBA shell命令。 The WshShell.Run has a window style (0 is hidden) and a flag to wait on the app or not. WshShell.Run具有窗口样式(0被隐藏)和一个标志,以等待是否在应用程序上等待。

Put files on the desktop. 将文件放在桌面上。 They must be ANSI. 它们必须是ANSI。

RunAsAdmin.vb 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 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>

And the command to compile. 和命令一起编译。

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

Thanks to @ACatInLove I have one working solution. 感谢@ACatInLove,我有了一个有效的解决方案。 It is based on their answer to mkdir in batch file as admin 它基于他们以管理员身份处理批处理文件中的mkdir的答案

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 我发现如何在VB6中执行进一步的代码之前等待shell进程完成此操作

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 我将生成的.exe复制到我的项目文件夹中,并将exec-maven-plugin配置更改为

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

The only restriction is that UAC pops up but I was anticipating that. 唯一的限制是UAC会弹出,但是我已经预料到了。

I will leave my question open to other suggestions for now. 我现在暂时不提其他建议。 Please give @ACatInLove some credit over at mkdir in batch file as admin 请给@ACatInLove在mkdir中以管理员身份批处理文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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