Thursday, May 25, 2023

How Deploy project to the GitHub maven artifact repository and use it in another project

Common stuff


1. Create or edit c:\user\<username>\.m2\settings.xml

At minimum should look like 

<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">


<repositories>

<repository>

<id>central</id>

<url>https://repo1.maven.org/maven2</url>

</repository>

 

<repository>

<id>github-biz.daich</id>

<url>https://maven.pkg.github.com/BorisDaich/biz.daich</url>

<snapshots>

<enabled>true</enabled>

</snapshots>

</repository>

 

</repositories>


<servers>

<server>

<id>github-biz.daich</id>

<username>BorisDaich</username>

<password>XXXXXXX</password>

</server>

<server>

<id>github-cmd-runner-rerunner</id>

<username>BorisDaich</username>

<password>XXXXXXX</password>

</server>

 

</servers>

</settings>

 the password is something called Personal access tokens (classic)

can be created and managed here https://github.com/settings/tokens
click "user avatar" -> Settings -> Developer Settings -> Tokens (classic) 

for every repository on github here should be a separate <server> section
the <id> of the section must be same as <id> of the <repository> element in the pom.xml

the <repository> can go directly into the pom.xml of the project that uses the dependency
but the <server> element can be present only in the settings.xml of the maven installation or user ~/.m2/settings.xml

<repository>

<id>github-biz.daich</id>

<url>https://maven.pkg.github.com/BorisDaich/biz.daich</url>

<snapshots>

<enabled>true</enabled>

</snapshots>

</repository>


My naming convention for <id> elements: "github-<GitHubRepositoryName>" 

Same token for a user can be used for all repositories so it ok to copy the <server> section and just change name

How to use a dependency from GitHub Artifact Repo



repositories section to add to every pom.xml 

<repositories>

         <repository>

         <id>github-biz.daich</id>

         <url>https://maven.pkg.github.com/BorisDaich/biz.daich</url>

        </repository>

         <repository>

         <id>github-cmd-runner-rerunner</id>

         <url>https://maven.pkg.github.com/BorisDaich/cmd-runner-rerunner</url>

         </repository>

</repositories>




How to publish a project to a repository


1. Make sure that you have c:\user\<username>\.m2\settings.xml
2. add to the pom.xml 

<distributionManagement>

<repository>

<id>github-{GitHubRepositoryName}</id>

<url>https://maven.pkg.github.com/BorisDaich/{GitHubRepositoryName}</url>

</repository>

</distributionManagement>


notice that you need to replace {GitHubRepositoryName}

and make sure that in the settings.xml you have <server> section with this ID


>> mvn clean deploy 

will deploy the project to the github repo

No comments:

Followers