かんがるーさんの日記

最近自分が興味をもったものを調べた時の手順等を書いています。今は Spring Boot をいじっています。

Spring Boot でログイン画面 + 一覧画面 + 登録画面の Webアプリケーションを作る ( その18 )( Spring Boot Gradle plugin で Spring Boot のバージョン番号を指定する )

概要

Spring Boot でログイン画面 + 一覧画面 + 登録画面の Webアプリケーションを作る ( その17 )( unittest モードを作成してユニットテストのスピードアップ ) の続きです。

ソフトウェア一覧

参考にしたサイト

手順

なぜ気づいたのか?

Spring FrameworkSlideShare を見つけたので中を見ていたところ、「CORS v4.2」のページに "@CrossOrigin" というアノテーションが記載されていました。

Spring 4 Web App - SlideShare
http://www.slideshare.net/rstoya05/spring-4-web-app

CORS 関連の実装に興味がわき、CORS + Spring Boot で検索してどんな記事があるのか調べていたところ以下の記事を見つけました。

Enabling Cross Origin Requests for a RESTful Web Service
https://spring.io/guides/gs/rest-service-cors/

この記事の「Build with Gradle」を見ると以下のソースが。。。

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.2.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'

jar {
    baseName = 'gs-rest-service-cors'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("com.fasterxml.jackson.core:jackson-databind")
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.3'
}

spring-boot-gradle-plugin で 1.2.2.RELEASE のバージョン番号を指定しているけど、spring-boot-starter-web では何もバージョン番号を指定していない!?

え、そんなことが出来るの???

Spring Boot Gradle plugin とは

Google で "spring-boot-gradle-plugin" で検索すると、以下のページがヒットします。

Spring Boot Reference Guide - 59. Spring Boot Gradle plugin
http://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-gradle-plugin.html

Spring Boot Reference Guide は困った時に参照するくらいで全部に目を通していなかったのですが、まさかこんなページがあるとは。。。 中を読んでみると、確かにバージョン番号は空でよいと書かれていますね。以下のページに書かれているものはバージョン番号を記載しなくてもよいそうです。

Spring Boot Reference Guide - Appendix E. Dependency versions
http://docs.spring.io/spring-boot/docs/current/reference/html/appendix-dependency-versions.html

build.gradle には spring-boot-gradle-plugin を記述してはいましたが、Spring Boot を Gradle で開発する上でとりあえず書いておけばよいものとしか思っておらず、詳細は全く確認していませんでした。

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.0.RELEASE")
        classpath("org.springframework:springloaded:1.2.1.RELEASE")
    }
}

他にも気づかなかった理由として

Spring Boot のプロジェクトサイト では、maven の場合には、

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.2.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

と spring-boot-starter-parent で共通のバージョン番号を指定できるように書かれているのですが、gradle の場合には、

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web:1.2.2.RELEASE")
}

と spring-boot-starter-web にバージョン番号を指定するよう書かれており、maven と比べると Gradle は不便だな、とすっかり思い込んでいましたよ!

試してみます。

いつもどおり手順を書きながら試してみます。

  1. IntelliJ IDEA 上で 1.0.x-gradleplugin ブランチを作成します。

  2. 現在 spring-boot-starter-... で始まるライブラリに個別に 1.2.2.RELEASE を指定していますが、spring-boot-gradle-plugin にだけ 1.2.1.RELEASE を指定して、spring-boot-starter-... で始まるライブラリにバージョン番号を指定しないようにするとどうなるのか試してみます。build.gradle を リンク先のその1の内容 に変更します。

  3. 変更後、Gradle tasks View の「Refresh Gradle projects」アイコンをクリックして、変更した build.gradle の内容を反映します。

    f:id:ksby:20150318025342p:plain

    spring-boot-starter 関連が全部 1.2.1.RELEASE になりました。なぜか org.springframework:spring- 関連は 4.1.4.RELEASE と 4.1.5.RELEASE が混在していますが。一旦このままで次に進みます。

  4. 今度は spring-boot-gradle-plugin にだけ 1.2.2.RELEASE を指定してみます。build.gradle を リンク先のその2の内容 に変更します。

  5. 変更後、Gradle tasks View の「Refresh Gradle projects」アイコンをクリックして、変更した build.gradle の内容を反映します。

    f:id:ksby:20150318031328p:plain

    今度は spring-boot-starter 関連が全部 1.2.2.RELEASE になりました。org.springframework:spring- 関連も 4.1.4.RELEASE が消えて 4.1.5.RELEASE のみになりました。

  6. clean タスク → build タスクの順に実行し、BUILD SUCCESSFUL が表示されることを確認します。

  7. commit、GitHub へ Push、1.0.x-gradleplugin -> 1.0.x へ Pull Request、1.0.x でマージ、1.0.x-gradleplugin ブランチを削除、をします。

