How to Build a Reusable Java
Library for teams
In this document, I’ll walk you through a step-by-step
approach to create a simple, reusable Java library
designed for easy sharing across teams and
microservices.
For Collaboration:
Email: Linkedin:
[email protected] Nestor Abiangang A.
Why create a Reusable Java
Library
Why use a reusable shared Java library?
Centralize and reuse code – Share common, well-tested components
such as contracts, DTOs, and utilities across multiple services.
Improve consistency – Ensure uniform standards and reduce
duplication across teams and services.
Accelerate delivery – Speed up development while minimizing
integration bugs.
Every production-grade reusable Java library follows a streamlined CI
pipeline to ensure quality, maintainability, and ease of adoption
across teams. In our case, the process includes:
Build – Compile the library.
Test – Run some tests (placeholder for real tests) to indicate
success.
Generate JavaDocs – Create API documentation from source code.
Obfuscate – Minify and protect the compiled code.
Deploy Docs – Publish generated JavaDocs to our documentation site.
Deploy Packages – Publish the packaged JAR to a package registry
like github or maven registry.
For Collaboration:
Email: Linkedin:
[email protected] Nestor Abiangang A.
Create a Java project with some
packages
Start by creating a project organized into distinct packages
for the various contracts such as enums, DTOs, and interfaces.
Add .github directory to keep the github action CI/CD files.
Don’t forget to add a professional readme file to document the
repository.
contract-library/
├── pom.xml
├── README.md
├── .github/
│ └── workflows/
│ └── ci.yaml
└── src/
├── main/java/com/contractlibs/
│ ├── dto/
│ │ ├── response/ApiResponse.java
│ │ └── error/ApiErrorResponse.java
│ ├── enums/AccountStatus.java
│ ├── events/AccountCreatedEvent.java
│ └── utility/AccountNumberGenerator.java
└── test/java/... (tests, if any)
For Collaboration:
Email: Linkedin:
[email protected] Nestor Abiangang A.
Add the following plugins on your pom.xml or
build.gradle file
If you’re using Maven, include the necessary plugins and dependencies in your pom.xml. For Gradle users,
equivalent configurations can be added to your build.gradle file.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.3.1</version>
<executions>
<execution>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<distributionManagement>
<repository>
<id>github</id>
<url>https://maven.pkg.github.com/your-github-username/library-name</url>
</repository>
</distributionManagement>
For Collaboration:
Email: Linkedin:
[email protected] Nestor Abiangang A.
Add the various contracts or DTOs in the
various packages
Note: Make sure to add comments on the fields, methods and class to ensure
proper Javadocs generation during the genrate docs job in the CI pipeline.
package com.contractlibs.events;
/**
* Base class for all domain events in the account microservice.
* <p>
* Provides common metadata such as a unique event ID and creation timestamp
* to support traceability, auditing, and idempotency across distributed systems.
*/
public abstract class BaseEvent {
private final UUID eventId;
private final LocalDateTime createdAt;
/**
* Constructs a new BaseEvent with auto-generated event ID and creation timestamp.
*/
public BaseEvent() {
this.eventId = UUID.randomUUID();
this.createdAt = LocalDateTime.now();
}
/**
* Returns the unique identifier for this event.
*
* @return UUID representing the event ID
*/
public UUID getEventId() {
return eventId;
}
/**
* Returns the timestamp when this event was created.
*
* @return LocalDateTime of event creation
*/
public LocalDateTime getCreatedAt() {
return createdAt;
}
}
For Collaboration:
Email: Linkedin:
[email protected] Nestor Abiangang A.
Add a ci/cd pipeline file to automate the
build - test - obfuscate - generate docs -
deploy stages.
Add a ci.yaml file inside the .github/workflows/ directory. This file defines your CI/CD pipeline to
run automatically with GitHub Actions on each push or pull request.
Note: This CI pipeline is a solid starting point but can be further optimized and customized to better
fit your project’s needs. This is a simplified pipeline; full details are available via the provided
link: https://shorturl.at/hW6ty
name: Maven CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- run: echo "Tests ran successfully."
build:
runs-on: ubuntu-latest
needs: test
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with: { java-version: '17' }
- run: mvn clean package -DskipTests
obfuscate:
runs-on: ubuntu-latest
needs: build
steps:
- run: echo "Run code obfuscation here"
generate-docs:
runs-on: ubuntu-latest
needs: build
steps:
- run: mvn javadoc:javadoc
deploy:
runs-on: ubuntu-latest
needs: [obfuscate, generate-docs]
steps:
- run: echo "Deploy docs and package to registry"
For Collaboration:
Email: Linkedin:
[email protected] Nestor Abiangang A.
You can write some unit test cases for the
library then git add commit and push the code
to see the pipeline do the job
Create repository for the library on Github or Gitlab, add the project by commiting and pushing to
the repository.
After a successful pipeline run, your library will be deployed to Github packages and the library
docs will be deploy to Github pages.
deployed dependency to be shared or use in a project:
Install 1/2: Add this to pom.xml:
<dependency>
<groupId>com.contractlibs</groupId>
<artifactId>contract-library</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
Install 2/2: Run via command line
mvn install
Javadocs Library documentation link:
https://martourez21.github.io/contract-library/
For Collaboration:
Email: Linkedin:
[email protected] Nestor Abiangang A.
Thank you for reading. Please like,
repost and connect
Link to the medium
article:https://shorturl.at/hW6ty
For Collaboration:
Email: Linkedin:
[email protected] Nestor Abiangang A.