GitlabをMavenリポジトリとして使用する方法について記載する。
URL(例) | 説明 |
https://gitlab.com/username/java_lib.git | Mavenリポジトリとして公開するライブラリプロジェクト |
ビルド前
├ build.gradle
└ src
└ main
└ java
└ com/example
└ SampleUtil.java
ビルド後
├ build.gradle
├ maven
│ └ com/example/sample
│ ├ 1.0.0
│ │ ├ sample-1.0.0.jar
│ │ ├ sample-1.0.0.pom
│ │ └ :
│ └ maven-metadata.xml
└ src
└ main
└ java
└ com/example
└ SampleUtil.java
ビルド前
├ build.gradle
└ src
└ main
└ java
└ com/example
└ UseSample.java
秘密鍵: ~/.ssh/gitlab_xxx@example.com、 公開鍵: ~/.ssh/gitlab_xxx@example.com.pub として生成
ssh-keygen -t rsa -C xxx@example.com
cat gitlab_xxx@example.com.pub # Macの場合 cat gitlab_xxx@example.com.pub | pbcopy # そのままクリックボードにコピー
※取得した内容をgitlabに登録する。( https://gitlab.com/profile/keys )
Host gitlab.com User xxx@example.com IdentityFile ~/.ssh/gitlab_xxx@example.com
# ssh-agent起動 eval `ssh-agent` # 登録されている秘密鍵の一覧を確認 ssh-add -l # 秘密鍵を登録 ssh-add ~/.ssh/gitlab_xxx@example.com # 登録されている秘密鍵の一覧を確認 ssh-add -l # 接続確認 ssh -T git@gitlab.com
事前に gitlab にプロジェクト( java_lib )を作成しておく。
git clone git@gitlab.com:username/java_lib.git
src/main/java/com/example/SampleUtil.java
package com.example; public class SampleUtil { public static String hello(String name){ return "Hello " + name; } }
apply plugin: 'idea' apply plugin: 'java' apply plugin: 'maven' def defaultEncoding = 'UTF-8' [compileJava, compileTestJava]*.options*.encoding = defaultEncoding sourceCompatibility = 1.8 targetCompatibility = 1.8 repositories { //maven { url System.getenv('HOME') + '/.m2/repository' } mavenCentral() } dependencies { testCompile 'junit:junit:4' } // グループID、アーティファクトID、バージョンを指定 group = 'com.example' archivesBaseName = 'samplelib' version = '1.0.0' // maven構成の生成用タスクを追加 uploadArchives.repositories.mavenDeployer { repository(url: "file:${projectDir}/maven") }
gradle uploadArchives
※ プロジェクト maven 配下にビルドされる
git add . git commit -m 'first commit' git push
build.gradle
apply plugin: 'idea' apply plugin: 'java' apply plugin: 'maven' //apply plugin: 'git-repo' apply plugin: 'application' def defaultEncoding = 'UTF-8' [compileJava, compileTestJava]*.options*.encoding = defaultEncoding sourceCompatibility = 1.8 targetCompatibility = 1.8 mainClassName = 'com.example.UseSample' buildscript { repositories { jcenter() //maven { url "https://github.com/layerhq/releases-gradle/raw/master/releases" } } dependencies { //classpath 'com.layer:gradle-git-repo-plugin:2.0.2' } } repositories { //maven { url System.getenv('HOME') + '/.m2/repository' } mavenCentral() /* プライベートリポジトリ上のライブラリを利用する場合 (利用するプラグインによって書き方は様々) mavenプラグインを利用する場合は素の maven-metadata.xml を読めるURLのプレフィックスを指定する */ //git("https://gitlab.com/username/java_lib.git", "com.example.samplelib", "master", "maven") //git("git@gitlab.com:username/java_lib.git", "com.example.samplelib", "master", "maven") //maven { url "http://localhost:8080/sample-group1/sample1/raw/master/maven" } maven { url "git@gitlab.com:username/java_lib.git/raw/master/maven" } /* maven { url "https://gitlab.com/username/java_lib/raw/master/maven" //credentials { // username "username" // password "password" //} } */ } dependencies { testCompile 'junit:junit:4' // 先程作成したライブラリのグループID、アーティファクトID、バージョンを追加 compile "com.example:samplelib:1.0.0" //compile fileTree(dir: "libs", includes: ['*.jar']) } // ファイルのコピー先(カレントからの相対PATH) def dependJarCopyTo = 'libs' // コピー先にあるファイルをいったん削除 task delDependJar << { delete dependJarCopyTo } // 依存するライブラリjarを指定したディレクトリにコピーする task getDependJar(dependsOn: delDependJar) { doLast { configurations.compile.each { def fromJarFile = it.absolutePath copy { from fromJarFile into dependJarCopyTo } } println "----- 以下のjarファイルを取得しました -----" FileCollection copyFiles = files { file(dependJarCopyTo).listFiles() } copyFiles.each {File file -> println file.name} } }
src/main/java/com/example/UseSample.java
package com.example; import com.example.SampleUtil; public class UseSample { public static void main(String[] args){ System.out.println(SampleUtil.hello("Taro")); } }
gradle build
gradle run