DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Integrate Cucumber in Playwright With Java
  • Using Python Libraries in Java
  • Using Lombok Library With JDK 23
  • Selenium Grid Tutorial: Essential Tips and How to Set It Up

Trending

  • Detection and Mitigation of Lateral Movement in Cloud Networks
  • Beyond Code Coverage: A Risk-Driven Revolution in Software Testing With Machine Learning
  • Scaling DevOps With NGINX Caching: Reducing Latency and Backend Load
  • Optimizing Integration Workflows With Spark Structured Streaming and Cloud Services
  1. DZone
  2. Coding
  3. Java
  4. How to Create a Java Library: From Scratch to Maven Central

How to Create a Java Library: From Scratch to Maven Central

If you usually need to rewrite or copy similar code between different projects, it may be time to stop replicating it and create a library.

By 
Helber Belmiro user avatar
Helber Belmiro
DZone Core CORE ·
Apr. 22, 21 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
49.6K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

If you usually need to rewrite or copy similar code between different projects, it may be time to stop replicating it and create a library.

You can also share it as open-source, so other people can use it and help you improve it.

To use your library in different projects, you have to publish it on a repository like Maven Central Repository.

So let’s run through the entire process and publish a library for padding Strings. We’ll start by creating our project from scratch.


The Complete Java Master Class Bundle.*

*Affiliate link. See Terms of Use.

Note

If you want to skip the project creation:

  • You can use your own project and jump ahead to Preparing pom.xml to Deploy on Maven Central; or,

  • Download my project from GitHub and jump ahead to Requesting Access to Maven Central.

** If you use my project, don’t forget to change its group ID! **


Creating the Project

Run the following command on your terminal:

mvn archetype:generate -DgroupId=com.thegreatapi.demolibrary -DartifactId=demolibrary -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false

Note

Use your own group ID on the command. If you use com.thegreat.api.demolibraryyou won’t be able to publish to Maven Central.

If you’re not sure about what group ID to use, look at this article.

That command will create a project with the following pom.xml:

XML
 




x
75


 
1
<?xml version="1.0" encoding="UTF-8"?>
2

          
3
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
    <modelVersion>4.0.0</modelVersion>
6

          
7
    <groupId>com.thegreatapi.demolibrary</groupId>
8
    <artifactId>demolibrary</artifactId>
9
    <version>1.0-SNAPSHOT</version>
10

          
11
    <name>demolibrary</name>
12
    <!-- FIXME change it to the project's website -->
13
    <url>http://www.example.com</url>
14

          
15
    <properties>
16
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17
        <maven.compiler.source>1.7</maven.compiler.source>
18
        <maven.compiler.target>1.7</maven.compiler.target>
19
    </properties>
20

          
21
    <dependencies>
22
        <dependency>
23
            <groupId>junit</groupId>
24
            <artifactId>junit</artifactId>
25
            <version>4.11</version>
26
            <scope>test</scope>
27
        </dependency>
28
    </dependencies>
29

          
30
    <build>
31
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
32
            <plugins>
33
                <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
34
                <plugin>
35
                    <artifactId>maven-clean-plugin</artifactId>
36
                    <version>3.1.0</version>
37
                </plugin>
38
                <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
39
                <plugin>
40
                    <artifactId>maven-resources-plugin</artifactId>
41
                    <version>3.0.2</version>
42
                </plugin>
43
                <plugin>
44
                    <artifactId>maven-compiler-plugin</artifactId>
45
                    <version>3.8.0</version>
46
                </plugin>
47
                <plugin>
48
                    <artifactId>maven-surefire-plugin</artifactId>
49
                    <version>2.22.1</version>
50
                </plugin>
51
                <plugin>
52
                    <artifactId>maven-jar-plugin</artifactId>
53
                    <version>3.0.2</version>
54
                </plugin>
55
                <plugin>
56
                    <artifactId>maven-install-plugin</artifactId>
57
                    <version>2.5.2</version>
58
                </plugin>
59
                <plugin>
60
                    <artifactId>maven-deploy-plugin</artifactId>
61
                    <version>2.8.2</version>
62
                </plugin>
63
                <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
64
                <plugin>
65
                    <artifactId>maven-site-plugin</artifactId>
66
                    <version>3.7.1</version>
67
                </plugin>
68
                <plugin>
69
                    <artifactId>maven-project-info-reports-plugin</artifactId>
70
                    <version>3.0.0</version>
