14

I have a Maven project and after I install the project I need to run a script. I want to automize this process. My guess is that by adding something in the pom file I could automize this, but so far i haven't found how to run a script after installation. I only found how to run a script before the maven project has finised installing.

So, how can I run a script after a Maven project as finished installing?

Dorin
  • 2,167
  • 4
  • 20
  • 32
  • http://stackoverflow.com/a/2008258/4796021 – David Pérez Cabrera Aug 22 '16 at 20:14
  • This version runs the script before maven install. I use maven3. – Dorin Aug 22 '16 at 20:28
  • 2
    This kind of goes against the whole Maven model. If the script you need to run comes after Maven install, it seems likely that it is out of the scope of Maven's responsibility. You are better off writing a wrapper script that executes both maven and your post-install script. – rmlan Aug 22 '16 at 20:52

4 Answers4

6

Use the http://www.mojohaus.org/exec-maven-plugin/ exec-maven-plugin, in conjunction with an "executions" configuration block that specifies the installation phase. Make sure it is after your maven-install-plugin as plugins are ran in order (within the same phase)

(in build/plugins)  
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <version>2.5.2</version>
  </plugin>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.5.0</version>
    <executions>
      <execution>
        <phase>install</phase>
        <goals>
           <goal>exec</goal>
        </goals>
        <configuration>
          <executable>do-something.sh</executable>
          <workingDirectory>/some/dir</workingDirectory>
          <arguments>
             <argument>--debug</argument>
             <argument>with_great_effect</argument>
          </arguments>
        </configuration>
      </execution>
    </executions>
  </plugin>
wittich
  • 2,079
  • 2
  • 27
  • 50
Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • Don't include ant configurations just to do stuff there's maven plugins for. That makes your configuration maintenance include support for both Maven and Ant! – Edwin Buck Aug 22 '16 at 21:14
3

For a purely maven-driven approach, the answer you're looking for is the exec goal of exec-maven-plugin, and this answer applies: https://stackoverflow.com/a/2008258/3403663

EDIT: OP indicates the above doesn't work for him.

Alternative approach: I just tried the following in my own project, and it executes ls at the very end of the install phase, after artifacts have been deployed.

mvn clean install exec:exec -Dexec.executable="/bin/ls" -Dexec.args="/etc"

Otherwise, you could always just wrap the whole thing in a script:

#!/bin/bash

set -o errexit

mvn clean install
<your other commands here>
Community
  • 1
  • 1
Larry McQueary
  • 416
  • 3
  • 9
  • The solution from the link doesn't work, the script is run before maven install., Thanks for your response. – Dorin Aug 22 '16 at 20:32
0

Why can't you do something like this? This will go after the normal maven install phase.

EDIT: If you add the maven-install-plugin before it, maven will run each in the order they are in the pom.

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <version>2.5.2</version>
  </plugin>
  <plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <executions>
      <execution>
        <phase>install</phase>
        <configuration>
          <tasks>
            <exec
              dir="${project.basedir}"
              executable="${project.basedir}/src/main/sh/do-something.sh"
              failonerror="true">
              <arg line="arg1 arg2 arg3 arg4" />
            </exec>
          </tasks>
        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

Source: maven-antrun-plugin

Talon
  • 811
  • 7
  • 22
-3

You can chain the commands

$ mvn clean install && myscript.sh || echo "error."
chenchuk
  • 5,324
  • 4
  • 34
  • 41