3

I have a .bat file that executes some pre-requisites of the build of my Maven project, and I want to execute this .bat file automatically when the project that I developed is built.

Is there any way to perform this task with Maven?

I saw another question about something similar but it didn't solve my problem.

Filipe
  • 1,189
  • 4
  • 15
  • 30
  • possible duplicate: http://stackoverflow.com/questions/3491937/i-want-to-execute-shell-commands-from-mavens-pom-xml – Adrian Shum Feb 18 '14 at 14:03
  • personally I will recommend avoid doing that. Calling shell script is making the maven build not portable. Learn to write a little MOJO, or switch to other build tools. – Adrian Shum Feb 18 '14 at 14:04
  • 4
    You could use `maven-antrun-plugin` and execute the .bat file with ANT. – yamilmedina Feb 18 '14 at 14:11
  • What is the intention of your request? Why would you like to execute a batch file ? What will the batch file do? Apart from being not portable etc. and presumeable not working on a CI solution. – khmarbaise Feb 18 '14 at 22:25
  • the batch file call install node, npm and bower, if needed, to get dev dependencies with commands like npm install, bower install and run the Grunt task runner to minify js files – Filipe Feb 19 '14 at 00:37

2 Answers2

4

The most straight forward and flexible way to execute a batch file as part of the Maven build process is to use the maven-antrun-plugin and the exec task. As an example consider the following code for running a windows executable:

      <!-- in the <build> section -->
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <id>sign</id>
                    <phase>package</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <tasks>
                            <exec executable="${env.WIN7BASE}\bin\selfsign\inf2cat.exe">
                                <arg line="/drv:${project.build.directory}\classes /os:Vista_X86" />
                            </exec>
                            <exec executable="${signtool}">
                                <arg line="sign /v /a /t http://timestamp.verisign.com/scripts/timestamp.dll ${project.build.directory}\classes\${project.name}.cat" />
                            </exec>
                        </tasks>
                    </configuration>
                </execution>
            </executions>
        </plugin>
0

This answer might be redundant but exec can also be run as below

<plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>my-id</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target name="my-target">                                    
                                <exec executable="cmd" dir="${project.basedir}\buildtool">
                                    <arg value="/C"/>
                                    <arg value="make.bat"/>
                                </exec>
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>