Is there any way to run batch files during the build of a Maven project?

4

I have a .bat file that performs some prerequisites during the build of my Maven project. I want to run this .bat file automatically when I build the project I developed

Is there any way to accomplish this task with Maven?

    
asked by anonymous 18.02.2014 / 15:10

3 answers

3

Using Exec Maven Plugin .

You can run other Java programs in the same JVM or a .

See a usage example for a .bat file:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <executions>
          <execution>
            ...
            <goals>
              <goal>exec</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <executable>cmd.exe</executable>
          <!-- opcional -->
          <workingDirectory>c:\diretorio</workingDirectory>
          <arguments>
            <argument>/c</argument>
            <argument>build.bat</argument>
            <argument>parametro_bat</argument>
          </arguments>
        </configuration>
      </plugin>
    </plugins>
  </build>
   ...
</project>

Using the Maven Antrun Plugin

If you already use Ant for some other project, this plugin will be even more flexible.

See an example extracted from this OS :

<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">
                        <arg value="/c"/>
                        <arg value="C:\pasta\arquivo.bat"/>
                    </exec>
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
       </execution>
   </executions>      
</plugin>
    
18.02.2014 / 15:19
1

You can use this maven plugin, which allows you to perform virtually anything during the maven phases. As you want to run before the build (compile), the configuration would look like this:

<build>
  <plugins>
    ...
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <version>1.2.1</version>
      <executions>
        <execution>
          <id>pre-bat</id>
          <phase>process-resources</phase>  // antes do compile
          <goals>
            <goal>exec</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <executable>c:\pre_processamento.bat</executable> // caminho do seu bat
      </configuration>
    </plugin>
</build>

To find out when you should run the plugin, that is, at which stage, see reference of the maven life cycle.

    
18.02.2014 / 15:20
0
The Exec Maven Plugin provides two goals to run Java programs and programs
exec:exec  que executa programa Java ou um programa arbitrário em um processo separado
exec:java  que executa programa Java na mesma JVM

To invoke the goal use:

mvn exec:exec

Example:

To run a program written in Node JS (Test.js) located in the / Users / joao_parana directory, type the POM.xml Segment below:

<build>
    <finalName>testeNode</finalName>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <executable>node</executable>
                <!-- opcional -->
                <workingDirectory>/Users/joao_parana</workingDirectory>
                <arguments>
                    <argument>Teste.js</argument>
                </arguments>
            </configuration>
        </plugin>
    </plugins>
</build>

You will see the output of NodeJS in the console if you use the code below:

cat Teste.js 
console.log('Oi Mundo. Estou testando o Maven (goal exec:exec)')
    
18.02.2014 / 16:54