デフォルトでは、GitHubはパッケージはそのままの名前で既存のリポジトリに公開されます。 たとえば、GitHubはOWNER/test
というリポジトリ内のcom.example:test
という名前のパッケージを公開します。
GitHub Packages への認証を行う
GitHub Packagesでパッケージを公開、インストール、削除するにはアクセストークンが必要です。 GitHub Packagesに直接、あるいはGitHub APIでユーザ名で認証を受けるのに、個人のアクセストークンが利用できます。 個人トークンを作成する際には、必要に応じて様々なスコープをトークンに割り当てできます。
認証を
GitHub Actionsワークフローを使ってGitHub Packagesに対して行うには、GITHUB_TOKEN
を使わなければなりません。
個人アクセストークンでの認証
GitHub Packages内でパッケージを公開及びインストールするためには、適切なスコープで個人アクセストークンを使わなければなりません。 詳しい情報については「GitHub Packagesについて」を参照してください。
~/.m2/settings.xmlファイルを編集して個人アクセストークンを含めることで、Apache MavenでGitHub Packagesの認証を受けられます。 ~/.m2/settings.xmlファイルがないなら新しく作成してください。
servers
タグの中に、子としてserver
タグをid
付きで追加し、USERNAMEをGitHubのユーザ名で、TOKENを個人アクセストークンで置き換えてください。
repositories
の中で、リポジトリのid
をクレデンシャルを含むserver
タグに追加したid
にマッピングして、リポジトリを設定してください。 Replace HOSTNAME with the host name of your GitHub Enterprise Server instance, , and OWNER with the name of the user or organization account that owns the repository. 大文字はサポートされていないため、仮にGitHubのユーザあるいはOrganization名が大文字を含んでいても、リポジトリオーナーには小文字を使わなければなりません。
複数のリポジトリとやりとりをしたい場合には、それぞれのリポジトリをrepositories
タグの子の個別のrepository
に追加し、それぞれのid
をservers
タグのクレデンシャルにマッピングできます。
GitHub PackagesはApache MavenのSNAPSHOT
バージョンをサポートしています。 SNAPSHOT
成果物をダウンロードするためにGitHub Packagesリポジトリを使うには、使用するプロジェクトのPOM中もしくは~/.m2/settings.xmlファイルでSNAPSHOTSを有効にしてください。
パッケージの作成に関する詳しい情報についてはmaven.apache.orgのドキュメンテーションを参照してください。
<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">
<activeProfiles>
<activeProfile>github</activeProfile>
</activeProfiles>
<profiles>
<profile>
<id>github</id>
<repositories>
<repository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
</repository>
<repository>
<id>github</id>
<url>https://maven.HOSTNAME/OWNER/*</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
<servers>
<server>
<id>github</id>
<username>USERNAME</username>
<password>TOKEN</password>
</server>
</servers>
</settings>
たとえば、以下のOctodogAppとOctocatAppは同じリポジトリに公開されます。
<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">
<activeProfiles>
<activeProfile>github</activeProfile>
</activeProfiles>
<profiles>
<profile>
<id>github</id>
<repositories>
<repository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
</repository>
<repository>
<id>github</id>
<url>https://maven.pkg.github.com/OWNER/*</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
<servers>
<server>
<id>github</id>
<username>USERNAME</username>
<password>TOKEN</password>
</server>
</servers>
</settings>
GITHUB_TOKEN
での認証
GitHub Actionsワークフローを使っているなら、GITHUB_TOKEN
を使い、個人アクセストークンを保存したり管理したりする必要なくGitHub Packages内のパッケージの公開や利用ができます。 詳しい情報については「GITHUB_TOKEN
での認証」を参照してください。
パッケージを公開する
デフォルトでは、GitHubはパッケージはそのままの名前で既存のリポジトリに公開されます。 たとえば、GitHubはOWNER/test
というリポジトリ内のcom.example:test
という名前のパッケージを公開します。
同じリポジトリに複数のパッケージを公開したい場合には、そのリポジトリのURLをpom.xmlファイルの<distributionManagement>
要素に含めてください。 GitHub は、このこのフィールドを元にしてリポジトリを照合します。 リポジトリ名もdistributionManagement
要素の一部なので、複数のパッケージを同じリポジトリに公開するための追加手順はありません。
パッケージの作成に関する詳しい情報についてはmaven.apache.orgのドキュメンテーションを参照してください。
-
Edit the
distributionManagement
element of the pom.xml file located in your package directory, replacing HOSTNAME with the host name of your GitHub Enterprise Server instance,OWNER
with the name of the user or organization account that owns the repository andREPOSITORY
with the name of the repository containing your project.If your instance has subdomain isolation enabled:
<distributionManagement> <repository> <id>github</id> <name>GitHub OWNER Apache Maven Packages</name> <url>https://maven.HOSTNAME/OWNER/REPOSITORY</url> </repository> </distributionManagement>
If your instance has subdomain isolation disabled:
<distributionManagement> <repository> <id>github</id> <name>GitHub OWNER Apache Maven Packages</name> <url>https://HOSTNAME/_registry/maven/OWNER/REPOSITORY</url> </repository> </distributionManagement>
-
パッケージを公開します。
$ mvn deploy
パッケージを公開した後は、GitHub上でそのパッケージを見ることができます。 詳しい情報については「パッケージの表示」を参照してください。
パッケージをインストールする
GitHub PackagesからApache Mavenパッケージをインストールするには、pom.xmlファイルを編集してパッケージを依存関係として含めてください。 複数のリポジトリからパッケージをインストールしたい場合は、それぞれについてrepository
タグを追加してください。 プロジェクト内でのpom.xmlファイルの利用に関する詳しい情報については、Apache Mavenドキュメンテーション中の「 Introduction to the POM」を参照してください。
-
GitHub Packagesに認証を受けてください。 詳しい情報については「GitHub Packagesへの認証を行う」を参照してください。
-
パッケージの依存関係をプロジェクトのpom.xmlファルの
dependencies
要素に追加し、com.example:test
をパッケージで置き換えてください。<dependencies> <dependency> <groupId>com.example</groupId> <artifactId>test</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> </dependencies>
-
パッケージをインストールします。
$ mvn install