Is it possible to run a Bash script from Maven?

Solved. The problem is, executable is working in a different way for bash. This code is working. Write it in pom.xml

    <plugin>
        <artifactId>exec-maven-plugin</artifactId>
    <version>1.6.0</version>
        <groupId>org.codehaus.mojo</groupId>
        <executions>
            <execution><!-- Run our version calculation script -->
                <id>Renaming build artifacts</id>
                <phase>package</phase>
                <goals>
                    <goal>exec</goal>
                </goals>
                <configuration>
                    <executable>bash</executable>
            <commandlineArgs>handleResultJars.sh</commandlineArgs>
                </configuration>
            </execution>
        </executions>
    </plugin>

Would look more like:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <id>generateSources</id>
            <phase>generate-sources</phase>
            <configuration>
                <tasks>
                    <exec executable="/bin/bash">
                        <arg value="myFirst.sh" />
                        <arg value="inputOne" />
                    </exec>
                    <exec executable="/bin/bash">
                        <arg value="src/mySecond.sh" />
                        <arg value="inputTwo" />
                    </exec>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

With myFirst.sh:

echo "call to myFirst.sh, message ${1}"

You can do this, see answer:

I want to execute shell commands from maven's pom.xml

But it is not advisable, as this produces not so portable builds. Why do you need this in first place? Using this plugin usually indicates some weird necessity in project build


Could the Bash Maven Plugin help you? (Disclaimer: I initiated it, so please send me feedback)

<build>
    <plugins>
        <plugin>
            <!-- Run with:
                mvn bash:run
                mvn install
            -->
            <groupId>com.atlassian.maven.plugins</groupId>
            <artifactId>bash-maven-plugin</artifactId>
            <version>1.0-SNAPSHOT</version>
            <executions>
                <execution>
                    <id>test</id>
                    <phase>integration-test</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <script>
                    # Here you can execute shell commands
                    echo "Tomcat will start"
                    /opt/apache-tomcat/bin/startup.sh
                </script>
            </configuration>
        </plugin>
    </plugins>
</build>

You will need to install this maven plugin in your own Maven repo.

Like Konstantin: When you execute a shell script, you're not portable anymore.

Tags:

Bash

Maven