LoginSignup
2
3

More than 5 years have passed since last update.

GitHubのプライベートリポジトリにホスティングされているMavenリポジトリを使う

Last updated at Posted at 2019-02-27

GitHubのプライベートリポジトリでMavenリポジトリをホスティングするのはサンプルが多いのですんなりいったが、それを別プロジェクトから参照するときにハマったのでメモ。

settings.xml

このファイルがミソです。
この設定はGitHubにmavenリポジトリをデプロイするときにはid、username、passwordがあればよいですが、プライベートリポジトリからmavenリポジトリを参照するためにはconfigurationタグの内容を追加して、別途ベーシック認証の情報も与える必要があります。

ちなみにidはpom.xmlで同じものを指定する必要があります。

settings.xml
<settings>
  <servers>
    <server>
      <id>github-repo</id>
      <username>${github_user_name}</username>
      <password>${github_access_token}</password>
      <configuration>
        <httpHeaders>
          <property>
            <name>Authorization</name>
            <value>Basic ${base64 username:token}</value>
          </property>
        </httpHeaders>
      </configuration>
    </server>
  </servers>
</settings>

<value>にはユーザー名とトークンを:(コロン)で接続したものをbase64エンコードしたものを指定します。

pom.xml

pom.xml側にはリポジトリの追加を行います。
settings.xmlで指定したidと同じものをrepositoryに指定します。

pom.xml
<repositories>
  <repository>
    <id>github-repo</id>
    <url>https://raw.githubusercontent.com/user/repo/mvn-repo/</url>
  </repository>
</repositories>

ちなみにホスティングしているのがライブラリではなく、独自のMavenプラグインの場合は

pom.xml
<pluginRepositories>
  <pluginRepository>
    <id>github-repo</id>
    <url>https://raw.githubusercontent.com/user/repo/mvn-repo/</url>
  </pluginRepository>
</pluginRepositories>

の様にpluginRepositoriesとpluginRepositoryに変えます。
あとはdependencyを書くなり、pluginタグを書くなりします。

ちなみに

Gradleの場合だと

build.gradle
allprojects {
    repositories {
        maven {
            url "https://raw.githubusercontent.com/user/repo/mvn-repo"
            credentials {
                username ${username}
                password ${token}
            }
            authentication {
                basic(BasicAuthentication)
            }
        }
    }
}

みたいに書くと同じことが出来ます。
こっちは他にも情報があるのでハマる人はいないでしょう。

個人的にはGradleのほうが楽だなぁ。

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3