[JAVA] A new engineer tried to publish his own library to Maven Central Repository

For the first time, I registered and published my own library in Maven Central Repository for half a year as an engineer. There was a place where I was often clogged up, so I will leave that trajectory.

I hope it helps beginners like myself when publishing the library. .. ..

I mainly referred to the following links.

0. The beginning of things

In the first place, I talked about why I, who has little history, suddenly tried to publish the library. For business, I had to upload and update the library to Maven Central Repository. However, I lack knowledge and experience "Maven Central has various libraries and I want to use it with gradle etc. It's a place where you can pull the library. Is it easy to put the library there? " It was a situation like that. .. ..

It's too dangerous to suddenly mess with work in such a situation (sweat) That's why I decided to try it myself.

So this article is

――Maven Central is a place with a lot of libraries, isn't it? --You can write java at a minimum and create a library --You can do basic things on github

I will write it so that I can understand it if I know it in advance.

Please let me know if there are any mistakes or differences.

1. Creating a library

First of all, create the library you want to publish and publish it on github.

Initially "It must be amazing because it can be used by people all over the world." I thought, but it seems that it is okay to publish any library separately. (Of course, malicious things are not good)

The ones I have published are as follows. https://github.com/yutwoking/randomJapanLocationer It is a simple one that randomly returns the 47 prefecture names and prefecture numbers. ~~ Who will use it! !! !! !! !! ~~

It means that even something as simple as this can be published to the Maven Central Repository. Lol

As an aside, I have never launched a web server by myself and published a web application, so I would like to use this published library to release a simple web application at a later date.

2. Create an account with Sonatype JIRA

In order to publish the library to Maven Central Repository, you need to apply with Sonatype JIRA, so first register an account. Once the application is successful, it can be deployed to sonatype OSSRH and published to the Maven Central Repository. There is nothing difficult to register, just fill in the items. https://issues.sonatype.org/secure/Signup!default.jspa You can also select Japanese in the settings. I'm going fast here.

3. Apply for a project

Next, apply for a project to publish the library. Click Create at the top of the sonatype page to bring up the application form.

スクリーンショット 2019-09-19 10.27.45.png

Fill in each item as follows. Since it is a foreigner who will judge, I will write it in English.

-** Project : Community Support --Open Source Project Repository Hosting (OSSRH) - Issue Type : New Project - Summary : Enter the project name of the library to be published. The point is the name of the library, so give it something that is easy to understand. - Description : Enter a description of the contents of the library. It's not required, so you don't have to enter it. However, if you want a lot of people to use it, you should enter it. I was blank. - Attachment : No need to mess with - GropId : Enter the namespace of the package to upload your library. As an amateur myself got caught here, I will explain later. - Project URL : Enter the URL of the github that published the library - SCMurl : Enter the clone URL of the corresponding github repository. - Username (s) **: If you want to share permissions for the specified Group Id with others, write your Sonatype JIRA account name here. If you are doing it alone, you can leave it blank.

Fill in and press Create to create a project page and move on to review. スクリーンショット 2019-09-19 10.52.53.png

After a while, if there are no problems, the application will pass and you should proceed to the next step. However, the following comments arrive on the project page. スクリーンショット 2019-09-19 11.00.01.png

The application does not pass. .. .. The reason is that ** GroupId is not appropriate **.

When you apply, you don't have to wear it because it's a namespace, right? I had only thought about it, so I wrote "com.yutwoking" on the textbook and applied for it. It was helpful to know how to deal with the comments. ~~ The bot may just be returning a fixed phrase ~~ Thank you for the person in charge \ (^ o ^) /

If you publish it on Github, io.github. {username} or com.github. {username} It seems that you should use the GroupId.

There is an edit button at the top of the project screen, so fix the GroupId there. Then send a comment to report the fix to the person in charge. (Be careful because it seems that the person in charge will not be notified of the correction unless you comment) スクリーンショット 2019-09-19 11.08.53.png And wait for a while. A few hours later, a comment will come from the person in charge. スクリーンショット 2019-09-19 11.12.18.png Apparently, I need proof that it's really my github account. As told, create a repository called OSSRH-51643 and return comments. スクリーンショット 2019-09-19 11.17.13.png I will wait for a while. I received the following comments and the application was completed successfully! スクリーンショット 2019-09-19 11.19.12.png At the end, it says that you should comment after the first release.

4. Create pom.xml for registration with Maven Central Repository

If you use Maven normally, you should have written pom.xml. However, I usually use gradle, so I had a hard time here as well.

For the time being, it seems that you can also register using gradle. https://tagomoris.hatenablog.com/entry/2016/02/16/114226

However, since it will be released to Maven Central, I would like to try it with Maven. So, write pom.xml.

Click here for the pom.xml I wrote https://github.com/yutwoking/randomJapanLocationer/blob/master/pom.xml

Click here for the official pom.xml https://github.com/simpligility/ossrh-demo/blob/master/pom.xml

