26

I am trying to perform integration tasting of the deployment of my application on the top of a custom container. Since my container is custom, I cannot use Maven Cargo plugin to setup the container.

My container:

  • Has to be started though a proper bat file, which is in the path of the machine where the tests are run.
  • Can be manually closed since I have a single maven module containing all my integration tests, even if one day I would like to know how to shut the process down after my tests are completed.

My problem is that I have to run my container in a different process, because it needs to keep running while my tests are performed. Furthermorely, I have an API in my tests that let me wait for the container to be ready (a sort of lookup with timeout).

I have added the following lines to my pom.xml

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.1</version>
            <executions>
                <execution>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <executable>scripts/start-agent.bat</executable>
            </configuration>
        </plugin>

This will call a script, which contains only

start call gs-agent.bat

However the mvn exec plugin gets stucked and my tests are not run. According to what is suggested in How do I run a batch file from my Java Application? , I have modified my pom.xml as the following:

 <configuration>
                <executable>cmd</executable>
                <arguments>
                    <argument>/C</argument>
                    <argument>start</argument>
                    <argument>gs-agent.bat</argument>
                </arguments>
            </configuration>

But this does not seem to solve the issue:

Maven halted

Community
  • 1
  • 1
Edmondo
  • 19,559
  • 13
  • 62
  • 115

5 Answers5

24

exec plugin is not able to do this, and I found the issue for it, too: http://jira.codehaus.org/browse/MEXEC-87 (link now dead due to codehaus rampdown, can be found from web archive)

In the jira issue linked above, there is a mention and a link of a fork for exec plugin that would have the functionality.

Other than that, I think you'll need to use an antrun-plugin for the time being.

Here's an example taken from working configuration and run with mvn verify. This needs to be in the <plugins>, not <pluginManagement> (exec could reside in pluginmanagement just fine).

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-antrun-plugin</artifactId>
    <version>1.6</version>
    <executions>
        <execution>
            <phase>pre-integration-test</phase>
            <configuration>
                <target>
                    <exec executable="cmd.exe"
                          spawn="true">
                        <arg value="/c"/>
                        <arg value="D:\myfolder\test.bat"/>
                    </exec>
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
       </execution>
   </executions>      
</plugin>

Note that spawn="true" is key here if you don't want the execution to block, like specified in the question. If you do want it to block and see the output immediately, set it to false.

eis
  • 51,991
  • 13
  • 150
  • 199
  • As I always say: for 80% of your build needs, Maven. For everything else, Maven Antrun Plugin. :) – noahlz Sep 26 '12 at 15:16
  • with the maven verify , with the following target I get empty execution – Edmondo Sep 26 '12 at 15:27
  • @Edmondo1984 added direct copy-paste from a working script, runs fine with mvn verify for me. You did have it in plugins and not pluginmanagement? – eis Sep 26 '12 at 15:52
  • Do you have a long-lasting bat script which calls other batch script? In my case it works only without the spawn=true,but the process gets called synchronously – Edmondo Sep 26 '12 at 16:10
  • yes, the spawn parameter is there to make it asyncronous. so what happens with spawn=true? – eis Sep 26 '12 at 16:12
  • @Edmondo1984 in my case, the batch file which is called contains `start "windowname" PATH_TO_REAL_EXECUTABLE` so maybe you want to try with an batch file like that, sitting in between... – eis Sep 26 '12 at 16:22
  • It works! spaw=true seems executes the command in another process? If I set spaw=false, then I can see the output of the bat file. – NangSaigon Aug 17 '16 at 12:01
  • @NangSaigon but if you set `spawn=false`, the execution blocks, and the point of this question was to get it not to block. If you're fine with that, sure, use that. – eis Aug 17 '16 at 12:49
  • Just what solved my problem was changing to the ant-run plugin. – neves Mar 19 '18 at 22:37
8

See this question: How do I run a batch file from my Java Application?

Windows Batch Files are not executable. They are scripts that are run by the cmd executable.

More Information

Exec plugin source code reveals that Apache Commons Executor is used to actually execute the command line.

There is a lot of reading you can do here, i.e. in the Apache Commons Executor documentation and their JIRA issues, but the short version is: this isn't a problem with "Maven," it's a problem with the platform-dependent nature of executing an exec() command.

I've tackled this sort of problem before, and the solution I always devise is to deconstruct the .bat script into its actual commands and launch it directly from the exec plugin, rather than calling the script.

Community
  • 1
  • 1
noahlz
  • 10,202
  • 7
  • 56
  • 75
1

The Fallowing Code works for me

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.3</version>
        <executions>
            <execution>
              <phase>generate-sources</phase>
                <configuration>
                    <tasks>
                        <exec dir="${project.basedir}" executable="Script.bat"  failonerror="true">
                        </exec>
                    </tasks>
                </configuration>
                <goals>
                    <goal>run</goal>
                </goals>
       </execution>
   </executions>      
</plugin>

Source https://maven.apache.org/guides/mini/guide-using-ant.html

Vsegar
  • 113
  • 1
  • 8
1

In my case I had trouble with npm and ng.. the underlying problem was exec-maven-plugin was trying to execute the sh scripts (which had no extensions).

Renaming

C:\Users\User\AppData\Roaming\npm\ng to C:\Users\User\AppData\Roaming\npm\ng.sh

and

C:\apps\nodejs\npm to C:\apps\nodejs\npm.sh

solved the problem.

Anand Rockzz
  • 6,072
  • 5
  • 64
  • 71
0

If you want to use exec-maven-plugin here is an example to run a batch script with arguments. E.g. to run something like:

.\test.bat arg1 arg2 arg3

add the following to pom.xml:

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.6.0</version>
    <executions>
        <execution>
            <id>exec-test</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>exec</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <executable>cmd</executable>
        <workingDirectory>./</workingDirectory>
        <arguments>
            <argument>/c</argument>
            <argument>start</argument>
            <argument>""</argument>
            <argument>exec.bat</argument>
            <argument>arg1</argument>
            <argument>arg2</argument>
            <argument>arg3</argument>
        </arguments>
    </configuration>
  </plugin>
GLampros
  • 121
  • 1
  • 8