• 周四. 10 月 3rd, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Remember a non Maven project published to the central warehouse crawling over the pit!

King Wang

1 月 3, 2022

Today, a friend chatted with me and said that the author has not written an article for two days , What did they do ? I replied helplessly that I was maven It’s autistic . What kind of problem did it take me so long to solve it ? This article tells you .

One 、 What I want to do

The writer has been looking at Hotspot Provided tools Package source code , And tried to transform and expand , Finally, Xiaoyou accomplished the first version of virtual machine monitoring API:vmconsole, This is not the point , The point is at the back . I beat it to jar package , And want to share , Let other developers use , The first thing I think about is Maven, If I post it to Maven The warehouse is OK ? So I began to climb the pit road .

I’m here to make a pre advertisement for my work , Welcome the majority of programming enthusiasts to use and provide comments and suggestions , At the same time, I hope to be encouraged by your little star O(∩_∩)O, There will also be a special article to introduce the work .

JVM Virtual machine monitoring API:VmConsole-Api

  • Github:https://github.com/tzfun/VmConsole-Api
  • API Document:doc.vmconsole.beifengtz.com

Two 、 Problems encountered

This is the first time the author has published his project to Maven Warehouse , The steps are not clear , So I’m all kinds of Google Baidu , If you can’t solve the problem, ask someone else for help , Simple problems are solved successfully , But the most critical problem is finally solved by ourselves , Next, I’ll throw out the problems I’ve encountered , The answer and the solution will be given later .

  • To which Maven Warehouse ?
    Maven The information and configuration of the project is provided by pom.xml To store , It contains project information 、 Developer Information 、 rely on 、 Plug-ins, etc. , If you want to get something developed by someone else or an organization jar package , Then we should introduce what we are familiar with rely on 了 , Dependencies need to be downloaded from the repository , The download process is as follows :
  1. Search the local warehouse , If there is a dependency, return success , Otherwise, go to the next step ;
  2. Search private warehouses , If there is a dependency, return success , Otherwise, go to the next step ;
  3. Search the central warehouse , If there is a dependency, return success , Otherwise return to failure .

The local repository is on every developer’s computer , Generally in c:/user/.m File directory ; Private warehouses are warehouses built by individuals or organizations , It needs to be on the whole settings.xml Configure the warehouse address in ; The central warehouse is the whole thing Maven The core warehouse of storage , All the resources are available for developers . Then the problem is solved , In order to make it easy for developers to get the dependency , I need to put jar The package is released to the central warehouse .

  • How to make a non Maven Project release to central warehouse ?

This problem is the main problem encountered by the author , My project is not Maven project , A lot of online tutorials are all Maven project How to publish to central warehouse , If mine is Maven The project is not as busy as this , My project is right and wrong Maven project , A pure Java Of , It also includes jdk Of jar package , It is because jdk These are provided jar Wrapped in Maven There is no , So I can’t change my project to Maven project , Many of these classes have to rely on these jar package . So the question now is How to pack up already jar The package is released to the central warehouse .

I’ll give you a brief account of what I’ve done , The complete steps are given below , This is the only way that I have thought of so far , If the reader has a better way , I hope you can contact me , I also learn to learn . It still needs to be transferred to the central warehouse Maven project , So I built an empty one Maven project , And then jar The package is installed in the local warehouse first , In this empty Maven Introduce this jar Local dependence on , And then pom Middle configuration assembly The plug-in merges dependency packages into a single jar After Bao ran released ( The inspiration comes from Springboot, because Springboot Of Maven This plug-in is configured in the project ), Finally, publish the whole project to nexus in ( Warehouse Management Center ), When introducing dependencies, you can’t simply introduce this project , It’s the one that was incorporated into the project jar, Configure… In dependencies classifier that will do .

3、 … and 、 Complete steps

Next I’ll post my complete steps , The code part is a bit long , The purpose of posting code is to make it convenient for people who encounter the same situation to paste and copy the content , The reader may skip , Just look at words or pictures .

1. register sonatype Account and create issue

The first thing you need to do is https://issues.sonatype.org/secure/Dashboard.jspa Registered account ,Sonatype adopt JIRA To manage OSSRH Warehouse .

Need to fill in Email, Full Name, Username as well as password, among Username And Password The following steps are required , Please write down , You need to use .

Create a issue

 Insert picture description here

  • Project: Project defaults , My choice is :Community Support – Open Source Project Repository Hosting (OSSRH)
  • IssueType: The default value is x, My choice is :New Project
  • Summary: explain , Please introduce the project
  • GroupId: project id, According to your domain name , The first time I made a mistake here , Such as my github yes github.com/tzfun, that groupId Namely com.github.tzfun.
  • ProjectURL: Visit the project URL, What I’m writing is github The address of this project in .
  • SCMurl: Visit the project URL, What I’m writing is github Warehouse address .
  • Fill in the rest according to the prompts

 Insert picture description here

