How to Add JAR Files to a Maven Project?

Ways to add JAR file to a Maven project

Were you trying to link custom local JAR files to a Maven Project — all to no avail? We’ve got your back!

Maven is a popular build tool for Java projects, and it can be useful to include external JAR files in your Maven project.

JAR files can be a pain to integrate; linking them up with a maven project can be a bigger hassle since there’s no universal know-how 🧐. While you could leave the fate of your files in the hands of an automated plugin, the loss of versatility in return is no joke. 

To solve this, you could theoretically ask an expert to do it for you, but there’s no learning in that. 

Adding Local JAR Files to a Maven Project can be as simple as a one-step process or take you hours of head-scratching sessions. It all depends on your approach. 😃

Add local JAR files to a Maven project

 

With all that said, to add Local JAR files to a Maven project, you can either try to write a dependency code or use a plugin that will automatically add the local JAR file to your Maven Project. Alternatively, making direct edits to the POM file is possible

Therefore, we recommend you use Maven directly instead of a regular IDE since it adds an extra layer of complexity that’s not needed otherwise. With that said, we’d essentially go through the following methods:

  • Manual Installation: Installing JAR into a local Maven repository with the help of commands. 
  • System Scope: Converting the file to a system scope to add the dependency directly. 
  • Creating A Local Maven Repository: Using a dummy repository instead of the main one. 

This article will show how the mentioned methods add local JAR files to a Maven project. So, let’s get started! 😁

 

 

Ways to Add JAR Files to a Maven Project

Method 1: Manual Installation

1. Understanding The Apache Maven Install Plugin

This plugin allows you to install any file in your local repository. Its full name is as follows:

org.apache.maven.plugins:maven-install-plugin:3.0.1:install-file

 

You would need to add the following parameters to call this plugin for file installation:

  • <File> : This parameter refers to the File Type and passes the file that must be installed in the local repository. 

Aside from the required parameter, you can also pass the following to mold the process according to your requirements:

  • <artifactId> : This parameter refers to the String Type and passes the ArtifactId of the Artifact you want to install. It’s generally retrieved or extracted from the POM file. 
  • <classifiers> : Also takes in a String Type containing the artifact’s classifier type needed to be installed. 
  • <generatePom> : This parameter takes a Boolean value to generate a minimal POM for the artifact. Generally used when there’s no POM file in the repository. 
  • <groupId> : A parameter that takes a String with the GroupId of the artifact that needs to be installed. Usually retrieved from the POM file.
  • <javadoc> : Takes a File Type as a parameter and acts as bundled API docs for the artifact.  
  • <localRepositoryPath> : This parameter takes the path for the Local Repository Directory. Maven will automatically use the path configured in the settings by default. 
  • <packaging> : Takes a String as the parameter type that contains the packaging type of the artifact that needs to be installed. By default, this value is generally taken from the POM file. 
  • <pomFile> : The parameter here is of File Type. It essentially sends the location of an existing POM file that needs to be installed along the main artifact. 
  • <sources> : Takes a File Type containing a bundled form of the sources for the artifact. 
  • <version> : This String parameter specifies the version of the artifact that needs to be installed. It’s retrieved from the POM file by default. 

 

2. Creating A Command

Once you’ve decided on all the parameters and vice versa, it’s time to create a prompt that can execute all of them to install the JAR file into a Maven Project according to your requirements. 🤨

The Apache Maven Install Plugin is known by the following name:

install:install-file

 

Therefore, you’d have to write your command as follows:

mvn install:install-file –Dfile=C:\dev\app.jar -DgroupId=com.roufid.tutorials -DartifactId=example-app -Dversion=1.0

 

This command will include all the parameters and the necessary values to get the installer up and running. 

 

3. Adding To POM File

Your POM file is your central brain of operations. Therefore, it must contain all the data necessary for your Maven Project to install and run. 

To ensure your pom.xml file is not lacking, add the remaining parameter values in it in terms of tags. 

Here’s how you can do that 😎:

<dependency>

    <groupId>com.roufid.tutorials</groupId>

    <artifactId>example-app</artifactId>

    <version>1.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/example-library.jar</systemPath>

</dependency>

 

Once done, wait for the process to complete, and you’d be good to go!

