Maven is still a popular project build tool that is widely used today. The convenience is that we just need to introduce dependencies and plug-ins into the configuration file POM.xml, and he can automatically download, configure, and run the program from the central repository. But what if we wrote a jar external that we wanted to put into Maven’s central repository and make it easier for others and ourselves to use?

Sign up for your first account – go to Sonatype to sign up for an account and apply for a work order

Go to issues.sonatype.org, register an account, and click on the New button to create a work order:

Community Support-Open Source Project Repository Hosting (OSSRH)

Problem type: New Project

The rest are as follows:

Wait for administrator review. When you are done, the administrator will prompt you to verify that the domain name is yours or that the gitee or Github space is yours, such as mine:

Let me create a new public warehouse in my Gitee account with the space address: gitee.com/swsk33/OSSR…

After creating a new warehouse, click the “Remarks” button below to reply the administrator and tell him that I have created a new warehouse.

After verification, the administrator will then reply to you with some addresses, which means that your work order is approved and you can upload the project.

Two, configure our project

Now we can upload the project.

Go to our Maven installation directory, edit the Settings. XML file in the conf folder in the directory, go to the < Servers > TAB and add the following:

<id> Create a snapshot server id</id> <username> Sonatype username you just registered </username> <password> Sonatype username you just registered </password> </server> <server> <id> customize a release server id</id> <username> sonatype username you just registered </username> <password> Sonatype user password you have just registered </password> </server>Copy the code

This configuration is to configure our Sonatype server account so that our project can be uploaded. Here is an example:

<server>
	<id>snapshot</id>
	<username>user</username>
	<password>123456</password>
</server>

<server>
	<id>release</id>
	<username>user</username>
	<password>123456</password>
</server>
Copy the code

Reconfigure our project’s POM.xml to look like this:

The < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > The < modelVersion > 4.0.0 < / modelVersion > <! <artifactId> <artifactId> <version> version </version> <name> Application name </name> <description> Description </description> <url> Application url </url> <! <packaging>jar</packaging> <! -- Maven project configuration --> <properties> <java.version>1.8</java.version> <maven.compiler.source>${java.version}</maven.compiler.source> <maven.compiler.target>${java.version}</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <! -- License information, --> <license> <license> <name>The Apache Software license, Version2.0 < / name > < url > http://www.apache.org/licenses/ < / url > < distribution > '< / distribution > < / license > < / licenses > <! -- Hosting warehouse information, </connection> <developerConnection> </developerConnection> </scm> <! </developer> </developer> </developer> </developer> </developer> </developer> <! -- Repository address configuration information --> <distributionManagement> <snapshotRepository> <! -- id with setting. XML server id is consistent - > < id > snapshot < / id > < url > https://oss.sonatype.org/content/repositories/snapshots < / url > </snapshotRepository> <repository> <! XML server id --> <id>release</id> <url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url> </repository> </distributionManagement> <! <build> <plugins> <! </groupId> <artifactId> </artifactId> </artifactId> </artifactId> </artifactId> </artifactId> <version>2.2.1</version> <executions> <execution> <id> ADD-sources </id> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin> <! </groupId> <artifactId> </artifactId> </artifactId> </artifactId> </artifactId> Executions > <execution> <id> ADD-javadocs </id> <goal> </goal> </goal> </goal> </execution> </executions> </plugin> <! </groupId> <artifactId> </artifactId> </artifactId> </artifactId> </artifactId> Executions > <execution> <id>sign-artifacts</id> <phase>verify</phase> <goal>sign</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>Copy the code

A quick word about this configuration file.

  • <groupId>It must be the groupId we just applied for the work order, and the package name should be chosen by ourselves.
  • <version>It is the application version. Note that the version does not contain -snapshot, or it may not be found in the repository and published after uploading.
  • <url>,<name>and<description>Is the project website, name and description, these three are necessary, one cannot be absent, otherwise the uploaded project is unable to pass the verification.
  • <licenses>,<developers>and<scm>This is also necessary, according to their own actual situation to fill in.
  • <distributionManagement>To set the Snapshot repository (<snapshotRepository>) and the release repository (<repository>) address, fill in above, it is necessary to note that the two warehouse configuration must and we set beforesettings.xmlIn the<servers>In the<id>Otherwise, the file cannot be uploaded.
  • <build>The parts are the components necessary for the release project, which have been configured in the configuration file above and do not need to be changed, as shown in the comments above.

You can copy all of the above examples as a template and make your own changes.

Third, generate the secret key and upload the project

When uploading the project, we must use the secret key to upload the project.

To install git, right-click git bash and run the following command:

gpg --gen-key
Copy the code

You will then be prompted to enter your name and email address:

When you have finished typing, a window will pop up asking you to set the password for the key and confirm:

Set your own password and remember it because you’ll need it later.

Then enter the command to query the generated key:

gpg --list-keys
Copy the code

There is a hexadecimal number under the pub field, which is the id of the secret key.

Then we need to upload the secret key to the public key server. We need to upload it to both servers, using the following command:

GPG -- keyserver hkp://pool.sks-keyservers.net - send - keys secret key ID GPG -- keyserver hkp://keyserver.ubuntu.com: 11371 - send - keys secret key IDCopy the code

Select * from GPG –list-keys where ID = ID;

Then verify that it was successfully uploaded to both servers:

GPG -- keyserver hkp://pool.sks-keyservers.net -- recv - keys secret key ID GPG -- keyserver hkp://keyserver.ubuntu.com: 11371 - recv - keys secret key IDCopy the code

If the command output is as follows:

This completes the configuration of the secret key.

Note that the first public key address may fail to be uploaded (pool.sks-keyservers.net), that is, no data is found when you use recv-keys after the upload. At this time, you can repeat upload or change the above address to the following mirror station:

hkp://ha.pool.sks-keyservers.net
hkp://p80.pool.sks-keyservers.net:80
Copy the code

Replace the address in the preceding command.

Next, open git bash in our project folder and type the upload project command:

mvn clean deploy
Copy the code

During the execution, a window will pop up asking for the password of the secret key, which is the password we set when we first generated the secret key.

Wait for the upload. If the green “Build Success” is displayed, the upload is successful.

Four, go to the repository to release the project to Maven central repository

The above steps only upload the project to Sonatype, not publish it.

Go to oss.sonatype.org, log in, and click Staging Repositories next to you to see our published project.

Check the item and click Close to start the item verification.

I’ve already clicked it here, so it’s gray.

Click on the active TAB below to see the progress of close:

Display All rules Passed: Central Sync Requirement and you are ready to publish.

Click the release button above to publish to the central repository.

Once you’re done, in about two hours, you’ll be able to find your published project on search.maven.org!

Five, at a later release

In the future, if groupId remains unchanged, we do not need to apply for work order. Step 2, Step 3, step 4: Configure servers. Settings. The key has already been generated and does not need to be generated again, i.e. the key generation in step 3 is not needed either. However, if you change your computer, you will need to reconfigure settings. XML and regenerate and upload the key.