GradleでJenkins Shared Librariesを作る

# 概要

* GradleでJenkinsの共有ライブラリのプロジェクトを作る。
* TDDできるようにテスト実行可能にする。

[Gradle](https://docs.gradle.org/)
[Jenkins Shared Libraries](https://jenkins.io/doc/book/pipeline/shared-libraries/)

# 自分の環境

Windows 8
Gradle 4.4
Java 1.8

# 手順

gradle initでgroovy-libraryのひな形を作る。

```bash:実行コマンド
gradle init --type groovy-library
```


```bash:作られたファイル群
$ find
.
./.gradle
./.gradle/4.4
./.gradle/4.4/fileChanges
./.gradle/4.4/fileChanges/last-build.bin
./.gradle/4.4/fileHashes
./.gradle/4.4/fileHashes/fileHashes.bin
./.gradle/4.4/fileHashes/fileHashes.lock
./.gradle/4.4/taskHistory
./.gradle/4.4/taskHistory/taskHistory.bin
./.gradle/4.4/taskHistory/taskHistory.lock
./.gradle/buildOutputCleanup
./.gradle/buildOutputCleanup/buildOutputCleanup.lock
./.gradle/buildOutputCleanup/cache.properties
./.gradle/buildOutputCleanup/outputFiles.bin
./build.gradle
./gradle
./gradle/wrapper
./gradle/wrapper/gradle-wrapper.jar
./gradle/wrapper/gradle-wrapper.properties
./gradlew
./gradlew.bat
./settings.gradle
./src
./src/main
./src/main/groovy
./src/main/groovy/Library.groovy
./src/test
./src/test/groovy
./src/test/groovy/LibraryTest.groovy

```

テストも実行できる。

```bash:
$ gradle test
:compileJava NO-SOURCE
:compileGroovy
Download https://jcenter.bintray.com/org/codehaus/groovy/groovy-all/2.4.12/groovy-all-2.4.12.pom
Download https://jcenter.bintray.com/org/codehaus/groovy/groovy-all/2.4.12/groovy-all-2.4.12.jar
:processResources NO-SOURCE
:classes
:compileTestJava NO-SOURCE
:compileTestGroovy
Download https://jcenter.bintray.com/org/spockframework/spock-core/1.0-groovy-2.4/spock-core-1.0-groovy-2.4.pom
Download https://jcenter.bintray.com/junit/junit/4.12/junit-4.12.pom
Download https://jcenter.bintray.com/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.pom
Download https://jcenter.bintray.com/org/hamcrest/hamcrest-parent/1.3/hamcrest-parent-1.3.pom
Download https://jcenter.bintray.com/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar
Download https://jcenter.bintray.com/junit/junit/4.12/junit-4.12.jar
Download https://jcenter.bintray.com/org/spockframework/spock-core/1.0-groovy-2.4/spock-core-1.0-groovy-2.4.jar
:processTestResources NO-SOURCE
:testClasses
:test
BUILD SUCCESSFUL in 31s
3 actionable tasks: 3 executed
```

ただし、これだとJenkins Shared Libraryのディレクトリ構成と違うので修正する。

```text:Jenkins共有ライブラリのディレクトリ構成
(root)
+- src                     # Groovy source files
|   +- org
|       +- foo
|           +- Bar.groovy  # for org.foo.Bar class
+- vars
|   +- foo.groovy          # for global 'foo' variable
|   +- foo.txt             # help for 'foo' variable
+- resources               # resource files (external libraries only)
|   +- org
|       +- foo
|           +- bar.json    # static helper data for org.foo.Bar
```

なので、コマンドラインで構成変更する。

```bash:実行コマンド
$ mv src/* .
$ rm -r src
$ mv main src
$ mkdir vars resources
```

これだけだと、:test NO-SOURCENO-SOURCE になってしまう。

```
$ gradle clean test
:clean UP-TO-DATE
:compileJava NO-SOURCE
:compileGroovy NO-SOURCE
:processResources NO-SOURCE
:classes UP-TO-DATE
:compileTestJava NO-SOURCE
:compileTestGroovy NO-SOURCE
:processTestResources NO-SOURCE
:testClasses UP-TO-DATE
:test NO-SOURCE

BUILD SUCCESSFUL in 2s
1 actionable task: 1 up-to-date
```

そこで、build.gradleにsourceSetsを追記し、ソースとテストコードの場所を指定する。

```groovy:build.gradle
/*
 * This build file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Groovy project to get you started.
 * For more details take a look at the Groovy Quickstart chapter in the Gradle
 * user guide available at https://docs.gradle.org/4.4/userguide/tutorial_groovy_projects.html
 */

// Apply the groovy plugin to add support for Groovy
apply plugin: 'groovy'

// In this section you declare where to find the dependencies of your project
repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

dependencies {
    // Use the latest Groovy version for building this library
    compile 'org.codehaus.groovy:groovy-all:2.4.12'

    // Use the awesome Spock testing and specification framework
    testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
}

sourceSets {
    main {
        groovy {
            srcDirs = ['src']
        }
    }

    test {
        groovy {
            srcDirs = ['test']
        }
    }
}
```

無事テストが実行可能となる。

```
$ gradle clean test
:clean UP-TO-DATE
:compileJava NO-SOURCE
:compileGroovy
:processResources NO-SOURCE
:classes
:compileTestJava NO-SOURCE
:compileTestGroovy
:processTestResources NO-SOURCE
:testClasses
:test

BUILD SUCCESSFUL in 8s
4 actionable tasks: 3 executed, 1 up-to-date
```

Jenkins Shared Libraryで、サードパーティのライブラリを使用する場合は
以下のようにGrapeを用いる。

```groovy
@Grab('org.yaml:snakeyaml:1.18')
import org.yaml.snakeyaml.Yaml
```

GrapeとGradleを両方使用する場合にivyが無いといって、gradleのビルドがこけてしまう。
この対策として、build.gradleにivyの設定を追加してやる。

```groovy:build.gradle
/*
 * This build file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Groovy project to get you started.
 * For more details take a look at the Groovy Quickstart chapter in the Gradle
 * user guide available at https://docs.gradle.org/4.4/userguide/tutorial_groovy_projects.html
 */

// Apply the groovy plugin to add support for Groovy
apply plugin: 'groovy'

// In this section you declare where to find the dependencies of your project
repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
    mavenCentral()
}

configurations {
    ivy
}

dependencies {
    // Use the latest Groovy version for building this library
    compile 'org.codehaus.groovy:groovy-all:2.4.12'
    ivy 'org.apache.ivy:ivy:2.4.0'

    // Use the awesome Spock testing and specification framework
    testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
}
    
sourceSets {
    main {
        groovy {
            srcDirs = ['src']
        }
    }

    test {
        groovy {
            srcDirs = ['test']
        }
    }
}

tasks.withType(GroovyCompile) {
    groovyClasspath += configurations.ivy
}

```

できあがり。これで、Jenkins Shared LibraryをTDDできる。

# あとがき

```bash
gradle init --type jenkins-shared-library
```

これ一発でプロジェクト作れたらいいのになぁ。