In this example, the groupId, artifactId, and version elements identify the JAR file, and the systemPath element can also be used to specify the location of the JAR file on the local system. The scope element should be set to system to indicate that the JAR file is not available in a public repository.

Once you have added the dependency element to the pom.xml file, you will need to place the JAR file in the specified location. In this example, the JAR file should be placed in a lib directory at the root of the project.

You can also specify multiple JAR files by adding additional dependency elements to the pom.xml file.

To use the JAR files in your Maven project, you will need to import them in your Java code using the import statement. Here’s an example of how to do this:

import com.example.ExampleClass;

 

In this example, we are importing a class called ExampleClass from the example-library JAR file.

 

 

Method 2: Using System Scopes

1. Understanding System Scopes

System scopes are the ones describing all of the current systems that an application package is supposed to interact with or change. 

Here’s what they do in general:

  • Replacing the system entirely or partially.
  • Managing business objects or being the observing party.
  • Providing business objects to a system.
  • Acting as an interface with the system by accessing the business and nonbusiness objects.
  • Synchronizing execution times with the system.

 

2. Converting The JAR To A System Scope

Since System Scopes are responsible for interconnecting the mentioned tasks, you can use their authority to make your Maven Project run the JAR file. 

To do this, you’d have to add the following kinds of dependencies to your POM.xml file:

<dependency>

    <groupId>com.roufid.tutorials</groupId>

    <artifactId>example-app</artifactId>

    <version>1.0</version>

    <scope>system</scope>

    <systemPath>${basedir}/lib/yourJar.jar</systemPath>

</dependency>

 

Once done, you’re pretty much good to go! 😎

 

 

Method 3: Creating Local Repositories

1. Deploying The Local JAR Files

It’s also worth noting that you can add JAR files to a Maven project by installing them to your local repository. This is useful if you have a JAR file that is not available in a public repository and you don’t want to specify the system scope in the pom.xml file.

This method is quite similar to the first one. However, we will use a second repository to interconnect the JAR files. 

This one will be entirely local. Therefore, you’d have to deploy the files into it essentially. 

To do this, deploy the local JAR files by using the command template below:

mvn deploy:deploy-file -Dfile=<path-to-file> -DgroupId=<group-id>

 

Once all the JAR files have been deployed, add the repositories in your pom.xml file, and you’re pretty much done:

<repositories>

    <repository>

        <id>maven-repository</id>

        <url>file:///${project.basedir}/maven-repository</url>

    </repository>

</repositories>

 

Finally, include the dependencies in your pom.xml file: 😉

<dependency>

    <groupId>com.roufid.tutorials</groupId>

    <artifactId>example-app</artifactId>

    <version>1.0</version>

</dependency>

 

To install a JAR file to your local repository, you will need to use the install:install-file goal of the maven-install-plugin. Here’s an example of how to do this:

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

 

In this example, the file parameter specifies the path to the JAR file, and the groupId, artifactId, and version parameters identify the JAR file. The packaging parameter should be set to jar to indicate that the file is a JAR.

Once the JAR file is installed to your local repository, you can add it to your Maven project by specifying it as a normal dependency in the pom.xml file:

<dependencies>
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>example-library</artifactId>
        <version>1.0</version>
    </dependency>
</dependencies>

 

In this example, we don’t need to specify the systemPath or scope elements because the JAR file is now available in the local repository.

 

 

Conclusion

You can add local JAR files to your Maven Project or use a few workarounds to get the same result. Since the optimal solution varies from use case to use case, we recommend you familiarize yourself with all three. 

Luckily, each solution is based on a few commands, so you won’t have to spend hours understanding each command. 

Be sure to convert the local Maven Project into your main one in case you opt for the third solution. 

If you’re having difficulties with the parameters in your pom.xml file 😑, we recommend you dry-run it without the optional arguments to debug the issue. 

By following these steps and using the provided example code as a guide, you should be able to include external JAR files in your Maven project.

Lastly, let us know in the comments:

  • Were you able to add your local JAR files to your Maven Project?
  • Have you found a more straightforward plugin that can do all this and more with less effort?
  • What kind of project are you intending to create using Maven?
  • Are there any other points or helpful information you believe we should’ve mentioned?

Feel free to share this article amongst your friends and peers, so they don’t have to rack their brains while trying out Maven. 

 

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts
Total
0
Share