: Notebook: This article is filed under “blog”

Publish jar packages to a central repository

To avoid reinventing the wheel, I’m sure every Java programmer wants to build his own scaffolding or toolkit (custom is often the best one for him). So how to publish your own scaffolding to the central warehouse? We’ll do it step by step.

Create an Issue in Sonatype

(1) Register Sonatype account

To publish Java packages to Maven’s central repository, you first need to create a work order on the Sonatype website.

You need to register your account when using the site for the first time (remember this account and password for later use).

(2) Create an Issue

After registering the account successfully, write Summary, Description, Group Id, SCM URL, Project URL and other necessary information according to the function of your Java package. Please refer to the Issue I created earlier: OSSRH-36187.

Once created, Sonatype staff will have to wait for approval, which can be quite quick, almost two hours for my approval. When the Status of the Issue changes to RESOLVED, the next step can be taken.

Note: If your Group Id is listed as your own website (as was the case with mine), Sonatype staff will ask you if that Group Id is your domain name. All you have to do is say yes and you’ll be approved.

Generate a public and private key pair using GPG

(1) Install Gpg4win

In Windows, you can download the Gpg4win software to generate a key pair.

Gpg4win download address

Run the GPG –version command to check whether the installation is successful.

C: Program Files (x86)\GnuPG\bin> GPG --version GPG (GnuPG) 2.2.10 Libgcrypt 1.8.3 Copyright (C) 2018 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the exdunwu permitted by law. Home: C:/Users/Administrator/AppData/Roaming/gnupg Supported algorithms: Pubkey: RSA, ELG, DSA, ECDH, ECDSA, EDDSA Cipher: IDEA, 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH, CAMELLIA128, CAMELLIA192, CAMELLIA256 Hash: SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224 Compression: Uncompressed, ZIP, ZLIB, BZIP2Copy the code

(2) Generate a key pair

Run the GPG –gen-key command

C: Program Files (x86)\GnuPG\bin> GPG --gen-key GPG (GnuPG) 2.2.10; Copyright (C) 2018 Free Software Foundation, Inc. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the exdunwu permitted by law. Note: Use"gpg --full-generate-key" for a full featured key generation dialog.

GnuPG needs to construct a user ID to identify your key.

Real name: Zhang Peng
Email address: [email protected]
You selected this USER-ID:
    "Zhang Peng <[email protected]>"

Change (N)ame, (E)mail, or (O)kay/(Q)uit? O
Copy the code

Note: Enter the user name and email address as prompted.

(3) View the public key

C:\Program Files (x86) \GnuPG\bin>gpg --list-keys

gpg: checking the trustdb
gpg: marginals needed: 3  completes needed: 1.trust model: pgp
gpg: depth: 0  valid:   2  signed:   0  trust: 0, 0q. 0n. 0m. 0f2,u
gpg: next trustdb check due at 2020-11-05
C: /Users/Administrator/AppData/Roaming/gnupg/pubring.kbx
--------------------------------------------------------
pub   rsa2048The 2018-11-06 [SC] [expires: the 2020-11-06]E4CE537A3803D49C35332221A306519BAFF57F60
uid           [ultimate] forbreak <forbreak@ 163.com>
sub   rsa2048The 2018-11-06 [E] [expires: the 2020-11-06]Copy the code

Description: among them, E4CE537A3803D49C35332221A306519BAFF57F60 is public key

(4) Publish the public key to the PGP key server

GPG –keyserver hkp://pool.sks-keyservers.net –send-keys

C:\Program Files (x86)\GnuPG\bin>gpg --keyserver hkp://pool.sks-keyservers.net --send-keys E4CE537A3803D49C35332221A306519BAFF57F60
gpg: sending key A306519BAFF57F60 to hkp://pool.sks-keyservers.net
Copy the code

Note: GPG: keyServer receive failed: No DAT error may occur. Wait about 30 minutes and run the command again.

(5) Check whether the public key is published successfully

Run GPG –keyserver hkp://pool.sks-keyservers.net –recv-keys to check whether the public key is published successfully.

