maven 私有库,开源库_如何将开源Java库上载到Maven Central

maven 私有库,开源库

by Seun Matt

通过Seun Matt

如何将开源Java库上载到Maven Central (How to Upload an Open-Source Java Library to Maven Central)

This article is a comprehensive guide, from start to finish, on how to deploy a Java library to Maven Central so everyone can use it by including the dependency in their project(s).

本文是从头到尾的全面指南,内容涉及如何将Java库部署到Maven Central,以便每个人都可以通过在项目中包含依赖项来使用它。

It goes without saying that there has to be a Java library for it to be uploaded. Hence, the first thing is creating a Java library that’s unique, of quality code standard, and that will be beneficial to the developer community. Of course, you’ve got that already — that’s why you’re reading this after all (or maybe you’re planning on creating one).

不用说,必须有一个Java库才能上载它。 因此,第一件事就是创建一个唯一的,具有高质量代码标准的Java库,这将对开发人员社区有利。 当然,您已经知道了-这就是为什么您毕竟要阅读此内容(或者也许您打算创建一个)

In summary, to upload a shiny new Java library to Maven Central, we’ll have to reserve our Group ID, provide the required details in the project’s pom.xml, sign the generated artifacts with GnuPG and, finally, deploy to Sonatype’s Nexus Repository Manager.

总之,要将闪亮的新Java库上载到Maven Central,我们必须保留我们的组ID,在项目的pom.xml中提供所需的详细信息,使用GnuPG对生成的工件进行签名,最后将其部署到Sonatype的Nexus信息库经理。

步骤1: (STEP ONE:)

The first step is for us to make sure our open-source library is available on a publicly accessible code repository like Github. Then we can proceed to reserve our desired Group ID. The Group ID should ideally be unique to an individual or organization — as in a domain. Examples of Group ID includes com.smattme and org.apache.commons.

第一步是确保我们的开源库在Github等可公开访问的代码存储库中可用。 然后,我们可以继续保留所需的组ID。 理想情况下,组ID对于域或个人或组织应该是唯一的 。 组ID的示例包括com.smattmeorg.apache.commons

