Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Introducing Random Generator to Maven Central

DZone's Guide to

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.

· Java Zone
Free Resource

Try Okta to add social login, MFA, and OpenID Connect support to your Java app in minutes. Create a free developer account today and never build auth again.

Continuing 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 need to get up to speed on RandomGenerator, below are links to my prior articles in this series:

If you are interested in seeing my actual code or JavaDocs, they can be found at the following URLs:

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!

Build and launch faster with Okta’s user management API. Register today for the free forever developer edition!

Topics:
java ,tutorial ,randomness ,generator ,maven central

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}