71
                </plugin>
72
            </plugins>
73
        </pluginManagement>
74
    </build>
75
</project>



Let’s change it to use Java 11 instead of 1.7.

XML
 




xxxxxxxxxx
1


 
1
    <properties>
2
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3
        <maven.compiler.source>11</maven.compiler.source>
4
        <maven.compiler.target>11</maven.compiler.target>
5
    </properties>



And then we create a LICENSE file. I’ll use Apache 2 License, but you can use any license you want. To use Apache 2 License, you have to copy the content from http://www.apache.org/licenses/LICENSE-2.0.txt and paste it into your LICENSE file.

Implementing the Library

Now let’s create an interface StringPadder in the package com.thegreatapi.demolibrary.stringpadder. That will be the interface that clients will use to pad Strings.

Java
 




xxxxxxxxxx
1
50


 
1
package com.thegreatapi.demolibrary.stringpadder;
2

          
3
/**
4
 * Pads a {@link String}.
5
 * <p>
6
 * The instances of classes that implement this interface are thread-safe and immutable.
7
 */
8
public interface StringPadder {
9

          
10
    /**
11
     * Returns a new {@link String} that right-aligns the characters in the specified String by padding them with spaces
12
     * on the left, for a specified total length.
13
     *
14
     * @param stringToPad the {@link String} to be padded
15
     * @param totalLength total length of the new {@link String}
16
     * @return the padded {@link String}
17
     */
18
    String padLeft(String stringToPad, int totalLength);
19

          
20
    /**
21
     * Returns a new {@link String} that right-aligns the characters in the specified String by padding them with the
22
     * specified char on the left, for a specified total length.
23
     *
24
     * @param stringToPad the {@link String} to be padded
25
     * @param totalLength total length of the new {@link String}
26
     * @return the padded {@link String}
27
     */
28
    String padLeft(String stringToPad, int totalLength, char paddingCharacter);
29

          
30
    /**
31
     * Returns a new {@link String} that left-aligns the characters in the specified String by padding them with spaces
32
     * on the left, for a specified total length.
33
     *
34
     * @param stringToPad the {@link String} to be padded
35
     * @param totalLength total length of the new {@link String}
36
     * @return the padded {@link String}
37
     */
38
    String padRight(String stringToPad, int totalLength);
39

          
40
    /**
41
     * Returns a new {@link String} that left-aligns the characters in the specified String by padding them with the
42
     * specified char on the left, for a specified total length.
43
     *
44
     * @param stringToPad the {@link String} to be padded
45
     * @param totalLength total length of the new {@link String}
46
     * @return the padded {@link String}
47
     */
48
    String padRight(String stringToPad, int totalLength, char paddingCharacter);
49

          
50
}



Now let’s create the implementation of the StringPadder interface.

Java
 




xxxxxxxxxx
1
38


 
1
package com.thegreatapi.demolibrary.stringpadder;
2

          
3
class StringPadderImpl implements StringPadder {
4

          
5
    StringPadderImpl() {
6
    }
7

          
8
    @Override
9
    public String padLeft(String stringToPad, int totalLength) {
10
        return padLeft(stringToPad, totalLength, ' ');
11
    }
12

          
13
    @Override
14
    public String padLeft(String stringToPad, int totalLength, char paddingCharacter) {
15
        return getStringToBeAdded(stringToPad, totalLength, paddingCharacter) + stringToPad;
16
    }
17

          
18
    @Override
19
    public String padRight(String stringToPad, int totalLength) {
20
        return padRight(stringToPad, totalLength, ' ');
21
    }
22

          
23
    @Override
24
    public String padRight(String stringToPad, int totalLength, char paddingCharacter) {
25
        return stringToPad + getStringToBeAdded(stringToPad, totalLength, paddingCharacter);
26
    }
27

          
28
    private String getStringToBeAdded(String stringToPad, int totalLength, char paddingCharacter) {
29
        int quantity = totalLength - stringToPad.length();
30

          
31
        if (quantity > 0) {
32
            return Character.toString(paddingCharacter).repeat(quantity);
33
        } else {
34
            return "";
35
        }
36
    }
37

          
38
}



Note that the class is package-private, so the clients can’t use it in their code.

Now we’re going to create a factory for clients to create instances of StringPadder.

Java
 




xxxxxxxxxx
1
20


 
1
package com.thegreatapi.demolibrary.stringpadder;
2

          
3
/**
4
 * Factory for creating instances of {@link StringPadder}.
5
 */
