How to handle external jars with Maven

...the elegant way

If you've been struggling to add some jars to your maven project and maybe you've finished by adding them as a system scope dependency - you are in the right place!

After I spent some time on Stackoverflow, I decided to gather useful information and write a brief advice on how to deal with jar dependencies.

All you need to do is add and configure maven-install-plugin in your main pom.xml like this:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.3.1</version>
                <executions>
                    <execution>
                        <id>my-fancy-id</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>install-file</goal>
                        </goals>
                        <configuration>
                            <file>${session.executionRootDirectory}/lib/some-crap.jar</file>
                            <groupId>pl.edu.agh</groupId>
                            <artifactId>some-crap</artifactId>
                            <version>0.0.1</version>
                            <packaging>jar</packaging>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

Then, Maven will install the configured jar in a prepare-package phase. The ${session.executionRootDirectory} variable points to the directory from which the maven command was executed.

Of course it would be better to have your own repository like Artifactory. But there isn't always such a possibility.