LoginSignup
43
32

More than 5 years have passed since last update.

GitHubにMavenリポジトリを作ってしまってライブラリを公開してしまおう

Last updated at Posted at 2016-05-12

Javaは作ったライブラリをどうやって配布するか悩みの種です。jarファイルを作って読み込むのもダサいし、
Maven Centralに登録するのもハードル高そうだし、どうせならそのままGitHubで公開したいです。

maven-pluginsを使えばGitHubに置いて公開できるっぽかったのでやってみました。

HomeBrewで入れてるmavenは3.3.9

$ mvn -v
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T01:41:47+09:00)
Maven home: /usr/local/Cellar/maven/3.3.9/libexec
Java version: 1.8.0_73, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_73.jdk/Contents/Home/jre
Default locale: ja_JP, platform encoding: UTF-8
OS name: "mac os x", version: "10.11.4", arch: "x86_64", family: "mac"

access tokenを作る

https://github.com/settings/tokens でdeployで使用するaccess tokenを作成します。
repoの権限にチェックが入っていれば大丈夫なはず

認証情報を設定

認証情報はコミットしたくないのでローカルで管理
$vim ~/.m2/settings.xml

~/.m2/settings.xml
<settings>
  <servers>
    <server>
      <id>github</id>
      <username>username@example.com</username>
      <password>abcdefghijk(access tokenをいれる)</password>
    </server>
  </servers>
</settings>

passwordには普通にパスワード入れるって書いてあったけど、うまく動作しなかったのでさっき取得したaccess tokenを入れる

pom.xmlの設定

pom.xml
<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>

    <groupId>holiday-jp</groupId>
    <artifactId>holiday-jp</artifactId>
    <!-- バージョン番号 -->
    <version>1.0</version>
    <packaging>jar</packaging>

    <name>holiday-jp</name>
    <url>https://github.com/narikei/holiday-jp</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- 認証情報読み込み -->
        <github.global.server>github</github.global.server>
    </properties>

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

    <build>
        <plugins>
            <plugin>
                <groupId>com.github.github</groupId>
                <artifactId>site-maven-plugin</artifactId>
                <version>0.12</version>
                <configuration>
                    <!-- Git コミットメッセージ -->
                    <message>Maven artifacts for ${project.version}</message>
                    <noJekyll>true</noJekyll>
                    <!-- distributionManagement の url と一致させる -->
                    <outputDirectory>${project.build.directory}/mvn-repo</outputDirectory>
                    <!-- リモートブランチ名 -->
                    <branch>refs/heads/mvn-repo</branch>
                    <includes><include>**/*</include></includes>
                    <!-- Github リポジトリ名 -->
                    <repositoryName>${repositoryName}</repositoryName>
                    <!-- Github リポジトリユーザー名 -->
                    <repositoryOwner>${ownerName}</repositoryOwner>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>site</goal>
                        </goals>
                        <phase>deploy</phase>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

リポジトリ名、リポジトリユーザー名などを設定、versionは後で設定するtag名と合わせる必要があります。
この設定でmvn-repoブランチにアップロードされるようになります。

デプロイ実行

$ mvn clean deploy
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building holiday-jp 1.0
[INFO] ------------------------------------------------------------------------
[INFO] 

・
・
・

[INFO] Updating reference refs/heads/mvn-repo from a1d4648e3aa42b6cf247614fd38379ed0ae37a9d to c4cc9eee11adafa0f2de71eae88ee9c83f1647d1
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 38.698 s
[INFO] Finished at: 2016-05-12T12:25:24+09:00
[INFO] Final Memory: 20M/218M
[INFO] ------------------------------------------------------------------------

versionをタグ付け

pom.xmlで指定したversionを、デプロイのコミットにtag付します

$ git tag 1.0 c4cc9eee11adafa0f2de71eae88ee9c83f1647d1
$ git push origin 1.0

出来上がったのがこちらです

https://github.com/narikei/holiday-jp
https://github.com/narikei/holiday-jp/blob/master/pom.xml にmavenの設定が入っています
https://github.com/narikei/holiday-jp/tree/mvn-repo にデプロイされたファイルが入っています

使う側からライブラリを読み込むときは

pom.xml
<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">
    <repositories>
        <repository>
            <id>holiday-jp</id>
            <url>https://raw.github.com/narikei/holiday-jp/mvn-repo/</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>holiday-jp</groupId>
            <artifactId>holiday-jp</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>
</project>

urlをhttps://raw.github.com/${ownerName}/${repositoryName}/${branchName}/という形式で読み込んでくれます
versionにはつけたtag名を入力します

おわり

npmとかRubyGemsとかに慣れきっててJava辛いけど、
適当なオレオレライブラリ作るにはこれで結構便利かも

参考

http://oinume.hatenablog.com/entry/wp/527
http://yo1000.hateblo.jp/entry/2015/03/18/030358

43
32
2

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
43
32