DEV Community

WangGithub0
WangGithub0

Posted on

Tried to release my java package use Maven Central

Modern software releases rely on package managers and centralized package repositories. So I chose using Maven Central to release my Java project 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

I tried to do the release according to the doc and Central Repository document:

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

Image description

  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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Look for the line starting with "pub." The key ID is the long alphanumeric string following "rsa." It typically looks like this: 4096R/.

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

gpg --export --armor <your_key_id> > public_key.asc
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  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>

Enter fullscreen mode Exit fullscreen mode
  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>
Enter fullscreen mode Exit fullscreen mode

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

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

After searching the similar error, I found someone met this before, and need to ask for the permission, so I created an issue on jira, hope I can get the feedback soon.

I also tried to add a tag for my project according to the git document and make a release for it.

Image description

Top comments (2)

Collapse
 
khmarbaise profile image
Karl Heinz Marbaise

Unfortunately you are using ancient old plugin versions... please check here: maven.apache.org/plugins/

Collapse
 
wanggithub0 profile image
WangGithub0

I'll try it, thanks a lot