When you’re done, submit , In the middle, you may have filled in issue There will be problems. , Need to communicate with customer service , Finally, the following reply shows that it has been successfully created . Now you can go to nexus Project uploaded in

 Insert picture description here

2. Install and configure GPG

Publish to Maven All files in the repository are to be used GPG Signature , To ensure integrity . without GPG The previous project cannot be published to the central warehouse , In the rear close Stage Will be stopped .

IOS Please download the system GPG Suite:https://gpgtools.org/

Windows Please download the system gpg4win:https://www.gpg4win.org/download.html

Once installed, you can generate the secret key through the graphical interface , It can also be generated from the command line , Graphical interface operation is simple and easy to understand, do not demonstrate , Here’s a demonstration of the command line :

  1. Generate key pair :gpg --gen-key, This process will let you enter your user name and password , Just fill in the user name , Please remember the password , Because the password will be entered every time the deployment is released ;
  2. View public key id:gpg --list-keys, The next step is to use ;
  3. Public key id Publish to GPG Secret key server :gpg--keyserver hkp://keyserver.ubuntu.com:11371 --send-keys xxxxxxxxxxxxxxxxxxxx, Suppose my public key id yes xxxxxxxxxxxxxxxxxxxx;
  4. Check whether the sending is successful :gpg --keyserverhkp://pool.sks-keyservers.net --recv-keys xxxxxxxxxxxxxxxxxxxx, The release is successful. This step is completed .

3.Maven Global configuration

stay Maven Go out and look for settings.xml The configuration file is in apache-maven-3.5.3\conf Under the table of contents , Open it and find servers label , Add a service , there id Used to uniquely identify server, It will be used in later configuration and deployment , The user name and password you registered in the first step sonatype Account and password . Because it’s clear text , So please take care of it ! It’s not a problem for someone else to get rid of it .

<servers>
<server>
<id>oss</id>
<username>Fundebug</username>
<password>passsword</password>
</server>
</servers>

4. project pom.xml To configure

My approach is different from a lot of online tutorials in this step , The most critical configuration is also in this step .

If you are one Maven project , So just need to be in the project you’re writing pom.xml Just configure it ,Maven The dependency will not be packaged when the package is uploaded , Just package your project code and upload it , These dependencies are all through your pom File to record , After others introduce your dependence Maven According to you pom To download the relevant package . So in the final analysis, deploy this step You just package your project as jar And upload it to the warehouse , Dependent jar It’s not going to be uploaded !

If you are a pure Java project , There are some dependencies, only class Bytecode files or whatever jar package ( The library you need is in Maven Can’t find… In the warehouse , But there are related bytecode files or jar package ), Can’t change your project to Maven project , It is the same as the author , The specific operation is as follows .

  1. Create a new Maven project , There’s no need to write any code , You don’t need to create any classes , Just configure pom Project information in 、 The author information 、 Open source protocol, etc .
<!-- organization Id、 project Id、 Version number -->
<groupId>com.github.tzfun</groupId>
<artifactId>vmconsole</artifactId>
<version>1.0.0</version>
<!-- Project description -->
<name>vmconsole</name>
<url>https://github.com/tzfun/VmConsole-Api</url>
<description>An API that makes it easy to monitor virtual machines</description>
<!-- Open source licenses -->
<licenses>
<license>
<name>Apache 2</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
<comments>A business-friendly OSS license</comments>
</license>
</licenses>
<!-- Project address , Here I fill in github -->
<scm>
<url>https://github.com/tzfun/VmConsole-Api</url>
<connection>https://github.com/tzfun/VmConsole-Api.git</connection>
</scm>
<!-- Coding format -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- Developer Information -->
<developers>
<developer>
<name>beifengtz</name>
<id>beifengtz</id>
<email>[email protected]</email>
<roles>
<role>Developer</role>
</roles>
<timezone>+8</timezone>
</developer>
</developers>
  1. Install your dependencies into the local repository :

    1. If you have some class Bytecode files need to be typed jar package , It’s simple , Use any compression tool , Compress your dependency projects into zip Format , Note that it must be zip, because zip Compression format and jar The format is the same , After packing, change the suffix name to jar that will do .

    2. If you’re just some jar package , No, “ In bulk ” Of class Bytecode file , Then you can go directly to the next step .

    3. use Maven The order is coming jar Package installation to local warehouse , The order is as follows :

    mvn install:install-file -Dfile=D:\vmconsoel-api.jar -DgroupId=com.github.tzfun -DartifactId=vmconsole-local -Dversion=1.0.0 -Dpackaging=jar
    
    • – DgroupId and DartifactId Constitute the jar Wrapped in pom.xml Coordinates of , Corresponding to dependent DgroupId and DartifactId
    • – Dfile Indicates that you need to upload jar The absolute path of the package
    • – Dversion Version number
    • – Dpackaging For the type of installation file , Here is jar
  2. stay pom Add a local warehouse dependency to