6
public final class StringPadderFactory {
7

          
8
    private StringPadderFactory() {
9
    }
10

          
11
    /**
12
     * Creates an instance of {@link StringPadder}.
13
     *
14
     * @return the new instance
15
     */
16
    public static StringPadder createStringPadder() {
17
        return new StringPadderImpl();
18
    }
19

          
20
}



Creating Tests

Let’s replace JUnit 4 with JUnit 5 and add AssertJ dependency to our pom.xml.

XML
 




xxxxxxxxxx
1
14


 
1
<dependencies>
2
    <dependency>
3
        <groupId>org.junit.jupiter</groupId>
4
        <artifactId>junit-jupiter</artifactId>
5
        <version>5.7.1</version>
6
        <scope>test</scope>
7
    </dependency>
8
    <dependency>
9
        <groupId>org.assertj</groupId>
10
        <artifactId>assertj-core</artifactId>
11
        <version>3.19.0</version>
12
        <scope>test</scope>
13
    </dependency>
14
</dependencies>



Now we’re ready to implement our tests.

Java
 




xxxxxxxxxx
1
59


 
1
package com.thegreatapi.demolibrary.stringpadder;
2

          
3
import org.junit.jupiter.api.Test;
4

          
5
import static org.assertj.core.api.Assertions.assertThat;
6

          
7
class StringPadderImplTest {
8

          
9
    private final StringPadderImpl stringPadder = new StringPadderImpl();
10

          
11
    @Test
12
    void padLeft() {
13
        assertThat(stringPadder.padLeft("thegreatapi.com", 20))
14
                .isEqualTo("     thegreatapi.com");
15
    }
16

          
17
    @Test
18
    void padLeftWithZeros() {
19
        assertThat(stringPadder.padLeft("thegreatapi.com", 20, '0'))
20
                .isEqualTo("00000thegreatapi.com");
21
    }
22

          
23
    @Test
24
    void padRight() {
25
        assertThat(stringPadder.padRight("thegreatapi.com", 20))
26
                .isEqualTo("thegreatapi.com     ");
27
    }
28

          
29
    @Test
30
    void padRightWithZeros() {
31
        assertThat(stringPadder.padRight("thegreatapi.com", 20, '0'))
32
                .isEqualTo("thegreatapi.com00000");
33
    }
34

          
35
    @Test
36
    void padLeftWithInvalidTotalLength() {
37
        assertThat(stringPadder.padLeft("thegreatapi.com", 3))
38
                .isEqualTo("thegreatapi.com");
39
    }
40

          
41
    @Test
42
    void padLeftWithZerosInvalidTotalLength() {
43
        assertThat(stringPadder.padLeft("thegreatapi.com", 3, '0'))
44
                .isEqualTo("thegreatapi.com");
45
    }
46

          
47
    @Test
48
    void padRightInvalidTotalLength() {
49
        assertThat(stringPadder.padRight("thegreatapi.com", 3))
50
                .isEqualTo("thegreatapi.com");
51
    }
52

          
53
    @Test
54
    void padRightWithZerosInvalidTotalLength() {
55
        assertThat(stringPadder.padRight("thegreatapi.com", 3, '0'))
56
                .isEqualTo("thegreatapi.com");
57
    }
58

          
59
}



If we run mvn verify, we should see an output similar to the following:

/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home/bin/java -Dmaven.multiModuleProjectDirectory=/Users/helber/Desktop/demolibrary -Dmaven.home=/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3 -Dclassworlds.conf=/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3/bin/m2.conf -Dmaven.ext.class.path=/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven-event-listener.jar -javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=61185:/Applications/IntelliJ IDEA.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3/boot/plexus-classworlds.license:/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3/boot/plexus-classworlds-2.6.0.jar org.codehaus.classworlds.Launcher -Didea.version=2020.3.3 verify
[INFO] Scanning for projects...
[INFO] 
[INFO] --------------< com.thegreatapi.demolibrary:demolibrary >---------------
[INFO] Building demolibrary 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ demolibrary ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/helber/Desktop/demolibrary/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ demolibrary ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /Users/helber/Desktop/demolibrary/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ demolibrary ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/helber/Desktop/demolibrary/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ demolibrary ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/helber/Desktop/demolibrary/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ demolibrary ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.thegreatapi.demolibrary.stringpadder.StringPadderImplTest
[INFO] Tests run: 8, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.07 s - in com.thegreatapi.demolibrary.stringpadder.StringPadderImplTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 8, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] 
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ demolibrary ---
[INFO] Building jar: /Users/helber/Desktop/demolibrary/target/demolibrary-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.378 s
[INFO] Finished at: 2021-04-05T06:36:12-03:00
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0


