maven-antrun 生成进程并在 Windows 7 上退出

maven-antrun spawns process and exits on Windows 7

我在我的构建中观察到一个奇怪的行为,我对如何调查一无所知。

在验收测试期间,我在预集成测试阶段使用 maven-ant运行-plugin 来启动之前构建的 jar。然后故障安全应该开始 运行 针对 jar 的测试。我有一些可用的现有项目,所以我复制了配置:

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <id>start-application</id>
            <phase>pre-integration-test</phase>
            <configuration>
                <target>
                    <exec executable="cmd" dir="../" spawn="true" os="Windows 7">
                        <arg value="/c"/>
                        <arg value="launcher.bat"/>
                        <arg value="${project.version}"/>
                    </exec>
                    <exec executable="cmd" dir="../" spawn="true" os="Windows Server 2012 R2">
                        <arg value="/c"/>
                        <arg value="launcher.bat"/>
                        <arg value="${project.version}"/>
                    </exec>
                    <exec executable="./launcher.sh" dir="../" os="Linux">
                        <arg value="${project.version}"/>
                    </exec>
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
</plugin>

但是,我看到了不同的行为。当我在一个旧项目上执行 mvn -X clean install 时,我得到:

[INFO] Executing tasks
Build sequence for target(s) `main' is [main]
Complete build sequence is [main, ]

main:
     [exec] Current OS is Windows 7
     [exec] Executing 'cmd' with arguments:
     [exec] '/c'
     [exec] 'launcher.bat'
     [exec] '1.0.0-SNAPSHOT'
     [exec]
     [exec] The ' characters around the executable and arguments are
     [exec] not part of the command.
Execute:Java13CommandLauncher: Executing 'cmd' with arguments:
'/c'
'launcher.bat'
'1.0.0-SNAPSHOT'

The ' characters around the executable and arguments are
not part of the command.
spawned process java.lang.ProcessImpl@5b6ba5bb
     [exec] Current OS is Windows 7
     [exec] This OS, Windows 7 was not found in the specified list of valid OSes: Windows Server 2012 R2
     [exec] Current OS is Windows 7
     [exec] This OS, Windows 7 was not found in the specified list of valid OSes: Linux
[INFO] Executed tasks
[INFO]
[INFO] --- maven-failsafe-plugin:2.18.1:integration-test (default) @ acceptance ---

测试执行..

现在在我的另一个项目中,我得到了这个:

[INFO] Executing tasks
Build sequence for target(s) `main' is [main]
Complete build sequence is [main, ]

main:
     [exec] Current OS is Windows 7
     [exec] Executing 'cmd' with arguments:
     [exec] '/c'
     [exec] 'launcher.bat'
     [exec] '1.0.0-SNAPSHOT'
     [exec]
     [exec] The ' characters around the executable and arguments are
     [exec] not part of the command.
Execute:Java13CommandLauncher: Executing 'cmd' with arguments:
'/c'
'launcher.bat'
'1.0.0-SNAPSHOT'

The ' characters around the executable and arguments are
not part of the command.

D:\vincent\GIT\my_project\acceptance>

启动器有效,因为就像其他项目一样,我的 jar 在另一个 window 中启动。但是随后,构建简单地退出,而不是简单地使用故障安全执行测试! 日志没有像另一种情况那样显示 spawned process java.lang.ProcessImpl@5b6ba5bb.. 尽管它似乎确实产生了 launcher.bat .

我意识到我没有 运行使用完全相同版本的 ant运行 插件,所以我将我的新项目降级到 1.7 以便它匹配。我在这两种情况下都使用 Java 8。

我真的运行没有想法去调查...有什么想法吗?

谢谢

感谢 Eugen 对我的问题的评论,我对 launcher.bat 进行了更多调查,并用虚拟 echo/sleep/echo 替换了内容 -> 我的构建现在按预期继续进行,所以我知道我走在正确的道路上。

launcher.bat 的内容是这样的:

cd /d %~dp0
echo "killing app before starting it"
call ./kill.bat

start %JAVA_HOME%\bin\java ...blablabla...

如果应用程序已经 运行,kill.bat 应该终止应用程序,例如,如果它没有从以前的构建中正确关闭。

现在,这是kill.bat的内容:

wmic process where (commandline like "%%my-app%%" and not name="wmic.exe") delete

好吧,猜猜看...maven 构建和我生成的应用程序(我在上面用 my-app 替换了它)都匹配 where 子句 - 所以如果我的构建没有按预期完成,这是因为它被派生进程杀死,该进程应该杀死它的先前 运行 版本,而不是它的 "parent" 版本!

注意:它似乎适用于不同版本的 Windows(如 Windows 10),其中 wmic 可能略有不同