电光石火-穿越时空电光石火-穿越时空


发布maven项目到中央仓库

用maven已经一段时间,有一些通用的可以开源的代码想放到公网的仓库中,以便可以随时使用。
注册Sonatype OSSRH
1:注册一个JIRA账号:https://issues.sonatype.org/secure/Signup!default.jspa
2:创建一个新工程的单:https://issues.sonatype.org/secure/CreateIssue.jspa?issuetype=21&pid=10134
项目groupId怎么写? 如果你是个人的名义,然后代码是放到github上面的(比如是https://www.github.com/username/projectName的格式),那么推荐groupId写成:com.github.username, 不然可能会被拒绝(如果是自己的域名就要设置txt解析或者用域名邮箱发送邮件到指定邮箱就可以完成认证)
只有当project的status的状态是resolved时,才可以提交jar包
TIM截图20181220101819.jpg
审查要求
1: 提供javadoc和source
2: 使用gpg或者pgp对文件进行签名
3: pom.xml文件
4: 正确的坐标:groupId,artifactId,version,projectName,description,url等

<groupId>net.dopan</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.0.0</version>

<name>${project.groupId}:${project.artifactId}</name>
<description>Fastdfs Client Library</description>
<url>http://xxx/fastdfs-client</url>

5: license 信息

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

6: 开发者信息

<developers>
    <developer>
        <name>liukuan</name>
        <email>250502876@qq.com</email>
        <organization>dopan</organization>
        <organizationUrl>http://xxx</organizationUrl>
        <timezone>+8</timezone>
    </developer>
</developers>

7: SCM信息

<scm>
    <connection>
        scm:git:http://xxx/fastdfs-client.git
    </connection>
    <developerConnection>
        scm:git:http://xxx/fastdfs-client.git
    </developerConnection>
    <url>http://xxx/fastdfs-client</url>
    <tag>v1.0.0</tag>
</scm>

部署 然后添加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>

接着添加plugins

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </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-no-fork</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>3.0.1</version>
            <executions>
                <execution>
                    <id>attach-javadocs</id>
                    <goals>
                        <goal>jar</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>

<profiles>
    <profile>
        <id>release</id>
        <build>
            <plugins>
            <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>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-release-plugin</artifactId>
                    <version>2.5.3</version>
                    <configuration>
                        <autoVersionSubmodules>true</autoVersionSubmodules>
                        <useReleaseProfile>false</useReleaseProfile>
                        <releaseProfiles>release</releaseProfiles>
                        <goals>deploy</goals>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

修改maven的setting.xml
TIM截图20181220103728.jpg
然后设置gpg的profile

<profiles>
    <profile>
    <id>ossrh</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
        <gpg.executable>gpg</gpg.executable>
        <gpg.passphrase>the_pass_phrase</gpg.passphrase>
    </properties>
    </profile>
</profiles>

此时要先安装gpg
生成公钥私钥gpg --gen-key
按提示操作即可
查看公钥私钥gpg --list-keys
其中 pub 是需要传到服务器的发布公钥gpg --keyserver hkp://pool.sks-keyservers.net --send-keys 5D28048BDC8E5DC5C5CC549EDF13760BB8535F0E
查看是否成功gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 5D28048BDC8E5DC5C5CC549EDF13760BB8535F0E

执行mvn deploy
执行以下命令 mvn deploy -Dmaven.test.skip=true -e
发布到OSS
这一步主要是生成相应的一些jar包和签名文件,并上传到OSS的服务器,命令也比较简单mvn clean deploy -P release -Dgpg.passphrase=12345678
或者mvn clean package deploy -P release
如果前几个步骤全部正确完成,登录https://oss.sonatype.org/#stagingRepositories(用户名密码就是第一步注册时的用户名密码将Staging Rpositories拉到最下即可看到你刚刚发布的jar包,选择上方的Close,稍等片刻他先检查一下你上传的东西是否符合规范,检查完毕后该条状态被标记为closed,此时选中后点上面的Release即可,等2个小时左右即可在http://search.maven.org/看到你发布的jar包。
TIM截图20181220162527.jpg
上一步成功后,就表示发布完成。此时去https://issues.sonatype.org(就是你一开始创建的issue)中留言,告诉管理员你已经release了(Has been released)。等审核通过后,就可以在中央仓库https://mvnrepository.com中搜索出你的项目(是不是很激动,很有成就感^_^)。

本博客所有文章如无特别注明均为原创。作者:似水的流年
版权所有:《电光石火-穿越时空》 => 发布maven项目到中央仓库
本文地址:http://www.ilkhome.cn/index.php/archives/451/
欢迎转载!复制或转载请以超链接形式注明,文章为 似水的流年 原创,并注明原文地址 发布maven项目到中央仓库,谢谢。

评论