C:\Program Files (x86)\GnuPG\bin>gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys E4CE537A3803D49C35332221A306519BAFF57F60
gpg: key A306519BAFF57F60: "forbreak <[email protected]>" not changed
gpg: Total number processed: 1
gpg:              unchanged: 1
Copy the code

Maven configuration

After completing the preparations for the first two chapters, you are ready to upload the JAR package to the central repository. Of course, we also need to do some configuration for Maven.

Settings. The XML configuration

An example of a complete settings.xml configuration is as follows:

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

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

  <pluginGroups>
    <pluginGroup>org.sonatype.plugins</pluginGroup>
  </pluginGroups>

  <! -- User name and password are Sonatype accounts and passwords -->
  <servers>
    <server>
      <id>sonatype-snapshots</id>
      <username>xxxxxx</username>
      <password>xxxxxx</password>
    </server>
    <server>
      <id>sonatype-staging</id>
      <username>xxxxxx</username>
      <password>xxxxxx</password>
    </server>
  </servers>

  <! Use Maven repository aliyun -->
  <mirrors>
    <mirror>
      <id>nexus-aliyun</id>
      <mirrorOf>*</mirrorOf>
      <name>Aliyun</name>
      <url>http://maven.aliyun.com/nexus/condunwu/groups/public</url>
    </mirror>
  </mirrors>

  <! -- GPG password, note, not public key -->
  <profiles>
    <profile>
      <id>sonatype</id>
      <properties>
        <gpg.executable>C:/Program Files (x86)/GnuPG/bin/gpg.exe</gpg.executable>
        <gpg.passphrase>xxxxxx</gpg.passphrase>
      </properties>
    </profile>
  </profiles>

  <activeProfiles>
    <activeProfile>sonatype</activeProfile>
  </activeProfiles>
</settings>
Copy the code

Pom. The XML configuration

(1) Add licenses, SCM, developers configuration:

<licenses>
  <license>
    <name>The Apache Software License, Version 2.0</name>
    <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
    <distribution>repo</distribution>
  </license>
</licenses>

<developers>
  <developer>
    <name>xxxxxx</name>
    <email>[email protected]</email>
    <url>https://github.com/dunwu</url>
  </developer>
</developers>

<scm>
  <url>https://github.com/dunwu/dunwu</url>
  <connection>[email protected]:dunwu/dunwu.git</connection>
  <developerConnection>https://github.com/dunwu</developerConnection>
</scm>
Copy the code

(2) Add the distributionManagement configuration

<distributionManagement>
  <snapshotRepository>
    <id>sonatype-snapshots</id>
    <url>https://oss.sonatype.org/content/repositories/snapshots</url>
  </snapshotRepository>
  <repository>
    <id>sonatype-staging</id>
    <url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
  </repository>
</distributionManagement>
Copy the code


specifies the snapshot repository address;

specifies a staging (formal) repository address. Note that the ID needs to be the same as the id of

in settings.xml.


(3) Add profiles configuration

 <profiles>
  <profile>
    <id>sonatype</id>
    <build>
      <plugins>
        <plugin>
          <groupId>org.sonatype.plugins</groupId>
          <artifactId>nexus-staging-maven-plugin</artifactId>
          <version>1.6.7</version>
          <extensions>true</extensions>
          <configuration>
            <serverId>sonatype-snapshots</serverId>
            <nexusUrl>https://oss.sonatype.org/</nexusUrl>
            <autoReleaseAfterClose>true</autoReleaseAfterClose>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-javadoc-plugin</artifactId>
          <version>3.0.1</version>
          <configuration>
            <failOnError>false</failOnError>
            <quiet>true</quiet>
          </configuration>
          <executions>
            <execution>
              <id>attach-javadocs</id>
              <goals>
                <goal>jar</goal>
              </goals>
            </execution>
          </executions>
        </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>
  </profile>
</profiles>
Copy the code

Deployment and release

After following the above steps, everything is OK.

At this point, you can publish jar packages to the central repository using the MVN clean deploy -p sonatype command:

Note: To activate a profile, sonatype following the -p parameter must be the same as the ID in POM. XML.

Deploy maven private server

At work, Java programmers develop commercial Java projects that don’t want to be released to a central repository, so everyone knows about them. At this point, we need to set up a private server and deploy the Maven server on the company’s internal network to prevent the JAR package from spreading out. So how do we do that? Let’s learn step by step.

