Using Your Own Java Library with Maven

Using library in programming might not be a new thing to do when creating an application. A library is helpful because it allows programmers to skip writing some complicated but needed functions or methods.

In this example we gonna use Maven Repository as it is one of the most commonly used repository when creating Java Application.

Creating jar library file

Honestly creating library is not that different from creating another application, so we are gonna talk the detail about that. In this example, i’m using simple java project with Maven. Let’s create a simple method to return a boolean value with integer as a parameter.

code example

After that we can export the project to jar file. This step might be different if using another IDE, but if using VSCode as IDE we can use Java: Export Jar… command from Command Palette.

export project to jar

Importing jar file to local repository

We can import the jar file to our local repository with this following command,

this was taken from http://maven.apache.org/general.html#importing-jars

After the importing process is done, the screen will show this message, and then finally we can use our library as we did with another library from Maven Repository.

success message after importing jar to local repository

Using Library in Application

We can import our imported library using pom.xml as we use library from online Maven Repository, using groupId, artifactId, and version as specified before when importing to our local repository.

code example

Leave a comment

Design a site like this with WordPress.com
Get started