So, we are going to create a JIRA account here and then log in to create a new project ticket. Clicking on the create button at the top of the website will load a modal where we’ll supply the Group ID (e.g. com.smattme), SCM URL (e.g. https://github.com/SeunMatt/mysql-backup4j.git), project URL (e.g. https://github.com/SeunMatt/mysql-backup4j), Summary (which can be the name of the library like mysql-backup4j) and finally project description.

因此,我们将在此处创建JIRA帐户,然后登录以创建新的项目票证。 单击网站顶部的创建按钮将加载一个模式,我们将在其中提供组ID(例如com.smattme),SCM URL(例如https://github.com/SeunMatt/mysql-backup4j.git ) ,项目URL(例如https://github.com/SeunMatt/mysql-backup4j ),摘要(可以是类似于mysql-backup4j的库的名称),最后是项目描述。

NOTE: In the modal for creating a new project ticket, ensure the Project field is set to Community Support — Open Source Project Repository Hosting (OSSRH) and the Issue Type is New Project.

注意:在用于创建新项目票证的方式中,确保将“ 项目”字段设置为“ 社区支持-开源项目资源库托管(OSSRH)” 发行类型新项目

The creation of the new ticket project will trigger the creation of repositories on Sonatype’s OSS Repository Hosting (OSSRH) that’ll be synced to Maven Central after deploying our artifacts.

新票务项目的创建将触发Sonatype的OSS存储库托管(OSSRH)上存储库的创建,部署我们的工件后,该存储库将同步到Maven Central。

It’s important not to deploy until there’s an email confirmation that the issue created has been resolved. If there’s any problem along the line, we can always comment on the issue to get help and/or explanation.

请务必在收到确认已解决问题的电子邮件确认之前进行部署。 如果沿途有任何问题,我们可以随时对此问题发表评论以寻求帮助和/或解释。

第二步: (STEP TWO:)

Now that we’ve successfully registered our Group ID, the next thing to do is update the project’s pom.xml with the necessary information. Let’s start by providing the project’s name, description and URL as well as the coordinates and packaging information:

现在我们已经成功注册了组ID,接下来要做的就是使用必要的信息更新项目的pom.xml 。 让我们从提供项目的名称,描述和URL以及坐标和包装信息开始:

<groupId>com.smattme</groupId>
<artifactId>mysql-backup4j</artifactId>
<version>1.0.1</version>
<packaging>jar</packaging>
<name>${project.groupId}:${project.artifactId}&lt;/name>
<description> This is a simple library for backing up mysql databases and sending to emails, cloud storage and so on. It also provide a method for programmatically, importing SQL queries generated during the export process</description>
<url>https://github.com/SeunMatt/mysql-backup4j</url>

Up next is the license and developers information. In this case, we’ll be using an MIT license. If any other license is used, all that’s needed is a corresponding URL to such license. If it’s an open-source license, there’s a good chance it’s going to be available on opensource.org:

接下来是许可证和开发人员信息。 在这种情况下,我们将使用MIT许可证。 如果使用了其他任何许可证,则所需的只是该许可证的相应URL。 如果它是开源许可证,则很有可能将在opensource.org上提供

<licenses> <license>  <name>MIT License</name>              <url>http://www.opensource.org/licenses/mit-license.php</url>       <;/license> </licenses> <developers>
<developer>   <name>Seun Matt</name>   <email>smatt382@gmail.com</email>      <organization>SmattMe</organization> <organizationUrl>https://smattme.com</organizationUrl></developer>
</developers>

Another important piece of information required is the source code management (SCM) details:

所需的另一个重要信息是源代码管理(SCM)详细信息:

<scm>   <connection>scm:git:git://github.com/SeunMatt/mysql-backup4j.git</connection>   <developerConnection>scm:git:ssh://github.com:SeunMatt/mysql-backup4j.git</developerConnection>   <url>https://github.com/SeunMatt/mysql-backup4j/tree/master</url> </scm>

In this case, the project is hosted on Github, thus the reason for the supplied values. Example configurations for other SCM can be found here.

在这种情况下,项目托管在Github上,因此提供了值的原因。 其他SCM的示例配置可在此处找到。

第三步: (STEP THREE:)

In this step, we’ll be configuring our project for deployment to the OSSRH using the Apache Maven Plugin. The plugin requires that we add a distributionManagement section to our pom.xml:

在此步骤中,我们将配置我们的项目,以使用Apache Maven插件将其部署到OSSRH。 该插件要求我们在pom.xml中添加一个distributionManagement部分:

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

We’ll supply the user credentials for the configured repositories above by adding a <servers> entry to settings.xml which can be found in the user’s home directory e.g. /Users/smatt/.m2. Note that the id is the same as that of the snapshotRepository and repository configured above:

通过在settin gs.xml中添加<serve rs>条目,我们将为上述配置的存储库提供用户凭据,该条目可以在用户的​​主目录中找到, 例如/ Users / sma tt / .m2。 注意THA 的id是相同O至snapshotRepo SITOR y和回购 sitory如上配置:

<settings>   <servers>    <server>     <id>ossrh</id>    <username>your-jira-id</username>    <password>your-jira-pwd&lt;/password>  </server>   </servers></settings>

The next thing is for us to add some Maven build plugins: source-code, Javadoc, nexus staging and the GPG plugins. Each of these plugins will be placed within the <plugins> tag that’s inside the &lt;build> tag.

接下来我们要添加一些Maven构建插件:源代码,Javadoc,nexus登台和GPG插件。 每个这些插件将放置在<plugi N s >标记这insidË的&lt内;构建>标记。

Deploying a library to OSSRH requires that the source code and JavaDoc of the library be deployed as well. Hence, we’ll add the Maven plugins to achieve that seamlessly:

将库部署到OSSRH时,还需要部署库的源代码和JavaDoc。 因此,我们将添加Maven插件来无缝实现:

<plugin>   <groupId>org.apache.maven.plugins</groupId>  <artifactId>maven-javadoc-plugin</artifactId>         <version>3.0.0</version> <executions>   <execution>    <id>attach-javadocs</id>   <goals>     <goal>jar</goal>   </goals>   </execution>  </executions> </plugin>
<plugin>  <groupId>org.apache.maven.plugins</groupId>  <artifactId>maven-source-plugin</artifactId>    <version>3.0.1</version> <executions>   <execution>    <id>attach-sources</id>   <goals>      <goal>jar</goal>   </goals>   </execution> </executions> </plugin>

The latest version of the Javadoc and Source code plugins can be found here and here respectively.

可以在此处此处分别找到Javadoc和源代码插件的最新版本。

Another requirement we need to satisfy is the signing of our artifacts with a GPG/PGP program. For that, we have to install GnuPG on our system. After downloading and installation, we can always run gpg — version to verify the installation. Note that on some systems gpg2 — version will be used. Also, we should ensure that the bin folder of the GnuPG installation is in the system path.

我们需要满足的另一个要求是使用GPG / PGP程序对工件进行签名。 为此,我们必须在系统上安装 GnuPG。 下载并安装后,我们始终可以运行gpg-version来验证安装。 请注意,在某些系统上,将使用gpg2-版本 。 另外,我们应确保GnuPG安装的bin文件夹在系统路径中。

Now we can generate a key pair for our system by running the command gpg — gen-key and following the prompts. We can list the keys that are available using gpg — list-keys. It’s important to follow this guide to ensure the primary key generated is used to sign our files.

现在,我们可以通过运行命令gpg — gen-key并按照提示来为系统生成密钥对。 我们可以使用gpg列出可用的键-列表键。 请务必遵循本指南,以确保生成的主键用于对我们的文件进行签名。

Let’s get back to our pom.xml file and add the Maven plugin for the GnuPG program so our files can be automatically signed with the default key we generate during program build:

让我们回到pom.xml文件并为GnuPG程序添加Maven插件,以便我们的文件可以使用在程序构建期间生成的默认密钥自动签名:

<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>

The latest version for the Maven GPG plugin can be found here. While generating our key pair, we provided a passphrase for it; that passphrase is going to be configured in our .m2/settings.xml file. We can also specify the executable for our GnuPG program — either gpg or gpg2. So in the settings.xml file, we’ll add a &lt;profiles> section just after the closing tag for </servers>:

您可以在此处找到Maven GPG插件的最新版本。 在生成密钥对时,我们为其提供了密码。 该密码短语将在我们的.m2 / settings.xml文件中进行配置。 我们也可以指定为我们的GnuPG程序的可执行-无论是GPGgpg2。 因此,在settings.xml文件中,我们将在< / servers>的结束标记之后添加lt; profil es>部分:

<profiles>  <profile>   <id>ossrh</id>  <activation>   <activeByDefault>true</activeByDefault>  </activation>  <properties>    <gpg.executable>gpg</gpg.executable>   <gpg.passphrase>pass-phrase</gpg.passphrase> </properties> </profile> </profiles>

To wrap it all up, we’ll add the Nexus Staging Maven plugin so that we can deploy our library — including the source code, Javadoc and *.asc files to OSSRH, with simple commands:

总结一下,我们将添加Nexus Staging Maven插件,以便我们可以使用简单的命令将库(包括源代码,Javadoc和* .asc文件)部署到OSSRH中:

<plugin>  <groupId>org.sonatype.plugins</groupId> <artifactId>nexus-staging-maven-plugin</artifactId>          <version>1.6.8</version> <extensions>true</extensions>
<configuration>    <serverId>ossrh</serverId>    <nexusUrl>https://oss.sonatype.org/</nexusUrl> <autoReleaseAfterClose>false</autoReleaseAfterClose> </configuration>
</plugin>

The latest version of the plugin can be found here. Take note of the <serverId>ossrh</serverId>; You’ll notice the same value ossrh is used in the settings.xml file. This is important for the plugin to be able to locate the credentials we configured in the <servers> section of settings.xml.

可以在此处找到该插件的最新版本。 注意<serverId> ossrh </ se rverId>; 您会注意到s ettings.xml文件中使用相同的值ossrh。 该插件能够找到凭据这一点很重要,我们的配置ured在T ^ hë<服务RS>上的settings.xml的secti。

第四步: (STEP FOUR:)

In this section we’ll run some Maven CLI commands to do the actual deployment. Before this stage, it’s just so right that we double check and test the library to make sure there’re no bugs. Why? We’re about to go live!

在本节中,我们将运行一些Maven CLI命令来进行实际部署。 在此阶段之前,非常正确,我们再次检查并测试该库以确保没有错误。 为什么? 我们即将上线!

To start with run: mvn clean deploy

从运行开始: mvn clean deploy

If everything goes well, we’ll see, among the console outputs, the staging repository id created for the project like this:

如果一切顺利,我们将在控制台输出中看到为项目创建的登台存储库ID,如下所示:

* Created staging repository with ID “comsmattme-1001”.

*创建ID为“ comsmattme-1001”的登台存储库。

NOTE: “comsmattme” is the Group ID we’ve been using in this article as an example.

注意:“ comsmattme ”是我们在本文中用作示例的Group ID。

Let’s log in to https://oss.sonatype.org with the same credentials for the JIRA account created earlier to inspect the artifacts we’ve deployed to staging. After login, we’ll click on Staging Repositories on the left side menu under the Build Promotion sub-menu and using the search bar at the top right of the page, we’ll search for the staging repository ID created e.g. comsmattme-1001 in this case.

让我们登录到https://oss.sonatype.org 使用与先前创建的JIRA帐户相同的凭据来检查我们已部署到暂存的工件。 登录后,我们将在“ Build Promotion”子菜单下的左侧菜单上单击“ Staging Repositories” ,然后使用页面右上方的搜索栏搜索创建的Staging Repository ID,例如comsmattme-1001 。这个案例。

We should be able to see and inspect the artifacts that were uploaded by the nexus staging Maven plugin. If satisfied with everything, then we can do a release by running this maven command:

我们应该能够查看和检查由nexus登台的Maven插件上传的工件。 如果对所有内容都满意,则可以通过运行以下maven命令进行发布:

mvn nexus-staging:release

mvn连结阶段:发布

It may take up to 2 hours or more for the library to show up on Maven Central Search. To search for our artifact, we’ll supply the following query to the search box: g:com.smattme a:mysql-backup4j where g is the group ID and a is the artifact name.

该库最多可能需要2个小时或更长时间才能显示在Maven Central Search中 。 要搜索我们的工件,我们将在搜索框中提供以下查询: g:com.smattme a:mysql-backup4j其中g是组ID, a是工件名称。

结论 (Conclusion)

In this comprehensive article, we’ve walked through the process of deploying our Java library to Maven Central. The complete pom.xml for the example project used in this article can be found here and the settings.xml can be found here as well. Happy coding!

在这篇综合文章中,我们介绍了将Java库部署到Maven Central的过程。 完整的pom.xml 本文中使用的示例项目的示例可以在此处找到, settings.xml也可以在此处找到。 编码愉快!

Originally published at smattme.com.

最初发布于smattme.com

翻译自: https://www.freecodecamp.org/news/how-to-upload-an-open-source-java-library-to-maven-central-cac7ce2f57c/

maven 私有库,开源库

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值