Our implementation is ready. The next step is to deploy our library to Maven Central.

Preparing pom.xml to Deploy on Maven Central

There are some requirements we need to satisfy in order to upload our library to Maven Central. We can find those requirements on https://central.sonatype.org/pages/requirements.html.

The first thing we have to change in our pom.xml is to define a non-SNAPSHOT version for our library. To do that, we just need to remove the -SNAPSHOT suffix. Let’s define our version as 0.0.1.

XML
 




xxxxxxxxxx
1


 
1
<version>0.0.1</version>



Next, we have to add a description and a URL to our project. In my case, the URL should be http://thegreatapi.com. Your URL must differ from mine because you have to own the domain. You can also use a GitHub URL if you prefer.

XML
 




xxxxxxxxxx
1


 
1
<description>
2
    This project is a library for padding Strings in Java.
3
    DON'T USE THIS IN PRODUCTION. IT WAS CREATED FOR DEMO PURPOSES ONLY.
4
</description>
5
<url>http://thegreatapi.com</url>


After that, we add the license information. Note that I’m using Apache 2 license. Use the same license that you defined in your LICENSE file.

XML
 




xxxxxxxxxx
1


 
1
<licenses>
2
    <license>
3
        <name>The Apache License, Version 2.0</name>
4
        <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
5
    </license>
6
</licenses>


The next step is to add information about the developers.

XML
 




xxxxxxxxxx
1


 
1
<developers>
2
    <developer>
3
        <name>Helber Belmiro</name>
4
        <email>your@email.com</email>
5
        <organization>The Great API</organization>
6
        <organizationUrl>https://thegreatapi.com</organizationUrl>
7
    </developer>
8
</developers>


And then, add the information about the Source Code Management (SCM). The following information is for my project. Replace it with your project’s information.

XML
 




xxxxxxxxxx
1


 
1
<scm>
2
    <connection>scm:git:git@github.com:hbelmiro/demo-library.git</connection>
3
    <developerConnection>scm:git:ssh://github.com:hbelmiro/demo-library.git</developerConnection>
4
    <url>https://github.com/hbelmiro/demo-library/tree/master</url>
5
</scm>



Maven Central demands you to send the Javadoc and source code. So you have to create it when building your artifacts. You must also sign the artifacts you are sending to Maven Central.

Since we need to do it only when deploying to Maven Central, it might be a good idea to create a profile for this.

XML
 




xxxxxxxxxx
1
58


 
1
<profiles>
2
    <profile>
3
        <id>release</id>
4
        <activation>
5
            <property>
6
                <name>performRelease</name>
7
                <value>true</value>
8
            </property>
9
        </activation>
10
        <build>
11
            <plugins>
12
                <plugin>
13
                    <groupId>org.apache.maven.plugins</groupId>
14
                    <artifactId>maven-gpg-plugin</artifactId>
15
                    <version>1.6</version>
16
                    <executions>
17
                        <execution>
18
                            <id>sign-artifacts</id>
19
                            <phase>verify</phase>
20
                            <goals>
21
                                <goal>sign</goal>
22
                            </goals>
23
                        </execution>
24
                    </executions>
25
                </plugin>
26
                <plugin>
27
                    <groupId>org.apache.maven.plugins</groupId>
28
                    <artifactId>maven-javadoc-plugin</artifactId>
29
                    <version>3.2.0</version>
30
                    <executions>
31
                        <execution>
32
                            <id>attach-javadocs</id>
33
                            <goals>
34
                                <goal>jar</goal>
35
                            </goals>
36
                        </execution>
37
                    </executions>
38
                    <configuration>
39
                        <javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
40
                    </configuration>
41
                </plugin>
42
                <plugin>
43
                    <groupId>org.apache.maven.plugins</groupId>
44
                    <artifactId>maven-source-plugin</artifactId>
45
                    <version>3.2.1</version>
46
                    <executions>
47
                        <execution>
48
                            <id>attach-sources</id>
49
                            <goals>
50
                                <goal>jar-no-fork</goal>
51
                            </goals>
52
                        </execution>
