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

Asked

Viewed 589 times

4

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

Is there any way to accomplish this task with Maven?

3 answers

3


Using the Exec Maven Plugin.

With it you can perform other Java programs in the same JVM or a any program in a separate process.

See a example of use to an archive .bat:

<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 of this OS response:

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

1

You can use that Maven plugin, which allows you to perform virtually anything during the stages of Maven. As you want to run before 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 know right when you should run the plugin, ie at what stage, see the reference of the Maven life cycle.

0

The Exec Maven Plugin provides two Goals to run system programs and Java 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 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 exit 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)')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.