After the successful implementation, the local warehouse will have just that project , Next, add this dependency to pom In file , there groupId、artifactId as well as version Consistent with the content of the above command .

<dependencies>
<dependency>
<groupId>com.github.tzfun</groupId>
<artifactId>vmconsole-local</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
  1. Configure the release plug-in and repository address .

Generally, only three plug-ins are needed , But here I add one more assembly plug-in unit . If you are Maven project , There is no need to package and upload many dependencies together ( adopt pom Can download dependency , There’s no need to put the documents in the warehouse ), So you can’t do without this plug-in , Annotate the plug-in . If you’re like me, it’s not Maven project , Then you need to add this plug-in , Its role is to put all the dependencies jar The packages merge into one jar package .

Because this part of the content is more, so I will my whole pom.xml Post it together , Readers who don’t care here can skip to the following .

<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>
<!-- organization Id、 project Id、 Version number -->
<groupId>com.github.tzfun</groupId>
<artifactId>vmconsole</artifactId>
<version>1.0.0</version>
<!-- Project description -->
<name>vmconsole</name>
<url>https://github.com/tzfun/VmConsole-Api</url>
<description>An API that makes it easy to monitor virtual machines</description>
<!-- Open source licenses -->
<licenses>
<license>
<name>Apache 2</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
<comments>A business-friendly OSS license</comments>
</license>
</licenses>
<!-- Project address , Here I fill in github -->
<scm>
<url>https://github.com/tzfun/VmConsole-Api</url>
<connection>https://github.com/tzfun/VmConsole-Api.git</connection>
</scm>
<!-- Coding format -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- Developer Information -->
<developers>
<developer>
<name>beifengtz</name>
<id>beifengtz</id>
<email>[email protected]</email>
<roles>
<role>Developer</role>
</roles>
<timezone>+8</timezone>
</developer>
</developers>
<!-- rely on -->
<dependencies>
<dependency>
<groupId>com.github.tzfun</groupId>
<artifactId>vmconsole-local</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>release</id> <!-- Deployment needs to use -->
<build>
<plugins>
<!-- Package other dependencies into a jar Plug in for -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Javadoc -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- GPG -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<distributionManagement>
<snapshotRepository>
<id>oss</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>oss</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
</profile>
</profiles>
</project>

5. Publish to nexus

Next stay pom.xml Under the table of contents Carry out orders :

mvn clean deploy -P release

among release It’s not an order , It’s your pom Configured in the file profile id. stay build You need to enter gpg Password , Enter the first 2 Step Install and configure GPG When the password can be .

6. Go to nexus management

Log in to the warehouse management website nexus:https://oss.sonatype.org/#stagingRepositories, The user name and password here is the first step sonatype Account and password .

 Insert picture description here

Select the item you uploaded , And then first close, After about half a minute, if it works Release The button will light up , Click again Release button . Be careful close and Release All processes have corresponding verification rules , All must be met to pass , There is no problem following the above steps , If in the first place 4、5 Ministry use deploy-file Order to upload jar So close The process can fail , because close In the process javadoc、source、gpg One of the three is indispensable .

If Release Waiting after success is almost 2 You can search your projects in the central warehouse within hours , Address :https://search.maven.org/

 Insert picture description here

7. Add dependencies to your project

If you were Maven Project, then the dependency code provided by the warehouse can be successfully introduced . If you’re like me, you’ve been wrong from the beginning Maven project , So the dependency you search for is really your project , But the introduction doesn’t work , Because there is no choice classifier, Go into your project and look at the content of the project, and you can see that there are several jar package , You need to choose which one to introduce , For example, here is the following code .

<dependency>
<groupId>com.github.tzfun</groupId>
<artifactId>vmconsole</artifactId>
<version>1.0.3</version>
<classifier>jar-with-dependencies</classifier>
</dependency>

 Insert picture description here

Four 、 summary

It’s hard to control a tool without understanding how it works , And I didn’t know this time Maven In the case of the release process, we will start to move from place to place , So a lot of time was wasted . In the middle, I tried every possible operation , such as deploy-file、 Decompile the whole jar Package upload 、 To other private clouds (aliyun)、 Command line simulation pom wait , In the end, we have to understand its operation mechanism , The author has read in detail Maven And then we learned about Maven Several life cycles of , Finally, with the plug-in writing method of other frameworks to solve this problem . Once again verified a sentence : A swordsman without an internal skill foundation can’t practice a good sword . Programming best reflects the importance of Foundation , Only by understanding the operating mechanism of a computer or language can we integrate it .


The author also established his own official account. , I usually share some programming knowledge , Welcome everyone to support ~

Scan code or wechat search north wind IT The way Focus on

发表回复