53
                    </executions>
54
                </plugin>
55
            </plugins>
56
        </build>
57
    </profile>
58
</profiles>



You also have to install a GPG client and put it on your command line path. Follow the instructions on https://central.sonatype.org/pages/working-with-pgp-signatures.html to install the GPG client.

The next step is to add the Distribution Management to your pom.xml.

XML
 




xxxxxxxxxx
1
10


 
1
<distributionManagement>
2
    <snapshotRepository>
3
        <id>ossrh</id>
4
        <url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
5
    </snapshotRepository>
6
    <repository>
7
        <id>ossrh</id>
8
        <url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
9
    </repository>
10
</distributionManagement>



Also, add nexus-staging-maven-plugin.

XML
 




xxxxxxxxxx
1
18


 
1
<build>
2
    <pluginManagement>
3
        <plugins>
4
            <plugin>
5
                <groupId>org.sonatype.plugins</groupId>
6
                <artifactId>nexus-staging-maven-plugin</artifactId>
7
                <version>1.6.7</version>
8
                <extensions>true</extensions>
9
                <configuration>
10
                    <serverId>ossrh</serverId>
11
                    <nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
12
                    <autoReleaseAfterClose>true</autoReleaseAfterClose>
13
                </configuration>
14
            </plugin>
15
            ...
16
        </plugins>
17
    </pluginManagement>
18
<build>


Your library is ready to be published to Maven Central.

Requesting Access to Maven Central

Using the OSS Repository Hosting (OSSRH), provided by Sonatype for any open-source project, is the easiest way to publish your project.

The first step is to create a JIRA account.

After that, you have to create a New Project ticket. As an example, you can use the ticket I opened to publish demolibrary.

Officially, they ask you for 2 working days to complete the process, but it normally takes one or two hours.

Note

Use your own group ID. If you use com.thegreat.api.demolibrary you won’t be able to publish to Maven Central.

They will ask if you own the domain specified on groupId and if so, you’ll have to verify the ownership. On my ticket they commented:

Ticket Comment

In my case, I added a TXT record to my DNS. When I did that, I commented on the ticket, and they set the ticket to 'Resolved.'

Releasing the Library

After proving the domain ownership, you’re ready to upload your artifacts. To do that, you first release it to staging.

mvn clean deploy -P release

At this point, your artifacts are stored in a private repository, so you can verify them before releasing them. So log in to https://s01.oss.sonatype.org/ using your JIRA credentials.

On the left menu, click on 'Staging Repositories' and you’ll see your library.

If everything is OK with your library, you can close it, otherwise, you can drop it.

When you close it, on the low section, you can check if it was closed successfully by clicking on the 'Activity' tab. You should see something similar to this:

Activity Tab > Repository Closed

If the repository was successfully closed, now you’re able to promote it to release.

Run the following command using your Repository ID. On the image above, it’s comthegreatapidemolibrary-1005. Don’t forget to replace it with your own ID.

mvn nexus-staging:release -DstagingRepositoryId=comthegreatapidemolibrary-1005

Note

Use your own group ID. If you use com.thegreat.api.demolibrary you won’t be able to publish to Maven Central.

You should see an output similar to:

[INFO]  * Connected to Nexus at https://s01.oss.sonatype.org:443/, is version 2.14.20-02 and edition "Professional"
[INFO] Releasing staging repository with IDs=[comthegreatapidemolibrary-1005]

Waiting for operation to complete...
.........

[INFO] Released
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  28.583 s
[INFO] Finished at: 2021-04-07T07:53:50-03:00
[INFO] ------------------------------------------------------------------------


Now your library is published. On this very first deployment, you have to comment on the JIRA ticket, so they can activate the sync to Maven Central.

After Sonatype activates Maven Central sync for your library, when you successfully release new components, they will be published to Central https://repo1.maven.org/maven2/, typically within 10 minutes, though updates to https://search.maven.org can take up to two hours.

Start Your Library Now

You are now ready to start your own library. It will be great for you and the Java community.

If you still don’t feel prepared, what’s preventing you from taking the next step? Comment here or send me a message on my social media. I’ll be happy to help you.

Apache Maven Java (programming language) Testing

Published at DZone with permission of Helber Belmiro. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Integrate Cucumber in Playwright With Java
  • Using Python Libraries in Java
  • Using Lombok Library With JDK 23
  • Selenium Grid Tutorial: Essential Tips and How to Set It Up

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!