如何手动将JAR发布到maven central?

Ksh*_*rma 27 java maven

我创建了一个开源项目,我想将其发布到maven central,以便用户可以通过简单地在它们的pom中引用它来使用该库.像这样:

<dependency>
    <groupId>in.ksharma</groupId>
    <artifactId>log4j-weblayout</artifactId>
    <version>0.0.1-BETA</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

我发现了几个在线教程,但其中一些已经过时,有些人建议自动完成整个过程,从而使其明显复杂化.

例如,一个教程建议为您的github帐户创建SSH密钥,并且只要推送到maven central,maven就会自动创建一个git标记.虽然这很有用,但没有必要开始.

另一个例子,尝试通过maven直接释放它也会产生某种错误:

mvn release:clean release:prepare release:perform -B -e | tee maven-central-deploy.log
Run Code Online (Sandbox Code Playgroud)

得到:

svn:E155007:'/ home/kshitiz/Documents/workspaces/ggts/log4j-weblayout/pom.xml'不是工作副本

当你第一次做某事时,通常先手动完成,然后自动完成.

将JAR置于maven中心的最基本,最简单的方法是什么?

Ben*_*ldt 27

1)创建您的Jira帐户:注册Sonatype


2)创建新项目故障单(声明工作区):创建新项目故障单


3)生成PGP签名

gpg2 --gen-key
....
gpg: key YOUR_KEY_ID marked as ultimately trusted
...
Run Code Online (Sandbox Code Playgroud)

4)分发您的公钥

gpg2 --keyserver hkp://pool.sks-keyservers.net --send-keys YOUR_KEY_ID
Run Code Online (Sandbox Code Playgroud)

将密钥分发给多个服务器以加快同步过程(pgp.mit.edu,keyserver.ubuntu.com ...)


5)更新~.m2/settings.xml

<settings>
  <servers>
    <server>
      <id>ossrh</id>
      <username>jira_username</username>
      <password>jira_password</password>
    </server>
  </servers>
  <profiles>
    <profile>
      <id>ossrh</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <gpg.executable>gpg2</gpg.executable>
        <gpg.passphrase>your_key_passphrase</gpg.passphrase>
      </properties>
    </profile>
  </profiles>
</settings>
Run Code Online (Sandbox Code Playgroud)

6)更新项目pom.xml

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

    <parent>
        <groupId>org.sonatype.oss</groupId>
        <artifactId>oss-parent</artifactId>
        <version>9</version>
    </parent>

    <groupId>xxx.xxx</groupId>
    <artifactId>xxx</artifactId>
    <version>0.1</version>

    <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>
    <build>
        <plugins>
            <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>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-gpg-plugin</artifactId>
                <version>1.5</version>
                <executions>
                    <execution>
                        <id>sign-artifacts</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>sign</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <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://oss.sonatype.org/</nexusUrl>
                    <autoReleaseAfterClose>true</autoReleaseAfterClose>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
Run Code Online (Sandbox Code Playgroud)

7)运行Maven

Maven会问你的密码

mvn clean deploy
Run Code Online (Sandbox Code Playgroud)

8)评论你的Jira票

这将触发与您的组ID同步的同步.

我推出了我的第一个版本.谢谢.


资源:

OSSRH指南

使用Maven部署

PGP签名


Ksh*_*rma 19

这个答案假定您有一个基于maven的项目,并且它处于可打包状态.mvn package应该运行没有任何错误.

发布到maven central时,您需要使用一个组ID来识别您上传的所有工件.有点像in.ksharma.您还需要对工件进行签名,以便用户能够验证他们实际上是否来自您.

因此,首先转到sonatype jira并创建一个帐户,然后创建一个jira问题以使您的组ID被批准.像这样的东西.

现在生成用于签署工件的gpg密钥对:

$ gpg --gen-key
Run Code Online (Sandbox Code Playgroud)

~/.m2/settings.xml以下位置定义此键:

<profiles>
  <profile>
    <id>sonatype-oss-release</id>
    <properties>
      <gpg.keyname>B63EFB4D</gpg.keyname>
      <gpg.passphrase>****</gpg.passphrase>
      <gpg.defaultKeyring>true</gpg.defaultKeyring>
      <gpg.useagent>true</gpg.useagent>
      <gpg.lockMode>never</gpg.lockMode>
      <gpg.homedir>/home/kshitiz/.gnupg</gpg.homedir>
    </properties>
  </profile>
</profiles>
Run Code Online (Sandbox Code Playgroud)

修改项目的pom文件并附-SNAPSHOT加到您的版本.因此0.0.1-BETA变得0.0.1-BETA-SNAPSHOT.否则maven会抱怨:

[错误]无法执行目标org.apache.maven.plugins:maven-release-plugin:2.4.2:在项目log4j-weblayout上准备(default-cli):您在反应堆项目列表中没有SNAPSHOT项目. - > [帮助1]

还添加:

<parent>
        <groupId>org.sonatype.oss</groupId>
        <artifactId>oss-parent</artifactId>
        <version>9</version>
</parent>
Run Code Online (Sandbox Code Playgroud)

此父pom为您提供了一些现成的功能,例如配置对maven-gpg-pluginJAR进行签名.

现在运行mvn release:clean release:prepare以生成工件和gpg签名.它会给你一些类似的东西:

log4j-weblayout-0.0.1-BETA-javadoc.jar.asc
log4j-weblayout-0.0.1-BETA-sources.jar.asc
log4j-weblayout-0.0.1-BETA.pom.asc
log4j-weblayout-0.0.1-BETA.pom
log4j-weblayout-0.0.1-BETA.jar.asc
log4j-weblayout-0.0.1-BETA-javadoc.jar
log4j-weblayout-0.0.1-BETA-sources.jar
log4j-weblayout-0.0.1-BETA.jar
Run Code Online (Sandbox Code Playgroud)

现在将它们打包成一个JAR:

jar -cvf bundle.jar log4j-weblayout-0.0.1-BETA*
Run Code Online (Sandbox Code Playgroud)

转到Sonatype Nexus并使用您的凭据登录.转到暂存上传并上传您的捆绑包.

在此输入图像描述

然后转到staging repositories部分,选择您的存储库并单击release(此处提供更多帮助).评论jira问题,你已经发布了工件并等待一段时间.

现在,您的用户可以从中央存储库中搜索和使用工件: 在此输入图像描述 在此输入图像描述

  • https://github.com/sonatype/oss-parents 表示 org.sonatype.oss 已弃用。也许需要更新?无论如何,感谢您提供详细的质量检查! (2认同)