Tried to release my java package use Maven Central
Tags: java, opensource
Published: 2023-11-23 19:56:36

Modern software releases rely on package managers and centralized package repositories. So I chose using Maven Central to release my Java project [ConvertTxtToHtml](https://github.com/WangGithub0/ConvertTxtToHtml).

In order to using this Maven Central, I restructuring my whole project using Maven, and made all the Source Code Formatter, Linter and Testing work.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cbfh8konha7ena5kpfe7.png)



I tried to do the release according to the [doc](https://jenkov.com/tutorials/maven/publish-to-central-maven-repository.html) and [Central Repository document](https://central.sonatype.org/publish/publish-manual/):


1. I create account at Sonatype using my github account, and it automatically generate the namespace for me

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bsssb1xqtkfyd7o7nbb5.png)


1. I created and published PGP Keys according to these steps:
Step 1: Install GPG
Before you start, make sure you have GPG (GNU Privacy Guard) installed on your machine.

Step 2: Generate PGP Key Pair
Open a terminal and run the following command to generate a new PGP key pair:
```
gpg --gen-key
```
Follow the prompts to provide your information. This includes your name, email address, and a passphrase to secure your private key.

Step 3: List Keys
List your keys to find the key ID:

```
gpg --list-keys
```
Look for the line starting with "pub." The key ID is the long alphanumeric string following "rsa." It typically looks like this: 4096R/<your_key_id>.

Step 4: Export Public Key
Export your public key to a file:

```
gpg --export --armor <your_key_id> > public_key.asc
```

Step 5: Publish Public Key
Publish your public key to a key server. You can use a key server like keys.openpgp.org:

```
gpg --keyserver keys.openpgp.org --send-keys <your_key_id>
```

Step 6: Keep Your Private Key Secure
Your private key is stored in your GPG keyring. Keep it secure and do not share it.

Step 7: Sign Artifacts
When you build your JAR files, sign them using your private key:

```
gpg --sign --detach-sign -a your-artifact.jar
```


1. Prepare my Maven POM
```
<?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>

    <distributionManagement>
        <snapshotRepository>
            <id>ossrh</id>
            <url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
        </snapshotRepository>
          <repository>
            <id>ossrh</id>
            <url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
        </repository>
    </distributionManagement>
    <groupId>org.example</groupId>
    <artifactId>ConvertTxtToHtml</artifactId>
    <version>1.0</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.sonatype.plugins</groupId>
                <artifactId>nexus-staging-maven-plugin</artifactId>
                <version>1.6.13</version>
                <extensions>true</extensions>
                <configuration>
                    <serverId>ossrh</serverId>
                    <nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
                    <autoReleaseAfterClose>true</autoReleaseAfterClose>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.2.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>2.9.1</version>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <sourcepath>${project.basedir}/src/main/java</sourcepath>
                    <subpackages>com.converttxtmdtohtml</subpackages>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-gpg-plugin</artifactId>
                <version>1.5</version>
                <executions>
                    <execution>
                        <id>sign-artifacts</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>sign</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <!-- junit 5, unit test -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.3.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

```

1. Add my `~/.m2/settings.xml`
```
<settings>

  <localRepository>ConvertTxtToHtml</localRepository>

  <servers>
    <server>
      <id>ossrh</id>
      <username>your-jira-id</username>
      <password>your-jira-pwd</password>
    </server>
  </servers>


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

</settings>
```
After doing these, I tried to ` mvn clean deploy -e -X`, but got the error:[ERROR] Failed to execute goal org.sonatype.plugins:nexus-staging-maven-plugin:1.6.13:deploy (injected-nexus-deploy) on project ConvertTxtToHtml: Execution injected-nexus-deploy of goal org.sonatype.plugins:nexus-staging-maven-plugin:1.6.13:deploy failed: Nexus connection problem to URL [https://s01.oss.sonatype.org/ ]: 403 - Forbidden -> [Help 1]


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/enexy94209y1b9e0ue6n.png)


I also tried to run one file using `mvn gpg:sign-and-deploy-file -Durl=https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/ -DrepositoryId=ossrh -DpomFile=ossrh-test-1.2.pom -Dfile=ConvertTxtMdToHtml.java`

but still got the same 403 error


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1nueqfd2t1yh4iaj9iao.png)


After searching the similar error, I found [someone](https://stackoverflow.com/questions/27675557/releasing-to-maven-central) met this before, and need to ask for the permission, so I created an issue on [jira](https://issues.sonatype.org/browse/OSSRH-96969), hope I can get the feedback soon.


I also tried to add a tag for my project according to the [git document](https://git-scm.com/book/en/v2/Git-Basics-Tagging) and make a release for it.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lopejd6s8qsig00ym0t5.png)