Spring BootのサンプルをGradle化した、けども……

最近Spring Bootで遊んでいます。

今回はMavenでビルドされているサンプルをGradle化しました。

ソースコードは https://github.com/backpaper0/spring_boot_sample です。

tagは https://github.com/backpaper0/spring_boot_sample/releases/tag/gradle です。

本題

まず、おもむろにgradle initしました。

gradle init

すでにpom.xmlがあるので依存関係とか色々よろしくやってくれたbuild.gradleが出力されました。

apply plugin: 'java'
apply plugin: 'maven'

group = 'sample'
version = '1.0-SNAPSHOT'

description = """spring-boot-sample"""

sourceCompatibility = 1.8
targetCompatibility = 1.8



repositories {

     maven { url "http://repo.maven.apache.org/maven2" }
}
dependencies {
    compile group: 'org.twitter4j', name: 'twitter4j-core', version:'4.0.2'
    compile(group: 'org.springframework.boot', name: 'spring-boot-starter-jersey', version:'1.2.1.RELEASE') {
exclude(module: 'spring-webmvc')
    }
    compile(group: 'org.glassfish.jersey.ext', name: 'jersey-mvc', version:'2.14') {
exclude(module: 'servlet-api')
    }
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf', version:'1.2.1.RELEASE'
    testCompile(group: 'org.springframework.boot', name: 'spring-boot-starter-test', version:'1.2.1.RELEASE') {
exclude(module: 'commons-logging')
    }
    testCompile group: 'junit', name: 'junit', version:'4.12'
}

あとはSpring Bootのリファレンスの 10.1.2 Gradle installation を参考にしてちょこちょこっと編集しました。

buildscript {
  repositories {
    jcenter()
    maven { url "http://repo.spring.io/snapshot" }
    maven { url "http://repo.spring.io/milestone" }
  }
  dependencies {
    //ここで拡張プロパティspringBootVersionは参照できひんの?_(:3」∠)_
    classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.1.RELEASE")
  }
}

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

group = 'sample'
version = '1.0-SNAPSHOT'

sourceCompatibility = 1.8
targetCompatibility = 1.8

ext {
  springBootVersion = '1.2.1.RELEASE'
}

repositories {
  jcenter()
  maven { url "http://repo.spring.io/snapshot" }
  maven { url "http://repo.spring.io/milestone" }
}

dependencies {
  compile 'org.twitter4j:twitter4j-core:4.0.2'
  compile ("org.springframework.boot:spring-boot-starter-jersey:$springBootVersion") {
    exclude(module: 'spring-webmvc')
  }
  compile ('org.glassfish.jersey.ext:jersey-mvc:2.14') {
    exclude(module: 'servlet-api')
  }
  compile "org.springframework.boot:spring-boot-starter-thymeleaf:$springBootVersion"
  testCompile ("org.springframework.boot:spring-boot-starter-test:$springBootVersion") {
    exclude(module: 'commons-logging')
  }
  testCompile 'junit:junit:4.12'
}

知りたいこと

build.gradleにも書いたけどbuildscriptのブロック内で拡張プロパティspringBootVersionを参照できないのでしょうか? (試しに使ってみたらビルド失敗した。。。)

教えてくださいお願いしますお願いします(他力本願)。

早速解決しました!

ありがとうございます!

修正してコミットしました!

まとめ

Gradle化すげえ簡単だった。