问题描述
我正在尝试建立一个构建过程,在此过程中,必须启动和停止Windows服务.我尝试使用exec-maven-plugin
:
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
:
<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').
我尝试使用runas /user:administrator
,但提示输入密码.我可以以admin身份运行Maven构建本身,但我想在可能无法实现的环境(Eclipse,Jenkins)中运行它.
I've tried using runas /user:administrator
but it's prompting for a password. 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).
有人对如何实现所描述的方案有想法吗?
Does anyone have an idea on how to implement the described scenario?
推荐答案
这是我解决高程问题的另一半.上半年是特定于控制台的.这是更通用的非控制台解决方案. WshShell.Run
不能很好地用作控制台程序,因此使用VB/VBA shell
命令. WshShell.Run
具有窗口样式(0处于隐藏状态)和一个标志,等待或不等待应用程序.
This is the other half of my solution to elevation mkdir in batch file as admin. 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. The WshShell.Run
has a window style (0 is hidden) and a flag to wait on the app or not.
将文件放在桌面上.它们必须是ANSI.
Put files on the desktop. They must be 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
这篇关于在Maven构建过程中以管理员身份运行批处理脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!