Download and install Nexus

Enter the official download address and select the appropriate version to download.

I wanted to deploy the Nexus on a Linux machine, so I chose the Unix version.

Here, if you want to download it directly by command (such as script installation), you can find the appropriate version on the official history release page and run the following command:

Wget - O/opt/maven/nexus - Unix. Tar. Gz tar ZXF - http://download.sonatype.com/nexus/3/nexus-3.13.0-01-unix.tar.gz nexus-unix.tar.gzCopy the code

After decompression, there are two directories:

  • Nexus-3.13.0-01 – Contains the files required to run the Nexus. Is required for the Nexus to run.
  • Sonatyp-work – contains configuration files, log files, repository files, and more generated by the Nexus. This is the default directory to back up the Nexus.

Start and stop Nexus

Go to the nexus-3.13.0-01/bin directory and there is an executable script nexus.

Run the./nexus command to view the parameters that are allowed to be executed, as shown below:

$ ./nexus
Usage: ./nexus {start|stop|run|run-redirect|status|restart|force-reload}
Copy the code
  • Start the nexus –./nexus start
  • Stop the nexus –

After the startup is successful, visit http://< IP >:8081 in the browser. The welcome page is as follows:

Click Sign in on the upper right corner to log in. The default user name/password is admin/admin123.

It is important to mention that the Management page for Nexus Repositories shows the available Maven repository, as shown below:

Description:

  • Maven-central – Maven central library (if no mirror is configured, jar packages are downloaded from this library by default), and obtain resources from repo1.maven.org/maven2/
  • Maven-releases – Releases jar packages that store private repositories
  • Maven-snapshots – Snapshots (debug version) jars that store private repositories
  • Maven-public – Private repository public space, combining the above three repositories to provide external services, used in the local Maven base configuration settings.xml.

Use the Nexus

If you want to use the Nexus, you must also configure authentication information in settings. XML and pom.xml.

The configuration Settings. The XML

A complete settings.xml:

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

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <pluginGroups>
    <pluginGroup>org.sonatype.plugins</pluginGroup>
  </pluginGroups>

  <! -- Maven private server account info -->
  <servers>
    <server>
      <id>releases</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
    <server>
      <id>snapshots</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
  </servers>

  <! -- Jar package download address -->
  <mirrors>
    <mirror>
      <id>public</id>
      <mirrorOf>*</mirrorOf>
      <url>http://10.255.255.224:8081/repository/maven-public/</url>
    </mirror>
  </mirrors>

  <profiles>
    <profile>
      <id>zp</id>
      <repositories>
        <repository>
          <id>central</id>
          <url>http://central</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <id>central</id>
          <url>http://central</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
  </profiles>

  <activeProfiles>
    <activeProfile>zp</activeProfile>
  </activeProfiles>
</settings>
Copy the code

Configuration of pom. The XML

Add the following configuration to pom.xml:

  <distributionManagement>
    <repository>
      <id>releases</id>
      <name>Releases</name>
      <url>http://10.255.255.224:8081/repository/maven-releases</url>
    </repository>
    <snapshotRepository>
      <id>snapshots</id>
      <name>Snapshot</name>
      <url>http://10.255.255.224:8081/repository/maven-snapshots</url>
    </snapshotRepository>
  </distributionManagement>
Copy the code

Note:

  • <repository><snapshotRepository>The id of thesettings.xmlIn the configuration file<server>The ID in the tag matches.
  • <url>The address of the tag must match the address of maven private server.

Implementing maven Builds

To use the private service configuration in settings.xml, you must activate the profile by specifying -p zp.

Example:

## Build and package the Maven project
$ mvn clean package -Dmaven.skip.test=true -P zp

## Build and upload maven deliverables (JAR packages)
$ mvn clean deploy -Dmaven.skip.test=true -P zp
Copy the code

The resources

  • www.jianshu.com/p/8c3d7fb09…
  • www.ruanyifeng.com/blog/2013/0…
  • www.cnblogs.com/hoobey/p/61…
  • Blog.csdn.net/wzygis/arti…
  • Blog.csdn.net/clj19860606…