結論

公式の英語のマニュアルはひと通り目を通しておかないとダメですね。。。

ソースコード

build.gradle

■その1

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.1.RELEASE")
        classpath("org.springframework:springloaded:1.2.1.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'spring-boot'
apply plugin: 'idea'

jar {
    baseName = 'ksbysample-webapp-basic'
    version = '0.0.1-SNAPSHOT'
}

idea {
    module {
        inheritOutputDirs = false
        outputDir = file("$buildDir/classes/main/")
    }
}

repositories {
    jcenter()
    maven {
        url "http://oss.sonatype.org/content/groups/public/"
    }
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("org.springframework.boot:spring-boot-starter-security")
    compile("mysql:mysql-connector-java:5.1.34")
    compile("org.mybatis:mybatis:3.2.8")
    compile("org.mybatis:mybatis-spring:1.2.2")
    compile("org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16")
    compile("org.codehaus.janino:janino:2.7.5")
    compile("org.apache.commons:commons-lang3:3.3.2")
    compile("org.projectlombok:lombok:1.14.8")
    testCompile("org.springframework.boot:spring-boot-starter-test")
    testCompile("org.springframework.security:spring-security-test:4.0.0.RC2")
    testCompile("org.dbunit:dbunit:2.5.0")
    testCompile("org.yaml:snakeyaml:1.16-SNAPSHOT")
}

bootRun {
    jvmArgs = ['-Dspring.profiles.active=develop']
}

test {
    jvmArgs = ['-Dspring.profiles.active=unittest']
}
  • buildscript 内の classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.0.RELEASE")classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.1.RELEASE") に変更します。
  • dependencies 内の def springBootVersion = '1.2.2.RELEASE' を削除します。また :${springBootVersion} と書いている部分を全て削除します。

■その2

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.2.RELEASE")
        classpath("org.springframework:springloaded:1.2.1.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'spring-boot'
apply plugin: 'idea'

jar {
    baseName = 'ksbysample-webapp-basic'
    version = '0.0.1-SNAPSHOT'
}

idea {
    module {
        inheritOutputDirs = false
        outputDir = file("$buildDir/classes/main/")
    }
}

repositories {
    jcenter()
    maven {
        url "http://oss.sonatype.org/content/groups/public/"
    }
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("org.springframework.boot:spring-boot-starter-security")
    compile("mysql:mysql-connector-java:5.1.34")
    compile("org.mybatis:mybatis:3.2.8")
    compile("org.mybatis:mybatis-spring:1.2.2")
    compile("org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16")
    compile("org.codehaus.janino:janino:2.7.5")
    compile("org.apache.commons:commons-lang3:3.3.2")
    compile("org.projectlombok:lombok:1.14.8")
    testCompile("org.springframework.boot:spring-boot-starter-test")
    testCompile("org.springframework.security:spring-security-test:4.0.0.RC2")
    testCompile("org.dbunit:dbunit:2.5.0")
    testCompile("org.yaml:snakeyaml:1.16-SNAPSHOT")
}

bootRun {
    jvmArgs = ['-Dspring.profiles.active=develop']
}

test {
    jvmArgs = ['-Dspring.profiles.active=unittest']
}
  • buildscript 内の classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.1.RELEASE")classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.2.RELEASE") に変更します。

履歴

2015/03/18
初版発行。