Running perl from maven
Next post in English (probably all short technical post will be in English from now longer and not technical will be still in Polish).
Ad rem. Sometimes one of this strange guys who write in perl/ruby/phyton create some super-fucking-extra script that solve all problems in project but you must run it always when you compile project. Of course you must do it in the right time (after compile and before package) and right way.
Today I need to run perl script that compress css file (remove comments, additional spaces, tabs and extra lines, I white GWT project so short css is a nice feature) and I need run it after compile and before package. I use maven to compile project.
The simplest way is make this process in tree steps. First step is call compile. Second is run script, and third is run package. When We got CI server like Hudson we can create job and forget about it. But when we developing application make two additional steps is not the best idea.
Solution for this problem is use exec-maven-plugin. Listing 1 show simple pom.xml that call Linux tree program with application root directory as argument:
Listing 1. Call tree ./ in the compile phase.
<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/maven-v4_0_0.xsd"><modelversion>4.0.0</modelversion><groupid>pl.koziolekweb.exec.test</groupid><artifactid>ExecTest</artifactid><packaging>jar</packaging><version>1.0-SNAPSHOT</version><name>ExecTest</name><url>http://maven.apache.org</url><build><plugins><plugin><groupid>org.codehaus.mojo</groupid><artifactid>exec-maven-plugin</artifactid><version>1.1</version><executions><execution><id>exec-tree</id><phase>compile</phase><goals><goal>exec</goal></goals></execution></executions><configuration><executable>tree</executable><workingdirectory>./</workingdirectory><arguments><argument>./</argument></arguments></configuration></plugin></plugins></build><dependencies><dependency><groupid>junit</groupid><artifactid>junit</artifactid><version>3.8.1</version><scope>test</scope></dependency></dependencies></project>
The result of execution:
Listing 2. Result of mvn clean compile.
koziolek@koziolek-desktop:~/workspace/ExecTest$ mvn clean compile
Warning: JAVA_HOME environment variable is not set.
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building ExecTest
[INFO] task-segment: [clean, compile]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean]
[INFO] Deleting directory /home/koziolek/workspace/ExecTest/target
[INFO] [resources:resources]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/koziolek/workspace/ExecTest/src/main/resources
[INFO] [compiler:compile]
[INFO] Compiling 1 source file to /home/koziolek/workspace/ExecTest/target/classes
[INFO] [exec:exec {execution: exec-tree}]
[INFO] ./
[INFO] |-- pom.xml
[INFO] |-- src
[INFO] | |-- main
[INFO] | | `-- java
[INFO] | | `-- pl
[INFO] | | `-- koziolekweb
[INFO] | | `-- exec
[INFO] | | `-- test
[INFO] | | `-- App.java
[INFO] | `-- test
[INFO] | `-- java
[INFO] | `-- pl
[INFO] | `-- koziolekweb
[INFO] | `-- exec
[INFO] | `-- test
[INFO] | `-- AppTest.java
[INFO] `-- target
[INFO] `-- classes
[INFO] `-- pl
[INFO] `-- koziolekweb
[INFO] `-- exec
[INFO] `-- test
[INFO] `-- App.class
[INFO]
[INFO] 19 directories, 4 files
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5 seconds
[INFO] Finished at: Tue Jul 07 23:21:17 CEST 2009
[INFO] Final Memory: 11M/118M
[INFO] ------------------------------------------------------------------------
That’s all folks 😉