Search code examples
batch-fileeclipse-pluginmaven-antrun-plugin

Run .bat file - Maven/Eclipse


I want to run a batch file using maven.

My pom.xml looks like this:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>test--clients</artifactId>
    <packaging>war</packaging>
    <name>test--clients</name>


    <parent>
        <artifactId>test--app</artifactId>
        <groupId>de.timetoact.test-</groupId>
        <version>10.0.0</version>
    </parent>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <!-- <properties> -->
    <!-- <maven.compile.source>1.6</maven.compile.source> -->
    <!-- <maven.compile.target>1.6</maven.compile.target> -->
    <!-- <failOnMissingWebXml>false</failOnMissingWebXml> -->
    <!-- </properties> -->

    <dependencies>

        <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
         <version>1.8</version>
        </dependency>

        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.cloudfoundry</groupId>
            <artifactId>cloudfoundry-client-lib</artifactId>
            <version>1.0.2</version>
        </dependency>
<!--        <dependency> -->
<!--            <groupId>org.springframework.security.oauth</groupId> -->
<!--            <artifactId>spring-security-oauth2</artifactId> -->
<!--            <version>1.0.0.RELEASE</version> -->
<!--            <exclusions> -->
<!--                <exclusion> -->
<!--                    <groupId>org.springframework</groupId> -->
<!--                    <artifactId>spring-jdbc</artifactId> -->
<!--                </exclusion> -->
<!--            </exclusions> -->
<!--        </dependency> -->
        <dependency>
            <groupId>com.ibm.sbt</groupId>
            <artifactId>com.ibm.commons</artifactId>
            <version>9.0.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>

        <dependency>
            <groupId>de.timetoact.test-</groupId>
            <artifactId>test--util</artifactId>
            <version>10.0.0</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.9</version>
        </dependency>
    </dependencies>


    <profiles>
        <profile>
            <id>test--clients-deploy-eu</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.cloudfoundry</groupId>
                        <artifactId>cf-maven-plugin</artifactId>
                        <version>1.1.2</version>
                        <configuration>
                            <server>bluemix</server>
                            <target>https://api.eu-gb.bluemix.net</target>
                            <org>test-</org>
                            <space>test--clients-eu</space>
                            <appname>test--clients</appname>
                            <urls>
                                <url>test--clients.eu-gb.mybluemix.net</url>
                                <url>test--client.eu-gb.mybluemix.net</url>
                            </urls>

                            <memory>192</memory>
                            <services>
                                <service>
                                    <name>test-</name>
                                    <label>postgresql</label>
                                    <plan>100</plan>
                                </service>
                            </services>
                        </configuration>
                        <executions>
                            <execution>
                                <phase>package</phase>
                                <goals>
                                    <goal>push</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>

        </profile>
        <profile>
            <id>test--dev-deploy-eu</id>
            <build>
                <plugins>

    <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.8</version>
        <executions>
          <execution>
            <phase>generate-sources</phase>
            <configuration>
              <tasks>
                <exec
                  dir="${project.basedir}"
                  executable="${project.basedir}/test.bat"
                  failonerror="true">
                  <arg line="/c" />
                </exec>
              </tasks>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>



                    <plugin>
                        <groupId>org.cloudfoundry</groupId>
                        <artifactId>cf-maven-plugin</artifactId>
                        <version>1.1.2</version>
                        <configuration>
                            <server>bluemix</server>
                            <target>https://api.eu-gb.bluemix.net</target>
                            <org>test-</org>
                            <space>test--dev-eu</space>
                            <appname>test--clients</appname>
                            <urls>
                                <url>test--clients-dev.eu-gb.mybluemix.net</url>
                                <url>test--client-dev.eu-gb.mybluemix.net</url>
                            </urls>

                            <memory>192</memory>
                            <services>
                                <service>
                                    <name>test-</name>
                                    <label>postgresql</label>
                                    <plan>100</plan>
                                </service>
                            </services>
                        </configuration>
                        <executions>
                            <execution>
                                <phase>package</phase>
                                <goals>
                                    <goal>push</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>

        </profile>
        <profile>
            <id>test--clients-deploy-us</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.cloudfoundry</groupId>
                        <artifactId>cf-maven-plugin</artifactId>
                        <version>1.1.2</version>
                        <configuration>
                            <server>bluemix</server>
                            <target>https://api.ng.bluemix.net</target>
                            <org>test-</org>
                            <space>test--clients-us</space>
                            <appname>test--clients</appname>
                            <urls>
                                <url>test--clients.mybluemix.net</url>
                                <url>test--client.mybluemix.net</url>
                            </urls>
                            <memory>192</memory>
                            <services>
                                <service>
                                    <name>test-</name>
                                    <label>postgresql</label>
                                    <plan>100</plan>
                                </service>
                            </services>
                        </configuration>
                        <executions>
                            <execution>
                                <phase>package</phase>
                                <goals>
                                    <goal>push</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>

        </profile>
    </profiles>

</project>

What I get as output is:

[INFO] Executing tasks
[INFO] Executed tasks

But the .bat file has not been called.

Tried so much answers here at Stackoverflow I am already despairing.

What is the problem here?


Solution

  • You are attempting to use the maven-antrun-plugin to call a batch script. If you still want to do that check out this tutorial run cmd with maven ant plugin alternatively you can use the maven-exec-plugin to run a batch script:

                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.2.1</version>
                    <executions>
                        <execution>
                            <phase>generate-sources</phase>
                            <configuration>
                                <executable>cmd</executable>
                                <workingDirectory></workingDirectory>
                                <arguments>
                                    <argument>/C</argument>
                                    <argument>"${project.basedir}/test.bat"</argument>
                                </arguments>
                            </configuration>
                            <goals>
                                <goal>exec</goal>
                            </goals>
                        </execution>                            
                    </executions>
                 </plugin>