目次 †
概要 †GitlabをMavenリポジトリとして使用する方法について記載する。 ライブラリプロジェクト †
ビルド前 ├ build.gradle ビルド後 ├ build.gradle ライブラリを使用する側のプロジェクト †ビルド前 ├ build.gradle 環境設定 †SSH鍵の作成 †秘密鍵: ~/.ssh/gitlab_xxx@example.com、 公開鍵: ~/.ssh/gitlab_xxx@example.com.pub として生成 ssh-keygen -t rsa -C xxx@example.com gitlabに作成した公開鍵を登録 †cat gitlab_xxx@example.com.pub # Macの場合 cat gitlab_xxx@example.com.pub | pbcopy # そのままクリックボードにコピー ※取得した内容をgitlabに登録する。( https://gitlab.com/profile/keys ) ~/.ssh/configの追記 †Host gitlab.com User xxx@example.com IdentityFile ~/.ssh/gitlab_xxx@example.com ssh-agent に秘密鍵を追加する †# 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 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;
}
}
build.gradleの作成 †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 配下にビルドされる gitlabにプッシュ †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 |