我正在尝试设置一个构建过程,在此过程中必须启动和停止 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)。
有人知道如何实现所描述的场景吗?
最佳答案
这是我的海拔解决方案的另一半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
关于windows - 在 Maven 构建期间以管理员身份运行批处理脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47902221/