Introducing Random Generator to Maven Central
With the project grunt work done, it's time to put our Random Generator onto Maven Central for everyone to use, including a simple example of the library in action.
Join the DZone community and get the full member experience.
Join For FreeContinuing with my series on a library I have created, called RandomGenerator, I am going to focus on the steps necessary to getting the library into Maven Central so that others can utilize my creation in their Java projects. I will also include a simple example of using RandomGenerator in a Maven project.
If you are interested in seeing my actual code or JavaDocs, they can be found at the following URLs:
- https://gitlab.com/johnjvester/RandomGenerator
- http://johnjvester.gitlab.io/RandomGenerator-JavaDoc/
Publishing to Maven Central
The process to add your project into Maven Central is quite detailed. The reason for this is to ensure a minimum level of quality within their repository. These same requirements include providing metadata so that end-users are able to find all the relevant details on your contribution.
At a high level, the steps required to add your component into Maven Central are listed below:
Supply JavaDoc and Source Files
Sign Files with GPG/PGP
Sufficient Metadata
groupId
artifactId
Version
Project Name
Description
URL
License Information
Developer Information
Source Control (SCM) Information
You can read more about this process here:
http://central.sonatype.org/pages/requirements.html
Once you are ready to begin the process, the required steps are listed here:
http://central.sonatype.org/pages/producers.html
At first glance, reviewing the Producers page may seem like the process is very complicated. However, I was able to figure everything out in my spare time, and I am far from an expert on everything involved with the process.
Once finished, your project will appear on Maven Central or any of the related mirrors. Below, is a link to the 1.2 version of RandomGenerator: RandomGenerator 1.2 on Maven Central
Using RandomGenerator in a Maven Project
With RandomGenerator 1.2 available for use in Maven Central, the library can be added to your project without much effort. Using IntelliJ, I was able to do so following the steps provided below:
Launch IntelliJ
Select File | New Project ...
Select Maven option and single-click Next
Enter a groupId and artifictId for you test project. I used a groupId of test and an artifact of testing, just for fun.
Single-click the Next button to continue.
Enter a Project Name, which I simply called testing, and single-click the Finish button.
Open the pom.xml and include the following dependency:
<dependencies>
<dependency>
<groupId>com.gitlab.johnjvester</groupId>
<artifactId>random-generator</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
Navigate to the src/main/java folder and create a new class using File | New | Java Class
Provide a name for your Java class. I simply chose Test as my class name.
I decided to mimic the example in the README.md for RandomGenerator, which introduced an inner class called Destination:
public static class Destination {
private String name;
private Integer rating;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getRating() {
return rating;
}
public void setRating(Integer rating) {
this.rating = rating;
}
}
I then added a main() method for my Test class, which included a reference to randomGenerator:
public static void main(String[] args) {
RandomGenerator randomGenerator = new RandomGenerator();
}
At this point, the Test class has access to the RandomGenerator library, without having to seek and find the jar file.
I then added some code (which I realize is very simplistic) to map out the options in the README.md on GitLab for RandomGenerator. The full main() method is shown below:
public static void main(String[] args) {
RandomGenerator randomGenerator = new RandomGenerator();
ArrayList<Destination> myList = new ArrayList<Destination>();
Destination destination = new Destination();
destination.setName("Boston, MA");
destination.setRating(2);
myList.add(destination);
destination = new Destination();
destination.setName("Las Vegas, NV");
destination.setRating(3);
myList.add(destination);
destination = new Destination();
destination.setName("Maui, HI");
destination.setRating(5);
myList.add(destination);
destination = new Destination();
destination.setName("Miami, FL");
destination.setRating(4);
myList.add(destination);
destination = new Destination();
destination.setName("New York, NY");
destination.setRating(1);
myList.add(destination);
destination = new Destination();
destination.setName("San Diego, CA");
destination.setRating(4);
myList.add(destination);
destination = new Destination();
destination.setName("Tampa, FL");
destination.setRating(5);
myList.add(destination);
System.out.println("before");
for (Destination thisDestination : myList) {
System.out.println("name = " + thisDestination.getName());
}
List<Destination> myRandomizedList = randomGenerator.randomize(myList);
System.out.println("after - standard");
for (Destination thisDestination : myRandomizedList) {
System.out.println("name = " + thisDestination.getName());
}
myRandomizedList = randomGenerator.randomize(myList, true);
System.out.println("after - standard with rating");
for (Destination thisDestination : myRandomizedList) {
System.out.println("name = " + thisDestination.getName());
}
}
The example provides outputs for the original list, then an unranked sort of the list, followed by a weighted list sort of the original list.
Running the main() method within IntelliJ produced the following results:
before
name = Boston, MA
name = Las Vegas, NV
name = Maui, HI
name = Miami, FL
name = New York, NY
name = San Diego, CA
name = Tampa, FL
after - standard
name = Boston, MA
name = Miami, FL
name = Tampa, FL
name = New York, NY
name = San Diego, CA
name = Las Vegas, NV
name = Maui, HI
after - standard with rating
name = Tampa, FL
name = Miami, FL
name = Maui, HI
name = San Diego, CA
name = Boston, MA
name = Las Vegas, NV
name = New York, NY
Process finished with exit code 0
Conclusion
When I started working on my project over the recent holiday break, I had planned to continue working on an application I started working on a few years ago. As I looked at the randomization code in the application, I felt like it would provide more value as a stand-alone library. Now, spending some of my free time focused on this effort has produced the reality that is RandomGenerator 1.2.
It feels great to give back to the open source community and feedback, suggestions and contributions are certainly welcome. I don't have any grand plans for version 1.3 of RandomGenerator, but I am not opposed to any new ideas either.
Have a really great day!
Opinions expressed by DZone contributors are their own.
Comments