For creating configuration of my application I need to run bash script. Is it possible to integrate execution of Bash scripts in Maven, maybe there are some plugins?
-
2Possible duplicate of [I want to execute shell commands from maven's pom.xml](http://stackoverflow.com/questions/3491937/i-want-to-execute-shell-commands-from-mavens-pom-xml) – Spotlight Mar 25 '16 at 01:05
7 Answers
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.

- 1,075
- 1
- 12
- 18
-
I've added an example. What else would you need to know? Do you think my answer was so unhelpful that I got a -2? – Adrien May 05 '13 at 06:24
-
3@Adrien Why? Why? Why? Why would you pollute the pom.xml with Bash scripting? Why would you make your builds not portable by calling bash (if you can avoid it)? Why if you did have to call bash on a Linux system would you not just use the maven-exec-plugin on a script in src/main/scripts/ ? I don't see your Bash maven plugin ever becoming widely used (maybe you just wrote it for a learning experience... OK then but don't recommend others use it). Maybe you can explain why it's better than the other options? – Jared May 23 '14 at 19:01
-
4@Jared 1. Because it is sometimes 30x faster to write it in Bash than in Maven/AntRun (One cause is, we can run a Bash script from the command line and we can't run a single execution ID in Maven); 2. Because running a bash script using maven-exec-plugin moves the code to another file and that's not good for the clarity of the pom.xml. **However**, it's better if people keep sticking to the best practice of writing portable pom.xml files, so it's good if they stick to maven-antrun-plugin. – Adrien Jun 24 '14 at 08:11
-
@Adrien OK, well I guess I see your points. The 30x slow bit can be mitigated by calling a script directly (antrun, exec, jacl, or whatever). I understand the clarity part of keeping things in the same file. My biggest concern is that this is non-standard and as you pointed out, not a best practice, and I don't want people to be confused on that point. As long as people understand the ramifications of what they're doing I'm all for hacking things to get them to work as needed. – Jared Jun 24 '14 at 15:33
-
@yegor256 Anyone is free to fork and deploy under their own groupId on Maven Central, so please go ahead. – Adrien Feb 23 '15 at 21:53
-
4@Adrien this is not how Maven is designed, I believe :) the author of plugin/artifact should deploy it to Maven Central and let others use it from there – yegor256 Feb 24 '15 at 00:04
-
2@yegor256 This is how open-source is designed: The person with the most motivation improves things for everyone (and gets the credit). Btw I'm not at Atlassian anymore, so I'd have to fork the plugin anyway, so it's better if you do it. – Adrien Feb 24 '15 at 08:37
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

- 1
- 1

- 12,329
- 1
- 30
- 35
-
2Agreed. If you really really need custom configuration in the POM consider a java based script like Groovy (more portable) http://docs.codehaus.org/display/GMAVEN/Executing+Groovy+Code – Mark O'Connor Dec 08 '11 at 18:57
-
2@MarkO'Connor gmaven has been discontinued (presumably since your comment). – Jared May 21 '14 at 18:32
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}"

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

- 321
- 5
- 4
If at all possible, I'd recommend using a scripting language that runs inside the JVM with dependencies that are captured either in your project or in the maven repository. That way your builds are platform independent and your dependencies are captured (i.e. you don't loose the build machine and realize your bash script was specific to that box). I showed an example in this post of using jacl. There are also good examples of using javascript and groovy inside antrun (though there may be more straightforward ways of calling them directly).
-
Nothing is platform independent. Even to run Maven, you have to install Maven. Java doesn't work unless it's installed, either, BTW. – ingyhere Jun 10 '16 at 18:43
Use the maven-antrun-plugin
artifact. This way, you can execute several executables sequentially more easily than exec-maven-plugin
. Example:
* The <exec>
tag is the important one here.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<configuration>
<tasks>
<exec executable="my1.sh">
<arg value="input1"/>
</exec>
<exec executable="my2.sh">
<arg value="input2"/>
</exec>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>

- 34,335
- 35
- 194
- 277
To experiment with commands you can use exec:exec
:
$ mvn exec:exec -q -Dexec.executable=echo -Dexec.args="your arguments"
your arguments
$ mvn exec:exec -q -Dexec.executable=echo -Dexec.args="'your arguments'"
your arguments
This demonstrates:
- passing arguments: if you need to pass several arguments: just split them with space
- if you need to pass an argument with spaces: enclose it in quotes, as you would do in bash script/terminal
-q
to shut up the mvn log

- 9,197
- 5
- 64
- 76