系列完整文章参考:发布jar包到maven中央仓库系列目录
发布jar包到maven中央仓库需要对本地maven的配置文件进行修改。
需要在<servers>节点下增加以下内容
<server> <id>sonatype</id> <username>liqingcan</username> <password>youpassword</password> </server>
其中username和password分别是你注册sonatype时的账号名和密码。https://issues.sonatype.org/secure/Dashboard.jspa
接下来是项目pom文件的结构。以下是我pom文件结构,可以直接复制过来进行修改。
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <groupId>com.github.lqccan</groupId> <artifactId>wechat-work-bot</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <url>https://github.com/lqccan/wechat-work-bot</url> <name>wechat-work-bot</name> <description>a sdk for wechat work bot</description> <licenses> <license> <name>The Apache Software License, Version 2.0</name> <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url> </license> </licenses> <developers> <developer> <name>liqingcan</name> <email>1102946234@qq.com</email> </developer> </developers> <scm> <connection>https://github.com/lqccan/wechat-work-bot</connection> <developerConnection>https://github.com/lqccan/wechat-work-bot</developerConnection> <url>https://github.com/lqccan/wechat-work-bot</url> </scm> <properties> <java.version>1.8</java.version> <javadocExecutable>${java.home}/../bin/javadoc</javadocExecutable> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!--阿里巴巴的json包--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.62</version> </dependency> <!--hutool工具包--> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>4.1.12</version> </dependency> </dependencies> <profiles> <profile> <!-- 这个id就是打包时的 -P 参数 --> <id>release</id> <build> <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> <executions> <execution> <phase>package</phase> <goals> <goal>jar</goal> </goals> <!-- -Xdoclint:none 是为了避免生成apidoc的时候检查过于严格而报错--> <configuration> <additionalparam>-Xdoclint:none</additionalparam> </configuration> </execution> </executions> </plugin> <!-- GPG加密插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-gpg-plugin</artifactId> <version>1.6</version> <executions> <execution> <phase>verify</phase> <goals> <goal>sign</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <!-- snapshotRepository与repository的id应与setting.xml中添加的server的id一致 --> <distributionManagement> <snapshotRepository> <id>sonatype</id> <url>https://oss.sonatype.org/content/repositories/snapshots/</url> </snapshotRepository> <repository> <id>sonatype</id> <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url> </repository> </distributionManagement> </profile> </profiles> </project>
接下来就是打包推送的命令了。
mvn clean deploy -P release -Dmaven.test.skip=true -Dgpg.passphrase=xxxx
其中passphrase对应的xxxx就是你生成gpg密钥时输入的密码。
命令执行完之后,你就可以到https://oss.sonatype.org/#stagingRepositories 去查看你发布的jar包
坑1:
在使用maven打包推送的时候,碰到了javadoc的一个错误
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-javadoc-plugin:2.9.1:jar (default) on project wechat-work-bot: MavenReportException: Error while creating archive: Unable to find javadoc command: The environment variable JAVA_HOME is not correctly set. -> [Help 1]
解决办法就是。在上述pom文件中指定的
<javadocExecutable>${java.home}/../bin/javadoc</javadocExecutable>
没有写。加上去就好了。
坑2:
在访问https://oss.sonatype.org/#stagingRepositories 的时候。找不到stagingRepositories
解决办法是,你要登陆,不登录看不到。