Maven是一个Java项目的管理和构建工具,Maven 仓库是项目中依赖的第三方库,这个库所在的位置叫做仓库。
Maven中央仓库是由 Maven 社区提供的仓库,其中包含了大量常用的库。中央仓库包含了绝大多数流行的开源Java构件,当我们需要使用某个构建功能时,我们只需要在pom文件引入其maven坐标即可。
本文我们讲解如何把自己的jar发布到maven中央仓库里面,供外网其他用户使用。
登录 issues.sonatype.org/secure/Sign… 注册一个账号
菜单栏->Create 弹出创建界面,Project选择Community Support - Open Source Project Repository Hosting (OSSRH) 。Issue Type选择 New Project。 Group Id填写maven坐标的Group Id。
注意:申请一个Group Id后,就可以发布不同的artifactId对应的jar。
brew install gnupg
gpg --gen-key # 创建自己的key
gpg --list-secret-keys # 查看自己的key
gpg --keyserver keys.gnupg.net --send-keys xxxxx gpg --keyserver keys.gnupg.net --recv-keys xxxxx gpg --keyserver keyserver.ubuntu.com --send-keys xxxxx gpg --keyserver keyserver.ubuntu.com --recv-keys xxxxx
在要发布的包的pom文件里面,添加如下插件:
- <plugins>
- <plugin>
- <artifactId>nexus-staging-maven-pluginartifactId>
- <configuration>
- <autoReleaseAfterClose>trueautoReleaseAfterClose>
- <nexusUrl>https://s01.oss.sonatype.org/nexusUrl>
- <serverId>ossrhserverId>
- configuration>
- <extensions>trueextensions>
- <groupId>org.sonatype.pluginsgroupId>
- <version>1.6.7version>
- plugin>
- <plugin>
- <artifactId>maven-compiler-pluginartifactId>
- <configuration>
- <encoding>UTF-8encoding>
- <source>1.8source>
- <target>1.8target>
- configuration>
- <version>${maven-compiler-plugin.version}version>
- plugin>
- <plugin>
- <artifactId>maven-resources-pluginartifactId>
- <configuration>
- <encoding>UTF-8encoding>
- configuration>
- <version>${maven-resources-plugin.version}version>
- plugin>
- <plugin>
- <artifactId>maven-source-pluginartifactId>
- <executions>
- <execution>
- <goals>
- <goal>jargoal>
- goals>
- <id>attach-sourcesid>
- execution>
- executions>
- <version>${maven-source-plugin.version}version>
- plugin>
- <plugin>
- <artifactId>maven-surefire-pluginartifactId>
- <configuration>
- <skipTests>trueskipTests>
- configuration>
- <groupId>org.apache.maven.pluginsgroupId>
- <version>${maven-surefire-plugin.version}version>
- plugin>
- <plugin>
- <artifactId>maven-jar-pluginartifactId>
- <configuration>
- <archive>
- <manifest>
- <addDefaultImplementationEntries>trueaddDefaultImplementationEntries>
- manifest>
- archive>
- configuration>
- <groupId>org.apache.maven.pluginsgroupId>
- <version>${maven-jar-plugin.version}version>
- plugin>
- <plugin>
- <artifactId>maven-javadoc-pluginartifactId>
- <configuration>
- <charset>UTF-8charset>
- <docencoding>UTF-8docencoding>
- <encoding>UTF-8encoding>
- <links>
- <link>http://docs.oracle.com/javase/8/docs/apilink>
- links>
- <show>publicshow>
- configuration>
- <executions>
- <execution>
- <goals>
- <goal>jargoal>
- goals>
- <id>attach-javadocid>
- execution>
- executions>
- <groupId>org.apache.maven.pluginsgroupId>
- <version>${maven-javadoc-plugin.version}version>
- plugin>
- <plugin>
- <artifactId>maven-gpg-pluginartifactId>
- <executions>
- <execution>
- <configuration>
- <gpgArguments>
- <arg>--pinentry-modearg>
- <arg>loopbackarg>
- gpgArguments>
- configuration>
- <goals>
- <goal>signgoal>
- goals>
- <id>sign-artifactsid>
- <phase>verifyphase>
- execution>
- executions>
- <groupId>org.apache.maven.pluginsgroupId>
- <version>${maven-gpg-plugin.version}version>
- plugin>
- plugins>
切换到要发的包的目录,执行: mvn clean deploy -Dgpg.passphrase=自己的gpg的密码
等待发布完毕,之后24小时后,会在mvnrepository.com/repos/centr… 搜到自己的包。