Getting Started with Maven | Developing and Using Your Own Library | Creating a Library Project

Maven can automatically download libraries from the central repository and use them. This may raise the question: are the only usable libraries those registered in the central repository?

That is not the case. You can, of course, reference and use libraries you created yourself in a project.

Creating a Library Project

Let’s create a simple library and use it. In this example, we will create a library project named “mylib.” Run the following command and enter the project settings to generate it.

Project Creation Command

$ mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart

Input Information

  • Group ID: com.devkuma.mylib
  • Artifact ID: MyLib
  • Version: 1.0-SNAPSHOT (default)
  • Package: com.devkuma.mylib (default)

Once the project is created, add a class for simple processing and use it as a library.

Because maven-archetype-quickstart was specified as the template, this project is a very ordinary Java application project. There is nothing special about it just because it will be used as a library.

Creating MyLib.java

A class named App.java is prepared by default, but we will use that as the main class for the application. Separately, create a class for the library.

Inside the project, create a Java source code file named “MyLib.java” under /src/main/java/com/devkuma/mylib/. Write it as follows.

package com.devkuma.mylib;
 
import java.util.*;
 
public class MyLib {
    private List<Integer> data = new ArrayList<Integer>();
 
    public MyLib(Integer...n){
        super();
        data = Arrays.asList(n);
    }
 
    public void add(int n){
        data.add(n);
    }
 
    public int getTotal(){
        int num = 0;
        for(int n:data){
            num += n;
        }
        return num;
    }
 
    @Override
    public String toString(){
        String res = "{data:[";
        for(int n:data){
            res += n + ", ";
        }
        res += "]}";
        return res;
    }
}

This provides methods that collect several integers in a List and display their total. It is simple, but enough to check how the library works.

For now, leave pom.xml in its default state. Even though this is a library, the project itself is still an ordinary Java application, so nothing special is needed.

Editing App.java

Now try using the MyLib class you created. Open the default App.java and write the source code as follows. Then build and run the application.

package com.devkuma.mylib;
 
public class App {
 
    public static void main( String[] args )    {
        MyLib lib = new MyLib(10, 20, 30, 40);
        System.out.println("MyLib " + lib);
        System.out.println("total " + lib.getTotal());
    }
}

Create the Package

$ mvn package

Run the App Class Using the Package

$ java -classpath target\MyLib-1.0-SNAPSHOT.jar com.devkuma.mylib.App

When you run it, the following is printed to the console. This is the result of creating and using the MyLib class from the App class.

MyLib {data : 10, 20, 30, 40,]}
total 100

For now, we have confirmed that MyLib works correctly.

Installing to the Local Repository

The package is now complete, but MyLib cannot be used from other projects as-is. This is because MyLib is not prepared in a repository.

A repository is a place where libraries are registered. The central repository is the most widely used one, but a repository is also prepared in the local environment. This is called the “local repository.”

The local repository is located in the user folder under the name .m2.

/Users/{user folder}/.m2

When you need to use a library you created yourself, add the library to this local repository. Then other projects can reference and use it. Run the following command.

$ mvn install

This command installs the package into the local repository. Now MyLib has been added to the local repository and can be referenced from other projects.

Using It from SampleMavenApp

Let’s use the MyLib you created from another program. Here, we will use it from the SampleMavenApp project used so far. The sample App.java code is shown below.

package com.devkuma;
 
import com.devkuma.MyLib;
 
public class App {
     
    public static void main( String[] args ){
        MyLib lib = new MyLib(123, 456, 78, 90);
        System.out.println("total " + lib.getTotal());
        System.out.println(lib);
    }
}

To use MyLib inside SampleMavenApp, register MyLib as a dependency library in the project. Add the MyLib settings inside the <dependencies> tag in pom.xml.

<dependency>
    <groupId>com.devkuma.mylib</groupId>
    <artifactId>MyLib</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

This is the <dependency> tag for MyLib. Add this tag to <dependencies> in pom.xml.

Checking Operation

Now check whether the program works correctly. First, package the application.

$ mvn package

This creates the Jar file. Next, run the program and check its operation.

mvn exec:java

Because SampleMavenApp has the exec-maven-plugin configuration, the App class can be run with exec:java. For details, see Java Application Development.

At this point, the external library can be used. However, this is still not perfect. Try running it with the regular java command.

$ java -classpath target\SampleMavenApp-1.0.jar com.devkuma.App

The program does not run and an error occurs. Why is that? Can it not be run with the java command? The reason is that the library is not included.

The Jar file stored in target by mvn package does not actually include the MyLib classes. It only contains the classes from the SampleMavenApp project. It worked with exec:java because it referenced the library in the local repository.

In contrast, the regular java command does not reference the local repository. Therefore, you would have to copy the MyLib Jar file into the lib folder of Java or the JDK to use it.

A more reliable approach is to include MyLib inside the Jar file as well. For how to include it, see maven-assembly-plugin.