2011/03/29

Mavenizing ECJ locally using maven and ANT

Sometimes you may need a useful library in your project which is not mavenized anywhere else, and you don't want to create a customized shell script to install the library in the local maven's repository for every operating system.
Of course you just can drop the jar into the project and distribute the whole project using some control version system.

This time I just preferred to have fun with maven :-)


Let maven do the job and skip any complex installation procedure needed (dependencies, tools, etc).

In this POM I used maven-antrun-plugin (ANT) to install ECJ into the local maven's repository. JDK and Maven are the only install requirements.

<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>org.university.project</groupId>
    <artifactId>X</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>ProjectName</name>
 

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.6</version>
                <executions>
                    <execution>
                        <id>wget</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target name="prepare">
                                <condition property="mavenized">
                                    <resourceexists>
                                        <file dir="${user.home}/.m2/repository/ecj"/>
                                    </resourceexists>
                                </condition>
                            </target>
                            <target name="install" unless="mavenized">
                                <property name="tmp.dir" value="${user.home}/tmp"/>
                                <property name="target.tgz" value="${tmp.dir}/ecj.tar.gz"/>
                                <property name="target.jar" value="${tmp.dir}/ecj.jar"/>
                                <mkdir dir="${user.home}/tmp"/>
                                <get src="http://www.cs.gmu.edu/~eclab/projects/ecj/ecj.tar.gz" dest="${target.tgz}" verbose="false" skipexisting="true"/>
                                <untar src="${target.tgz}" dest="${tmp.dir}" compression="gzip"/>
                                <jar destfile="${target.jar}" basedir="${tmp.dir}/ecj"/>
                                <macrodef name="mvn">
                                    <attribute name="failonerror" default="true"/>
                                    <attribute name="args" default=""/>
                                    <sequential>
                                        <exec failonerror="@{failonerror}" executable="mvn.bat" osfamily="Windows">
                                            <arg line="@{args}"/>
                                        </exec>
                                        <exec failonerror="@{failonerror}" executable="mvn" osfamily="unix">
                                            <arg line="@{args} "/>
                                        </exec>
                                    </sequential>
                                </macrodef>
                                <mvn args="install:install-file -Dfile=${target.jar} -DgroupId=ecj -DartifactId=ecj -Dversion=20 -Dpackaging=jar -DgeneratePom=true" />
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>



This file must not be your main pom.xml, it is just a auxiliar POM you may need the first time when you know you main POM would fail because it didn't find the dependency (ecj.jar) in the local repository.

The life cycle phase chosen is "validate" because we just need to install the external library first.


This is how you may run it:

mvn -f pom.install.xml validate

If you know a better way to do the same using maven, please tell me!

No comments:

Post a Comment