Search code examples
javamavenmaven-antrun-plugin

maven-antrun spawns process and exits on Windows 7


I observe a strange behavior in my build, and I am quite clueless on how to investigate.

During my acceptance tests, I use maven-antrun-plugin on pre-integration-test phase to start the jar that has been built previously. Then fail-safe is supposed to start to run the tests against the jar. I have some existing project for which this works, so I replicated the config :

<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>

However, I see a different behavior. When I do mvn -X clean install, on an old project for which it works, I get :

[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 ---

and the tests execute..

Now on my other project, I get this :

[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>

The launcher works, because just like for the other project, my jar gets launched in another window. But then, instead of simply executing the tests with failsafe, the build simply exits ! The log doesn't show spawned process java.lang.ProcessImpl@5b6ba5bb like in the other case.. even though it seems it does spawn the launcher.bat .

I realized I wasn't running exactly the same version of antrun plugin, so I downgraded my new project to 1.7 so that it matches. I am using Java 8 in both cases.

I am really running out of ideas to investigate... Any idea ?

Thanks


Solution

  • Thanks to Eugen's comment on my question, I investigated more on the launcher.bat, and replaced the content with a dummy echo/sleep/echo -> my build was now continuing as expected, so I knew I was on the right path.

    The content of the launcher.bat is something like this :

    cd /d %~dp0
    echo "killing app before starting it"
    call ./kill.bat
    
    start %JAVA_HOME%\bin\java ...blablabla...
    

    The kill.bat is supposed to kill the application if it's already running, if it didn't close properly from a previous build for instance.

    now, this is the content of kill.bat :

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

    well, guess what... both the maven build and the app I spawn (I replaced it above with my-app) match the where clause - so if my build was not finishing as expected, it's because it was getting killed by the spawned process, which was supposed to kill a previous running version of it, not its "parent" build !

    Note : it seems it works on different versions of Windows (like Windows 10) in which probably wmic is slightly different