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

标签 windows maven batch-file exec-maven-plugin

我正在尝试设置一个构建过程,在此过程中必须启动和停止 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/

相关文章:

windows - 将数字写入文本文件 - 批处理文件

batch-file - 如何在win 8中使用批处理代码更改壁纸?

shell - 如何在shell中创建日期生成器?

python - Tkinter,Windows : How to view window in windows task bar which has no title bar?

C heapsort一运行就崩溃,windows报错

c# - Windows Server 2003 到 2008 sslstream 无通用算法

java - exec-maven-plugin 未检测到 src/main/java 中的类

java - eclipse maven项目如何配置热部署

java - Maven 和开放 Km 构建失败

c# - 使用 C#,如何从批处理脚本创建的新 cmd.exe 运行命令?