In general, if we develop a tool component, we want to release it for others to use. Internally, we can publish it to a private repository, and in an Internet environment, we generally publish it to a Maven central repository. Here’s how to publish a component to Maven’s central repository, using our recently developed Java tool flyRafter.
First of all, we need to make sure that the components compile correctly as JAR packages. With that in mind, we need to pre-configure our project’s POM.xml for release.
- Configure basic project information
<groupId>fun.mortnon</groupId>
<artifactId>flyrafter</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<url>https://gitee.com/mortise-and-tenon/flyrafter</url>
<description>generate sql file from java entity class.</description>
Copy the code
Here configure the project’s most important groupId, artifactId, name, etc.
If you do not have a domain name, you can also use the domain name of your hosting environment as groupId. For example, if your project is on Github and your user name is ME, your groupId could be com.github.
- Open source protocol for components
<licenses>
<license>
<name>MIT License</name>
<url>https://opensource.org/licenses/MIT</url>
<distribution>repo</distribution>
</license>
</licenses>
Copy the code
Whatever protocol you use, configure it accordingly.
- Project source address and developer information
<scm>
<url>http://mortnon.fun</url>
<connection>scm:git:https://gitee.com/mortise-and-tenon/flyrafter.git</connection>
<developerConnection>scm:git:https://gitee.com/mortise-and-tenon/flyrafter.git</developerConnection>
</scm>
<developers>
<developer>
<name>mortnon</name>
<email>[email protected]</email>
<organization>Mortise and mortise - and - tenon</organization>
<organizationUrl>https://mortnon.fun</organizationUrl>
</developer>
</developers>
Copy the code
Fill in your project address, hosted SCM address, and developer profile here.
- Publish repository configuration
<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>ossrh</id> <url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
Copy the code
The warehouse address configured here is the latest address of the central database, and new projects will be submitted here.
Note that the id value ossrh is the same as the setting. XML value configured in maven. It is easiest to keep osSRh consistent with the official instructions.
- The plug-in configuration
<build>
<plugins>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.7</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
<configuration>
<additionalJOptions>
<additionalJOption>-Xdoclint:none</additionalJOption>
</additionalJOptions>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Copy the code
The above plug-ins are required to publish to the central library and are not directly related to the project compilation itself. Because source and doc compilation is involved, local debugging will increase compilation time. Therefore, it is recommended to configure these publish-related plug-ins separately through the profile and put them in the published profile, for example:
<profiles>.<profile>
<id>release</id>
<build>
<plugins>.</plugins>
</build>
</profile>
</profiles>
Copy the code
This is an introduction to configuring the project’s POM.xml. The following describes how to configure setting. XML for maven. Open the local Maven tool directory and go to the conf directory to see the setting.xml file. Open this file and add as follows:
- to
profiles
Node Adding Content
<profile>
<id>ossrh</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<gpg.executable>gpg</gpg.executable>
<gpg.passphrase>the_pass_phrase</gpg.passphrase>
</properties>
</profile>
Copy the code
Passphrase represents the password of your private key. You do not need to enter the password, but you will usually be asked to enter it in a popup window.
- to
servers
Node Adding Content
<server>
<id>ossrh</id>
<username>NAME</username>
<password>PWD</password>
</server>
Copy the code
Note that the username and password here are the username and password of the central library sonatype, which will be described later. The registered address is Sonatype
The above release plug-in and Maven setting.xml are all related to GPG configuration. Since GPG is used for signature verification after compilation and before release, we need to generate public and private keys using this tool first. This tool is very simple on Linux and will not be discussed here, only on Windows.
Let’s go to gpg4win to download the installation package of Windows environment. On the download page, you can choose whether to use paypal to donate. If you have no money, you can choose to donate 0 yuan and download directly. After successful download, double-click the installation program, as prompted to continue to install.
Once installed, open gpG4win (named Kleopatra, with an image of a woman’s head), If the following information is displayed, Kleopatra cannot be run as administrator without breaking file Permissions in the GnuPG data Folder. In this case, you can open the GPG application by clicking GPA, another shortcut icon generated during installation, and then clicking Keyring on the right of the icon to open the build screen.
Click on GPA interface to generate a new key, follow the prompts step by step, pay attention to remember your own key password. After the key is generated, right-click the key and choose Send Keys to Send the public key to the server.
After all the previous configurations and operations are complete, the local configuration is complete. Enter sonatype for account registration.
Go to Sonatype and register an account. Note that the password is lowercase and special characters. Once registered, click “New” on the top, then create an issue, select Community Support-Open Source Project Repository Hosting (OSSRH), The problem type is New Project.
-
Enter your project name or a description.
-
Group Id Enter the Group Id of your project, noting the rules mentioned above (either use your own domain name or the domain name of the hosting site, the official document Group Id).
-
Project URL Enter your Project web site address
-
SCM URL Enter the Git download address for your project
-
Leave other items blank by default
Finally, click New, submit a question, and wait for the official response to the instructions.
The official will prompt you to confirm whether you have the corresponding domain name ownership, as long as the DNS resolution of the domain name, add a TXT record, fill in the content as their own list URL, if it is the name of the hosting website, such as com.github. XXX, the official will directly check.
After DNS resolution is added, click Remarks to add a note to tell DNS resolution is added.
After the official review, there will be an instruction manual for uploading components that will show you how to add the configuration to POM.xml that we described above. Just go ahead and publish, by going back to setting.xml above and adding your Own Sonatype account and password.
Execute the MVN publish command MVN clean deploy on the project from the local command line to publish the components to the central repository stage library after successful compilation. Also, if the artifact ID does not end with -snapshot, the MVN’s stage plug-in will automatically publish the components of the stage library to the public repository.
Note that if pom. XML uses a profile to distinguish development from publishing, you need to specify the profile name in the publishing command, for example, MVN clean deploy-prelease. You can add more parameters according to the instructions of the MVN command
Once published, we can search the Repository for published components at Nexus Repository Manager, and manage them there if they are in the Stage library. You can view it directly from [Central Repository: maven.org], but you need to wait 2 hours to view it from Maven Central Repository Search.
This is how the entire component is published to the central repository. In the example above, we released the first preview of flyRafter, which is a tool for Java to automatically generate table building SQL files for the @Entity annotation Entity class. It can also be used in conjunction with Flyway to achieve this effect:
Dcl-auto: Update (dCL-AUTO: UPDATE) (DCL-AUTO: Update) (DCL-Auto: Update) (DCL-Auto: Update) (DCL-Auto: Update) (DCL-Auto: Update) (DCL-Auto: Update) (DCL-Auto: Update)
Maven FlyRafter: Entity to SQL, combined with the Flyway application tool (gitee.com), maven FlyRafter: This tool is being improved, please try it out and submit an issue.
Welcome to my blog: The Border city of A-woo
Welcome to pay attention to my public number: A woo programming