Explain Codes LogoExplain Codes Logo

How to add local jar files to a Maven project?

java
maven
local-jar
dependency-management
Nikita BarsukovbyNikita Barsukov·Aug 18, 2024
TLDR

To add a local jar to your Maven project, use the Maven Install Plugin. Execute this mvn command on your terminal:

mvn install:install-file -Dfile=<path-to-jar> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=jar

Replace <path-to-jar>, <group-id>, <artifact-id>, and <version> with the jar's accurate details. Thereafter, in your project's POM.xml, reference the jar as a dependency:

<dependency> <groupId><group-id></groupId> <artifactId><artifact-id></artifactId> <version><version></version> </dependency>

This process effectively installs the jar in your local Maven repository, making it usable in your project.

Deep Dive: Unboxing Maven Jar Tactics For Pro's

The reluctant dependency: <system> scope

Even though "<system>" scope is deprecated due to its limitations, it can be used sparingly when you have a jar that is not available in any repository. A typical syntax structure would look something like this:

<dependency> <groupId>yourjar.groupId</groupId> <artifactId>yourjar-artifactId</artifactId> <version>yourjar-version</version> <scope>system</scope> <!-- 🛠️ WARNING: May cause build errors if jar is not found in the specified path! --> <systemPath>${project.basedir}/path/to/yourjar.jar</systemPath> <!-- CORS: PathOverloadsRemotelySourced --> </dependency>

Stay updated with the Apache Maven JIRA issue MNG-6523 to handle future system-scoped dependencies more effectively.

Going local with maven repository

Creating a local-maven-repo within your project can improve your project's reliability and portability. Include this local repository in our pom.xml:

<repositories> <repository> <id>local-maven-repo</id> <!-- 💾 Oh look! I'm saving my jars on-premise. Cloud who? 🤣 --> <url>file://${project.basedir}/local-maven-repo</url> </repository> </repositories>

Now, all you need to do is upload your jars using mvn deploy:deploy-file, targeting the newly created local-maven-repo.

An ounce of initialization is worth a pound of build failures

Configure Maven to install local jars during the initialize phase by configuring maven-install-plugin in pom.xml:

<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-install-plugin</artifactId> <executions> <execution> <id>install-local-jar</id> <!-- 🚀 Initializing jar installation - shields up! --> <phase>initialize</phase> <goals> <goal>install-file</goal> </goals> <configuration> <file>${basedir}/path/to/local-jar.jar</file> <groupId>local.jar.groupId</groupId> <artifactId>local-jar-artifactId</artifactId> <version>local.jar.version</version> <!-- Nice jar you got there. Would be a shame if we forgot to pack it! 😅📦 --> <packaging>jar</packaging> <generatePom>true</generatePom> </configuration> </execution> </executions> </plugin> </plugins> </build>

This neat feature ensures that your build process won't hit a snag due to any missing dependency issues.

Autonomous projects with Maven repository manager

Consider using a Maven repository manager for greater autonomy and streamlining collaboration. It creates a central place to manage both public and proprietary artefacts efficiently.

Reflex switching: Local to Central repository

To shift from a Local Maven repository to a central one, remove the local repository configuration. The pom.xml auto-magically starts fetching dependencies from the Central repository.

Enhance: Advanced tactics and error prevention

Planning for the future

While the <system> scope is not yet obsolete, ensure long-term sustainability by integrating jars via methods not likely to be affected by future Maven updates.

Know your Maven lifecycle

Linking your local jar's installation to a specific Maven phase streamlines your build process. Choose a suitable phase such as validate or initialize.

Learning by example

For better understanding of the use of local-maven-repo, look at the real-world examples on GitHub.

Troubleshooting tips

If Maven is unable to locate the local jar, make sure the jar's path, groupId, artifactId, and version are correctly specified. Remember, the devil is in the details.