Monday, 13 August 2012

How to Include a Jar in a Maven Project?

This post is related to an answer I provided on Stackoverflow.com. It also contains a solution by Ryan Stewart.

There are situations when a .jar is required by a maven project, but it is not available in any repository. One needs a solution to make it a dependency in one's project. There are two solutions: including the .jar manually in your local repository and declare a systemPath.

Manual Inclusion in Local Repository

This requires a separate maven project based on the following pom.xml:
<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/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>net.mypackage</groupId>
    <artifactId>MavenMissingJars</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <name>Maven Missing Jars</name>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <id>dProguard-4.6</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>install-file</goal>
                        </goals>
                        <inherited>false</inherited>
                        <configuration>
                            <file>toinstall/4.6/proguard.jar</file>
                            <groupId>net.sf.proguard</groupId>
                            <artifactId>proguard</artifactId>
                            <version>4.6</version>
                            <packaging>jar</packaging>
                            <generatePom>true</generatePom>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
The configuration section is where one can indicate the location of the .jar to include and the maven coordinates to use, together with the packaging. This is a one time operation.

Use a System Path

Another solution is to to declare the following dependency in your projects' pom.xml:
<dependency>
    <groupId>net.sf.proguard</groupId><
    <artifactId>proguard</artifactId>
    <version>4.6</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/4.6/proguard.jar</systemPath>
</dependency>
The first solution can be used to include the .jar in a private or public repository and let all developers use it. The second solution is less hassle if you are working alone on your project.