将项目发布到maven过程记录

说明

  用于记录将自己的项目发布到maven中央仓库的过程。

记录

  1. 注册sonatype账号:【申请上传资格】
    注册地址:https://issues.sonatype.org/secure/Signup!default.jspa

  2. 登录sonatype并初始化

  3. 新建issue并解决提出的问题
    如:https://issues.sonatype.org/browse/OSSRH-54353

  4. 创建密钥并发布
    安装gpg,新建密钥对(输入账号、邮箱、密码),发布密钥到服务服务端

    1
    2
    3
    4
    5
    6
    7
    # 列出密钥
    gpg --list-keys
    gpg --keyserver http://keyserver.ubuntu.com:11371 --send-keys key_id
    gpg --keyserver http://pool.sks-keyservers.net:11371 --send-keys key_id
    gpg --keyserver http://keyserver.ubuntu.com:11371 --send-keys key_id
    gpg --keyserver http://keys.gnupg.net:11371 --send-keys key_id
    gpg --keyserver http://keys.openpgp.org:11371 --send-keys key_id
  5. 配置maven

    1. 修改setting.xml

      1
      2
      3
      4
      5
      <server>
      <id>ossrh</id>
      <username>sonatype用户名</username>
      <password>sonatype密码</password>
      </server>
    2. 修改项目pom.xml

      注意:snapshotRepository节点和repository节点的id要和上面server配置的id一致
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      83
      84
      85
      86
      87
      88
      89
      90
      91
      92
      93
      94
      95
      96
      97
      98
      99
      100
      101
      102
      103
      104
      105
      106
      107
      108
      109
      110
      111
      112
      113
      114
      115
      116
      117
      118
      119
      120
      121
      122
      <!-->项目的协议<-->
      <licenses>
      <license>
      <name>The Apache Software License, Version 2.0</name>
      <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
      <distribution>actable</distribution>
      </license>
      </licenses>

      <!-->开发者的信息<-->
      <developers>
      <developer>
      <name>example</name>
      <email>example@outlook.com</email>
      </developer>
      </developers>

      <!-->项目的版本管理地址<-->
      <scm>
      <url>https://github.com/Bpazy/Id</url>
      </scm>
      <profiles>
      <profile>
      <id>release</id>
      <properties>
      <profile.env>prod</profile.env>
      </properties>
      <build>
      <resources>
      <resource>
      <directory>src/main/java</directory>
      </resource>
      </resources>
      <plugins>
      <!-- Source -->
      <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>
      <configuration>
      <show>private</show>
      <nohelp>true</nohelp>
      <charset>UTF-8</charset>
      <encoding>UTF-8</encoding>
      <docencoding>UTF-8</docencoding>
      <additionalparam>-Xdoclint:none</additionalparam>
      <!-- TODO 临时解决不规范的javadoc生成报错,后面要规范化后把这行去掉 -->
      </configuration>
      <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.5</version>
      <executions>
      <execution>
      <phase>verify</phase>
      <goals>
      <goal>sign</goal>
      </goals>
      </execution>
      </executions>
      </plugin>

      <!--Compiler -->
      <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.0</version>
      <configuration>
      <source>1.8</source>
      <target>1.8</target>
      <fork>true</fork>
      <verbose>true</verbose>
      <encoding>UTF-8</encoding>
      <showWarnings>false</showWarnings>
      </configuration>
      </plugin>
      <!--Release -->
      <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-release-plugin</artifactId>
      <version>2.5.3</version>
      </plugin>
      </plugins>
      </build>
      <distributionManagement>
      <snapshotRepository>
      <id>ossrh</id>
      <name>Sonatype Nexus Snapshots</name>
      <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
      </snapshotRepository>
      <repository>
      <id>ossrh</id>
      <name>Nexus Release Repository</name>
      <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
      </repository>
      </distributionManagement>
      </profile>
      </profiles>
  6. 打包发布

    1
    mvn clean deploy -P release
  7. 将上传的开源库发布出去

    1. 登录https://oss.sonatype.org/#stagingRepositories查看staging中的包
    2. 选中包,closed,解决closed的时候出现的问题,再次打包上传,直到可以closed为止
    3. 选中包,release
  8. 登录sonatype并回复issue项目已发布,等待审核