专业的高端网站设计公司,网站建设费属于广告费吗,科技公司的网站,龙岩网站建设平台手把手教你将自己的项目发布到 Maven Central 一、准备工作#xff1a;确定你的 Group Id Group Id 是用来指定项目包归属的#xff0c;这个 id 可不能随便写哦。要自定义 group id#xff0c;通常需要有一个自己的域名#xff0c;将域名反转过来就是你可以管理的 group …手把手教你将自己的项目发布到 Maven Central一、准备工作确定你的 Group IdGroup Id 是用来指定项目包归属的这个 id 可不能随便写哦。要自定义 group id通常需要有一个自己的域名将域名反转过来就是你可以管理的 group id。比如 Spring Boot 持有域名boot.springframework.org在 Maven Central 注册的 group id 就是反转后的org.springframework.bootdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency不过买域名毕竟要花钱如果对 group id 的命名不那么执着还可以用代码托管平台提供的免费 Namespace。使用 GitHub/Gitee 作为 Group IdSonatype 为代码托管平台预留了免费的命名空间GitHubio.github.你的用户名例如io.github.exampleGiteeio.gitee.你的用户名例如io.github.mandala5741下面我以 Gitee 为例进行演示。注意Group Id 不能有特殊字符如下划线、横线等。如果你的 Gitee 用户名包含特殊字符如gavin_luo需要先修改用户名。修改方法进入 Gitee 设置 → 个人资料 → 个人空间地址修改为不含特殊字符的名称。二、注册 Maven Central 账号打开 central.sonatype.com点击右上角的Sign In在新窗口中点击Sign up填写注册信息用户名不能有特殊字符邮箱填写真实邮箱密码需要包含特殊字符且足够长点击Continue后前往邮箱查收确认邮件邮件标题Verify your email for central-portal点击邮件中的Confirm My Account验证成功后返回 central.sonatype.com点击Sign In登录三、创建并验证 Maven Group登录后点击右上角用户头像 →View Namespace点击Add Namespace添加新的 Group输入你的 Group Id例如io.gitee.luoyuan800点击创建此时 group id 状态为Unverified点击Verify Namespace按照提示进行验证在你的 Gitee 账号下创建一个public仓库仓库名称必须是指定的一串字符创建完成后点击Confirm验证成功后可以删除该仓库只是为了证明你有账号管理权限稍等片刻刷新页面状态变为Verified即表示验证成功四、配置本地 Maven 环境4.1 创建 Deployment TokenToken 用于本地部署时进行身份认证登录后点击右上角用户头像 →View Account找到Setup Token-Based Authentication页面点击Generate User Token在弹出的窗口点击OK立即保存生成的 Token它只会出现一次username: 一串字符 password: 一串字符如果不慎丢失可以先Revoke User Token再重新生成。4.2 配置 Maven settings.xml找到 Maven 的settings.xml文件通常在~/.m2/settings.xml或 Maven 安装目录的conf/下在servers标签中添加serveridcentral/id!-- 这个 id 后面要在 pom.xml 中引用 --username你的 token username/usernamepassword你的 token password/password/server五、安装配置 GPG 签名Maven Central 要求所有上传的 jar 包都必须经过 GPG 签名。5.1 下载安装 GPG访问 GnuPG 下载页面找到GnuPG binary releases下载对应操作系统的安装包安装完成后打开命令行验证gpg --version5.2 生成密钥对gpg --gen-key按提示输入Real name你的名字Email address你的邮箱密码务必记住这个密码后面配置要用5.3 上传公钥到公钥服务器查看生成的公钥pub 后面第二行的字符串gpg --list-keys上传公钥将public_key替换为你的公钥gpg --keyserver keyserver.ubuntu.com --send-keyspublic_key验证上传是否成功gpg --keyserver keyserver.ubuntu.com --recv-keyspublic_key公钥同步可能需要几分钟时间才能在全球生效。5.4 配置 GPG 到 settings.xml在settings.xml的profiles标签中添加profileidcentral/idactivationactiveByDefaulttrue/activeByDefault/activationproperties!-- 替换为你的 gpg 安装路径 --gpg.executableD:\Program Files\GnuPG\bin\gpg/gpg.executable!-- 替换为生成密钥时设置的密码 --gpg.passphrase你的密码/gpg.passphrase/properties/profile六、配置项目的 pom.xml在你需要发布的项目pom.xml中添加以下插件配置都在build→plugins内6.1 基础项目信息首先确保项目有完整的基础信息groupId你的-group-id/groupIdartifactId你的-artifact-id/artifactIdversion1.0.0/version!-- 注意不要带 -SNAPSHOTSNAPSHOT 版本不会发布到中央仓库 --packagingjar/packagingname项目名称/namedescription项目描述/descriptionurl项目主页/urllicenseslicensenameApache License, Version 2.0/nameurlhttps://www.apache.org/licenses/LICENSE-2.0.txt/url/license/licensesdevelopersdevelopername你的名字/nameemail你的邮箱/email/developer/developersscmconnectionscm:git:你的仓库地址.git/connectiondeveloperConnectionscm:git:你的仓库地址.git/developerConnectionurl你的仓库地址/url/scm6.2 生成 Javadoc 的插件plugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-javadoc-plugin/artifactIdversion3.12.0/versionexecutionsexecutionidattach-javadocs/idgoalsgoaljar/goal/goals/execution/executions/plugin6.3 打包源码的插件plugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-source-plugin/artifactIdversion3.4.0/versionexecutionsexecutionidattach-sources/idgoalsgoaljar-no-fork/goal/goals/execution/executions/plugin6.4 GPG 签名插件plugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-gpg-plugin/artifactIdversion3.2.8/versionexecutionsexecutionidsign-artifacts/idphaseverify/phasegoalsgoalsign/goal/goals/execution/executions/plugin如果本地有多个密钥对需要指定使用哪一个configurationkeyname你的密钥ID/keyname/configuration6.5 部署到 Maven Central 的插件plugingroupIdorg.sonatype.central/groupIdartifactIdcentral-publishing-maven-plugin/artifactIdversion0.9.0/versionextensionstrue/extensionsconfiguration!-- 必须与 settings.xml 中的 server id 一致 --publishingServerIdcentral/publishingServerId!-- true 表示上传后自动发布省去手动点击的步骤 --autoReleaseAfterClosetrue/autoReleaseAfterClose/configuration/plugin在 pom.xml 中配置发布地址distributionManagementrepository!-- 这里的 id 必须与 settings.xml 中的 server id 一致 --idcentral/id!-- 新版的 Portal Publisher API 地址 --urlhttps://central.sonatype.com/api/v1/publisher/url/repository/distributionManagement七、执行部署7.1 执行部署命令在项目根目录下执行mvn clean deploy注意第一次执行时因为网络原因你懂的下载加密相关依赖可能会比较慢甚至失败。可以多试几次或者配置国内镜像加速。7.2 部署成功看到类似下面的信息表示部署成功Deployment xxxx has been validated7.3 手动发布如果未配置自动发布如果你在插件中没有配置autoReleaseAfterClosetrue/autoReleaseAfterClose需要手动发布登录 central.sonatype.com左侧导航栏点击Deployments或从头像菜单进入View Deployments找到状态为Validated的发布记录点击进入详情点击右上角的Publish按钮等待 15-30 分钟你的项目就会出现在中央仓库八、验证发布结果8.1 在线搜索验证访问 Maven Central 搜索引擎搜索你的 Group Id 或 Artifact Id如果能查到项目信息说明发布成功。8.2 新建项目测试创建一个全新的 Maven 项目在pom.xml中引入你的依赖dependencygroupId你的-group-id/groupIdartifactId你的-artifact-id/artifactIdversion你的-version/version/dependency执行mvn compile如果能成功下载依赖说明你的包已经可以在全世界范围内被正常引用了九、常见问题与注意事项9.1 版本号注意事项不要使用-SNAPSHOT版本SNAPSHOT 版本不会发布到 Maven Central只能在 s01.oss.sonatype.org 找到只有正式版本如1.0.0、2.1.3才会发布到中央仓库9.2 GPG 相关问题密钥被锁或忘记密码重新生成密钥对即可多台电脑发布导出私钥gpg --export-secret-keys -a 你的KEY_ID private.key导入私钥gpg --import private.key签名错误检查settings.xml中的gpg.passphrase是否与生成密钥时设置的密码一致检查gpg.executable路径是否正确9.3 部署失败常见原因Group Id 未验证检查 Namespace 是否已变为 Verified 状态缺少必要文件确保配置了 source 和 javadoc 插件签名失败GPG 配置有问题网络问题第一次部署时下载依赖可能失败多试几次Maven settings.xml 配置文件?xml version1.0 encodingUTF-8?settingsxmlnshttp://maven.apache.org/SETTINGS/1.2.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd!-- 本地仓库路径 --localRepositoryD:\Android\maven\m2\repository/localRepository!-- 插件组默认保留无需修改 --pluginGroups/pluginGroups!-- 代理配置无 --proxies/proxies!-- 服务器认证配置关键部分 --servers!-- Maven Central 发布认证使用最新生成的 Token --serveridcentral/id!-- 此ID必须与pom.xml中的配置一致 --username7xxxxxxyb/usernamepasswordsg0rR0Oxxxxxxakzlpagf/password/server/servers!-- 镜像配置无 --mirrors/mirrors!-- 配置文件包含GPG签名配置 --profilesprofileidcentral/idactivationactiveByDefaulttrue/activeByDefault/activationproperties!-- GPG可执行文件路径Windows完整路径 --gpg.executableD:\Program Files (x86)\GnuPG\bin\gpg.exe/gpg.executable!-- 你的GPG密钥ID通过 gpg --list-keys 查看 --gpg.keyname1CXXXXXXXXXXXXXX/gpg.keyname!-- ⚠️ 重要这里需要修改为生成密钥时设置的密码不是密钥ID --gpg.passphrase请填写你的GPG密钥密码/gpg.passphrase/properties/profile/profiles!-- 激活的配置文件无因为上面已设置activeByDefault --activeProfiles/activeProfiles/settings配置说明✅已正确配置的部分本地仓库路径D:\Android\maven\m2\repositoryMaven Central Token使用最新的7xxxxybTokenGPG 可执行文件路径Windows 完整路径已设置⚠️需要你修改的部分GPG 密码gpg.passphrase中需要填写生成密钥时设置的密码不是密钥IDGPG 密钥ID如果上面的1CXXXXXXXXXXXXXX不是你的真实密钥ID需要通过以下命令查看# 查看所有密钥gpg --list-keys# 输出示例# pub rsa3072 2024-01-01 [SC]# 1A2B3C4D5xxxxQ8R9S0T# uid [ultimate] Your Name your.emailexample.com# 密钥ID就是上面那串长字符的最后16位或8位pom.xml 对应配置参考确保你的pom.xml中有以下配置与之对应!-- 发布管理配置 --distributionManagementrepositoryidcentral/id!-- 必须与settings.xml中的server id一致 --urlhttps://central.sonatype.com/api/v1/publisher/url/repository/distributionManagement恭喜现在你已经成功将自己的项目发布到 Maven Central全球的开发者都可以通过一行简单的依赖配置来使用你的代码了。