October 9th, 2020 by cedcraftscodes
A Github account and a new project repository which will contain the codes of our library.
Jitpack Account where we will be publishing our library.
The first thing you need to do is to create your library project. For this tutorial, we are going to create a Spring Boot Library, and later add it as a dependency on other projects that we will be working on.
Go to https://start.spring.io/ and create a basic SpringBoot Project. There are ways to create a Spring Boot Project, if you are interested, you may check out our tutorial on how to create SpringBoot using Intellij Ultimate or maven.
We need to delete unnecessary files in our library. I have deleted the AwesomeLibraryApplication and the equivalent test class. You may also delete the application.properties as externalizing your configuration files is best practice (we won’t do config externalization here).
Delete the following lines of code in your pom.xml
1 2 3 4 5 6 7 8 | <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> |
For this tutorial, I will be adding a method that gets the most frequent integer out of the array.
CustomArrayUtil.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | package com.devcraze.awesomelibrary.util; import java.util.Arrays; public class CustomArrayUtil { public static int mostFrequent(int arr[], int n) { Arrays.sort(arr); int max_count = 1, res = arr[0]; int curr_count = 1; for (int i = 1; i < n; i++) { if (arr[i] == arr[i - 1]) curr_count++; else { if (curr_count > max_count) { max_count = curr_count; res = arr[i - 1]; } curr_count = 1; } } if (curr_count > max_count) { max_count = curr_count; res = arr[n - 1]; } return res; } } |
Testing
Check if your library will still create a jar file. Run “mvn install”. It should create a jar file in your target folder.
After completing the steps above, we need to create a new repository and push our code to that repository in Github. If you do not know how to do that, you may check this tutorial.
1 2 3 4 5 6 | git init git add README.md git commit -m "first commit" git branch -M main git remote add origin git@github.com:yourusername/yourlibrary.git git push -u origin main |
Login to https://jitpack.io/. You may use your Github account. Paste the repository URL in the lookup field.
Choose which commit you would like to get. If the build is successful, you’ll see a green document icon. Click it and you can see the build logs.
Below, you can see how to import your library on different dependency management tools. Open your new Springboot application and add the library as a dependency.
I added the following codes in my pom.xml
(under <dependencies> tag)
1 2 3 4 5 6 7 8 9 10 11 12 | <dependency> <groupId>com.github.cedcraftscodes</groupId> <artifactId>MyAwesomeLibrary</artifactId> <version>c67992f720</version> </dependency> <repositories> <repository> <id>jitpack.io</id> <url>https://jitpack.io</url> </repository> </repositories> |
We were able to import the CustomArrayUtil from our library after adding the dependency.
Comments