Software Engineering

Release Java Library Using Jitpack

This article will guide you to release your Java Library using Github and Jitpack. Find out and publish your magnificent code.

As a Java Developer we can use an external library using dependency management like Maven or Gradle. Did you know how is the Java library could be there and downloadable automatically in your project? How to make it? Can we do that also?

Currently it’s a common to using a dependency management in software engineering. In Java, Kotlin, and other language which running on JVM we are using Maven or Gradle. Long ago before we know dependency management, when we need an external dependency we have to manually search and download it from Google, and copy-paste to our project. Now we can simply type the dependency we desire to be exist in our project, and click a sync button.

In this session I am not talking much more about dependency management, but we will focusing on how to make your own Java Library and publishing to cloud, so anyone in this internet could use your magnificent masterpiece.

When do we have to create our own library?

You shouldn’t have to, seriously. But, when you have more than one application that have a function to send email. Will you do a copy-paste thing? I am sure it’s a solution. But will you do that forever? Now it’s your time to create your own library to simplify the client application to sending email using specific or multi provider.

Another case, in your office you create a magnificent feature and you want all developer all over the globe using your feature to help them solve their problem. For example, a library to search a city around the world will shown a list of similar city with the country.

Let’s Start!!

Before continue, you will need:

  • Github account
  • IDE for your working station. I am using Intellij
  • Java installed on your computer
  • Connect to Internet of course

Create an Application

Simply create a maven project based application : File - New - Project

create java project using maven framework.
Figure 1 – Create Intellij Project

After it created, open pom.xml and do the change

<?xml version="1.0" encoding="UTF-8"?>
<project  >
   ...
   <packaging>jar</packaging>

</project>

Packaging tag, will tell Maven to build this application in *.jar extension.

Now we will make an employee library. It has a feature to get list of employees and get single employee data.

This is the repository, contains a list of employee data.

public class EmployeeRepository {
	public List<Employee> allEmployees() {
		List<Employee> employees = new ArrayList<Employee>();
		employees.add(new Employee(1L, "Nobita", "Tech"));
		employees.add(new Employee(2L, "Doraemon", "Tech"));
		employees.add(new Employee(3L, "Giant", "Finance"));
		employees.add(new Employee(4L, "Suneo", "Finance"));
		employees.add(new Employee(5L, "Sizuka", "Product"));
		employees.add(new Employee(6L, "Dekisugi", "Product"));
		return employees;
	}
}

Next we go to service that contains the features of this library.

public interface EmployeeService {
	List<Employee> findAllEmployees();
	Employee findEmployee(Long id);
	String findEmployeeToString(Long id);
}

Then the implementation class

public class EmployeeServiceImpl implements EmployeeService {

	private EmployeeRepository repository;

	public EmployeeServiceImpl() {
		this.repository = new EmployeeRepository();
	}

	public List<Employee> findAllEmployees() {
		return repository.allEmployees();
	}

	public Employee findEmployee(Long id) {
		return repository.allEmployees()
				.stream()
				.filter(employee -> id.equals(employee.getId()))
				.findFirst()
				.orElse(null);
	}

	@Override
	public String findEmployeeToString(Long id) {
		Employee employee = findEmployee(id);
		if (null == employee) {
			return "Data tidak ditemukan";
		}
		return String.format("[%s] %s", employee.getDivision(), employee.getName());
	}
}

Then, push your code to you Github Account. This is mine.

Release The Kraken

Go to your github repo and create a new release

github page which focus on release section
Figure 2 – Github Repository, release section

Create a release document. Add a version, title, and description if any. Then click green Release Button

release document to describe the release changes and version

We are doing good in Github

Jitpack

Using https://jitpack.io/ we can publish our java library to the internet, so all over the world developer could use your library. After login with Github Account, put one of your github repo that we want to publish, and click Look Up.

jitpack home page which shows us the specific github repository and its releases
Figure 3 – Jitpack Page

It will automatically find our released library and put it on Jitpack cloud. For client, we can simply click Get It to get accessible library to add to client’s maven pom.xml

maven guide after library released
Figure 4 – Jitpack How To

I think it’s done, Jitpack will very useful if you work for more than one application with same feature. Create your own library also will cut your development time so you can focus on any other things related to business process.

Leave a Reply

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