技術ネタはQiitaに移りました。壁もどこぞに。

Github を Maven リポジトリとして使う

調べてみると、いろいろとハマりどころがあったのでメモ。

事前に用意するもの

まず、適当な Java ファイル。今回はこれをモジュールとして扱うものとする。

public class Hoge {
    public static void main(String[] args) {
        System.out.println("Hoge module");
    }
}

そして、Maven は 3.x を使用する。今回使用したのは以下。

$ mvn --version
Apache Maven 3.2.5 (12a6b3acb947671f09b81f49094c53f426d8cea1; 2014-12-15T02:29:23+09:00)
Maven home: /Applications/Developments/apache-maven-3.2.5
Java version: 1.6.0_65, vendor: Apple Inc.
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: ja_JP, platform encoding: SJIS
OS name: "mac os x", version: "10.10.2", arch: "x86_64", family: "mac"

Github リポジトリの作成

Maven リポジトリとして使用する、Github リポジトリを作成しておく。なお、ブランクリポジトリだと site-maven-plugin の実行に失敗する。事前に README.md などをコミットしておく。

POM ファイルを用意

ネットで見つかるサンプルだと、言っていることがまちまちだったりして、けっこう設定にクセがあるように感じたので、いくらか紹介しつつ最後に使用した pom.xml を載せておく。

  • github.global.host プロパティ
    • Github API のホスト名、または URL を設定する。
  • github.global.userName プロパティ
    • Github ユーザー名(または、メールアドレス)。
  • github.global.password プロパティ
  • github.global.repositoryName プロパティ
  • github.global.repositoryOwner プロパティ
    • Github ユーザー名。
    • または Organization 名。
  • distributionManagement
    • モジュールの出力ディレクトリを設定する。
  • maven-deploy-plugin/altDeploymentRepository
    • distributionManagement にでてきた id と、url を使用する。
  • site-maven-plugin/repositoryName
    • github.global.repositoryName プロパティと同じものを設定する。省略不可。
    • ${github.global.repositoryOwner} を設定する。
  • site-maven-plugin/repositoryOwner
    • github.global.repositoryOwner プロパティと同じものを設定する。省略不可。
    • ${github.global.repositoryOwner} を設定する。
<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.yo1000</groupId>
    <artifactId>hoge-module</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Maven Quick Start Archetype</name>
    <url>http://maven.apache.org</url>
    
    <properties>
        <!-- global.host、他、プロパティを設定する場合は不要 -->
        <!-- <github.global.server>github</github.global.server> -->
        
        <!-- Github の場合に使用 -->
        <github.global.host>api.github.com</github.global.host>
        <!-- GithubEnterprise の場合に使用、{{hostname}} を適宜書き換える -->
        <!-- <github.global.host>https://{{hostname}}/api/v3/</github.global.host> -->
        
        <!-- Github リポジトリユーザー名 -->
        <github.global.userName>{{ユーザー名}}</github.global.userName>
        
        <!-- Github リポジトリユーザーのパスワード -->
        <github.global.password>{{パスワード}}</github.global.password>
        
        <!-- Github リポジトリ名 -->
        <github.global.repositoryName>mvn-repos</github.global.repositoryName>
        
        <!-- Github リポジトリユーザー名、または Organization 名 -->
        <github.global.repositoryOwner>{{ユーザー名 or Organization 名}}</github.global.repositoryOwner>
    </properties>
  
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <distributionManagement>
        <repository>
            <id>internal.repos</id>
            <name>Temporary Staging Repository</name>
            <url>file://${project.build.directory}/hoge-mod</url>
        </repository>
    </distributionManagement>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.1</version>
                <configuration>
                    <altDeploymentRepository>internal.repos::default::file://${project.build.directory}/hoge-mod</altDeploymentRepository>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.github.github</groupId>
                <artifactId>site-maven-plugin</artifactId>
                <version>0.11</version>
                <configuration>
                    <!-- Git コミットメッセージ -->
                    <message>Maven artifacts for ${project.version}</message>
                    <noJekyll>true</noJekyll>
                    <!-- distributionManagement の url と一致させる -->
                    <outputDirectory>${project.build.directory}/hoge-mod</outputDirectory>
                    <!-- リモートブランチ名 -->
                    <branch>refs/heads/hoge-mod-branch</branch>
                    <includes><include>**/*</include></includes>
                    <!-- Github リポジトリ名 -->
                    <repositoryName>${github.global.repositoryName}</repositoryName>
                    <!-- Github リポジトリユーザー名 -->
                    <repositoryOwner>${github.global.repositoryOwner}</repositoryOwner>
                </configuration>
                <executions>
                    <!-- run site-maven-plugin's 'site' target as part of the build's normal 'deploy' phase -->
                    <execution>
                        <goals>
                            <goal>site</goal>
                        </goals>
                        <phase>site</phase>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Github でユーザープロファイルを確認

ここが一番のクセモノ。使用するユーザー名のプロファイル情報に Name が設定されているかどうかを確認する。ここに Name が設定されていないと site-maven-plugin は正常に動作しない。

設定は、https://github.com/settings/profile から。

f:id:Yoichi-KIKUCHI:20150318021944p:plain

Maven 実行

ここまで準備ができれば、Maven を実行できる。

$ mvn clean deploy site
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Quick Start Archetype 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]

...

[INFO]
[INFO] --- site-maven-plugin:0.11:site (default) @ hoge-module ---
[INFO] Creating 12 blobs
[INFO] Creating tree with 13 blob entries
[INFO] Creating commit with SHA-1: aa31ccc2cf0d701420ddf4dea23299fb9fb3278a
[INFO] Creating reference refs/heads/hoge-mod-branch starting at commit aa31ccc2cf0d701420ddf4dea23299fb9fb3278a
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 14.949 s
[INFO] Finished at: 2015-03-18T01:30:01+09:00
[INFO] Final Memory: 19M/81M
[INFO] ------------------------------------------------------------------------

リポジトリの利用確認

適当なプロジェクトで先ほどの成果物を、Github 経由で参照するように依存関係を設定したら、また Maven コマンドを実行してみる。なお、リポジトリ URL は https://github.com/{username}/{repository-name}/raw/{branch-name}/ のようになる。

最後に利用側の POM サンプルと、実行結果を載せておく。

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.yo1000</groupId>
    <artifactId>hoge-module-use</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Maven Quick Start Archetype</name>
    <url>http://maven.apache.org</url>
    
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.yo1000</groupId>
            <artifactId>hoge-module</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>github-maven</id>
            <url>https://github.com/{{username}}/{{repository-name}}/raw/{{branch-name}}/</url>
        </repository>
    </repositories>
</project>
$ mvn compile
[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Quick Start Archetype 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: https://github.com/

...

[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hoge-module-use ---
[INFO] No sources to compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.310 s
[INFO] Finished at: 2015-03-18T03:01:03+09:00
[INFO] Final Memory: 7M/81M
[INFO] ------------------------------------------------------------------------

動いた!けっこう便利に使えそう。なお、POM 内に直接パスワードを記述するのが嫌な場合は、もちろん API トークンなども使用できる模様。

参考:https://github.com/github/maven-plugins