文章目录
Maven项目上传公共仓库是我见过最麻烦的(相比npm和pypi)
文档:https://central.sonatype.org/pages/apache-maven.html
准备好maven项目 + Hello类
package com.mouday; public class Hello { public void sayHello(){ System.out.println("Hello"); } }
1. github上传代码
eg: https://github.com/mouday/hello-package
2. sonatype新建账号
https://issues.sonatype.org/secure/Signup!default.jspa
3. sonatype提交发布申请
等待审核,状态变为RESOLVED已处理
eg: https://issues.sonatype.org/browse/OSSRH-59604
如果是自己的域名,需要认证
设置txt记录后可以进行检查
$ nslookup -q=txt ossrh-59604.mouday.com ossrh-59604.mouday.com text = "https://issues.sonatype.org/browse/OSSRH-59604"
4. 生成pgp公钥
# 查看gpg是否安装 gpg --version # 生成key gpg --gen-key # 查询key gpg --list-keys # 公钥id发布到GPG服务器 gpg --keyserver hkp://pool.sks-keyservers.net --send-keys <公钥key> # 查询,可能需要等待一会 gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys <公钥key>
5. 配置settings.xml
~/.m2/settings.xml
<?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <servers> <server> <id>ossrh</id> <username>OSSRH-USERNAME</username> <password>OSSRH-PASSWORD</password> </server> </servers> <profiles> <profile> <id>jdk-1.8</id> <activation> <activeByDefault>true</activeByDefault> <jdk>1.8</jdk> </activation> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> <gpg.executable>gpg</gpg.executable> <gpg.passphrase>GPG-PASSWORD</gpg.passphrase> </properties> </profile> </profiles> </settings>
6. 配置pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mouday</groupId> <artifactId>hello-package</artifactId> <version>1.0</version> <name>hello-package</name> <url>https://github.com/mouday/hello-package</url> <description>a maven package demo for my first java</description> <licenses> <license> <name>The Apache Software License, Version 2.0</name> <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url> <distribution>repo</distribution> </license> </licenses> <scm> <connection>scm:https://github.com/mouday/hello-package.git</connection> <developerConnection>scm:https://github.com/mouday/hello-package.git</developerConnection> <url>https://github.com/mouday/hello-package</url> </scm> <developers> <developer> <name>mouday</name> <email>pengshiyuyx@gmail.com</email> <organization>mouday.com</organization> <timezone>+8</timezone> </developer> </developers> <distributionManagement> <snapshotRepository> <id>ossrh</id> <url>https://oss.sonatype.org/content/repositories/snapshots</url> </snapshotRepository> <repository> <id>ossrh</id> <name>Nexus Release Repository</name> <url>http://oss.sonatype.org/service/local/staging/deploy/maven2/</url> </repository> </distributionManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <executions> <execution> <goals> <goal>jar</goal> <goal>test-jar</goal> </goals> </execution> </executions> </plugin> <!-- Source --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin> <!-- Javadoc --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <configuration> <additionalJOptions> <additionalJOption>-Xdoclint:none</additionalJOption> </additionalJOptions> </configuration> <executions> <execution> <id>attach-javadocs</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <!-- GPG --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-gpg-plugin</artifactId> <executions> <execution> <id>sign-artifacts</id> <phase>verify</phase> <goals> <goal>sign</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.sonatype.plugins</groupId> <artifactId>nexus-staging-maven-plugin</artifactId> <version>1.6.3</version> <extensions>true</extensions> <configuration> <serverId>ossrh</serverId> <nexusUrl>https://oss.sonatype.org/</nexusUrl> <autoReleaseAfterClose>true</autoReleaseAfterClose> </configuration> </plugin> </plugins> </build> </project>
7. 发布
mvn clean deploy
如果报错可查看解决方式:
signing failed: Inappropriate ioctl for device
过大概2个小时就能在仓库中搜索到自己发布的项目了
第二天上午,我能在 https://mvnrepository.com/ 搜索到自己的项目了
等待很久之后,就可以在maven项目中使用了
<dependency> <groupId>com.mouday</groupId> <artifactId>hello-package</artifactId> <version>1.0</version> </dependency>
好难…
参考