If this pom.xml is flawed, you will get an error when releasing. If you compare the formula with what I wrote, it will be easier to understand where to edit. I stumbled here many times (crying) I will write the stumbling point in the release item.

5. GPG key setup

Key preparation

It seems that a signature with a GPG key is required to publish the library. Since I am a mac, install the following software first. https://gpgtools.org/

Next, open a terminal and execute the following command. (If it doesn't work, restart the terminal or try gpg2 → gpg.)

First, generate the key. After entering the command, proceed according to the instructions OK

$ gpg2 --gen-key

Check if the key can be generated with the following command

$ gpg2 --list-keys

/Users/Ehara/.gnupg/pubring.kbx
-------------------------------
pub   rsa2048 2019-09-18 [SC] [expiration date: 2021-09-17]
      BDCD77306BCE8473BBFC69521697094FBD6EAK76
uid           [ultimate] Yuto Ehara <****@gmail.com>
sub   rsa2048 2019-09-18 [E] [expiration date: 2021-09-17]

If the key has been created, send the public key to the server with the following command. For the value of --send-keys, enter the value of the public key confirmed in ↑.

$ gpg2 --keyserver hkp://pool.sks-keyservers.net --send-keys BDCD77306BCE8473BBFC69521697094FBD6EAK76

Maven settings for using keys

You need to put a file called settings.xml locally so that you can deploy it to Sonatype OSSRH. It seems that the directory to be read is different for each user. As far as the literature is concerned, it is often read by placing it in ~ / .m2 / settings.xml. I created it here too and it worked.

The contents of settings.xml are as follows.

settings.xml


<settings>
  <servers>
    <server>
      <id>ossrh</id>
      <username>YOUR_JIRA_USERNAME</username>
      <password>YOUR_JIRA_PASSWORD</password>
    </server>
  </servers>
  <profiles>
    <profile>
      <id>ossrh</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <gpg.executable>gpg2</gpg.executable>
        <gpg.passphrase>GPG_KEY_PASSPHRASE</gpg.passphrase>
      </properties>
    </profile>
  </profiles>
</settings>

YOUR_JIRA_USERNAME and YOUR_JIRA_PASSWORD are yours when you created your JIRA account. GPG_KEY_PASSPHRASE is the one you set when you created the key. Now it will call the file when you start maven.

6. Deploy to Sonatype OSSRH

Execute the deployment with the following command.

$ mvn clean deploy -DperformRelease=true

If there is no problem, it should say BUILD SUCCESS. If it does not appear, see the error message and correct it.

In my case, I got an error due to the following mistake, so please refer to it if you get an error.

--The java version of the compilation is incorrect. → Change the \ tag and \ tag in the following part of pom.xml appropriately.

<plugin>
	<artifactId>maven-compiler-plugin</artifactId>
	<configuration>
		<source>12</source>
		<target>12</target>
		<encoding>UTF-8</encoding>
	</configuration>
</plugin>

--Plugin version → If the reference is old, the version of the plug-in may be old and may not work well. https://mvnrepository.com/repos/central
It is good to check with and rewrite pom.xml to the latest version.

7. Released on Sonatype OSSRH

Once the deployment is complete, it's time to release.

First, log in to sonatype OSSRH. The username and pass at login are for your JIRA account. https://oss.sonatype.org/

After logging in, click Build Promotion> Staging Repositories on the left side of the screen to display a list of staging repositories. If deploy works, you should have yours here. スクリーンショット 2019-09-19 16.12.35.png Select yours and close. When you close it, various checks will run, so wait for a while. If close works, all you have to do is press the Release button. If close fails, drop the repository with the error, fix the error, and then try deploying again.

I was covered with errors, so here are the errors I experienced and fixed.

Public key not found error

If you register the public key on the server and it doesn't take much time, an error will occur due to synchronization. After waiting a few hours and trying again, the error was resolved.

reference https://codeday.me/jp/qa/20190129/197488.html

pom validation error

This is an error caused by missing tag in pom.xml. Deploy again, including the following tags (and all the child tags for these tags):

Also, note that an error will occur unless the name tag and artifactId tag are the same.

Validation error other than pom

Put the appropriate plugin in pom.xml. Example) In case of javadoc validation error, insert maven-javadoc-plugin.

reference https://web-dev.hatenablog.com/entry/maven/central/nexus-close-error

If you can close it safely

All you have to do is press Release \ (^ o ^) / It will take a few hours for it to be reflected in the Maven Central Repository, so wait.

8. Confirm that it was published

http://search.maven.org/ You can find your published repository by searching for gropId in! スクリーンショット 2019-09-19 16.37.53.png

Impressions I tried

At the first time, various procedures were troublesome and it took a long time, but from the next time onward, it seems to be possible. I will try the release work with gradle next time.

Please let me know if there is any mistake m (_ _) m

Recommended Posts

A new engineer tried to publish his own library to Maven Central Repository
Procedure to publish to Maven Central Repository
How to create a Maven repository for 2020
How to publish a library in jCenter
I tried setting up a Maven remote repository
I tried using the GitHub repository as a library server