以下是 Eclipse 用于 Spring Boot 的 build.gradle 文件
我们的目标
最近搜索Spring的信息时,发现几乎所有项目都在使用Spring Boot,因此我也为了自己的需求尝试了一下。
可以说是在build.gradle中创建了一个Spring Boot版本的EclipseWTP项目。
预先准备
预先创建Gradle默认配置的文件夹。
以下是一种构建方式:适用于Web但不需要WEB-INF目录。
プロジェクトフォルダ
├─build.gradle
└─src
├─main
│ ├─java
│ └─resources
└─test
├─java
└─resources
构建.gradle文件的内容 .gradle de
创建一个名为build.gradle的文件,并运行gradle命令进行eclipse插件安装,这样就可以创建一个Spring + Gradle项目。
buildscript {
repositories {
jcenter()
// maven { url "http://repo.spring.io/snapshot" }
// maven { url "http://repo.spring.io/milestone" }
}
dependencies {
// これを作成した時点の最新バージョンは1.2.5
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'spring-boot'
apply plugin: 'eclipse'
// エンコーディング
def defaultEncoding = 'UTF-8'
[compileJava, compileTestJava]*.options*.encoding = defaultEncoding
// ソースと実行ファイルのJava バージョン
def jdkVersion = 1.8
sourceCompatibility = jdkVersion
targetCompatibility = jdkVersion
// 依存関係の解決
repositories {
jcenter()
// mavenCentral()
}
// 依存ライブラリの設定
dependencies {
// ここにバージョン指定は不要らしい
compile "org.springframework.boot:spring-boot-starter-web"
testCompile "org.springframework.boot:spring-boot-starter-test"
}
//// eclipseの設定を毎回リセットする場合は下記の設定を有効にするか
//// gradle cleanEclipse eclipse を実行する
// tasks.eclipse.dependsOn(cleanEclipse)
// eclipse プロジェクトの設定
import org.gradle.plugins.ide.eclipse.model.SourceFolder
eclipse {
// .project の設定
project {
// nature の追加
// spring project nature を追加
natures 'org.springframework.ide.eclipse.core.springnature'
// Gradle nature を追加
natures 'org.springsource.ide.eclipse.gradle.core.nature'
// buildCommand の追加
// spring のbuildCommand を追加
buildCommand 'org.springframework.ide.eclipse.core.springbuilder'
}
// gradleで取得したjarのパスを絶対パスにしない
// 事前に GRADLE_USER_HOME を設定しておく必要がある
// Windows: [ウィンドウ]->[設定] を開く
// Mac : [Eclipse]->[環境設定]を開く
// 設定ウィンドウが開いたら [Java]->[ビルド・パス]->[クラスパス変数] を選択する
// [新規]ボタンを押下して [名前]:GRADLE_USER_HOME, [パス]:gradleを配置したパス を設定する
pathVariables 'GRADLE_USER_HOME': gradle.gradleUserHomeDir
// .classpath の設定
classpath {
// 依存している jar の source と javadoc をダウンロードする
downloadSources = true // デフォルトは false
downloadJavadoc = true // javadoc のパスは絶対パスになる
file {
// ソースパスの設定を削除
beforeMerged { classpath ->
// classpath.entries.clear()
classpath.entries.removeAll { it.kind == "src" }
}
// 出力パスを gradle のデフォルトに合わせる
whenMerged { classpath ->
classpath.entries.findAll { it instanceof SourceFolder && it.path.startsWith("src/main/java") }*.output = "build/classes/main"
classpath.entries.findAll { it instanceof SourceFolder && it.path.startsWith("src/main/resources") }*.output = "build/resources/main"
classpath.entries.findAll { it instanceof SourceFolder && it.path.startsWith("src/test/java") }*.output = "build/classes/test"
classpath.entries.findAll { it instanceof SourceFolder && it.path.startsWith("src/test/resources") }*.output = "build/resources/test"
classpath.entries.removeAll { it.kind == "output" }
}
}
}
// .settings/org.eclipse.jdt.core.prefs の設定
jdt {
// 毎回固定の設定を行う場合は下記のようにする(テンプレは自前で用意しておく)
/*
file {
def props = new Properties()
props.load(new FileInputStream("${projectDir}/template/org.eclipse.jdt.core.prefs"))
withProperties { properties -> properties.putAll(props) }
}
*/
}
}
使用方法
-
- JARの依存関係を見たいとき: gradle dependencies
Eclipseからアプリを実行したいとき: gradle bootRun でアプリが実行される。内部でtomcat-embedが動作しているので、ブラウザで、localhost:8080/[@RequstMappingで設定したパス] にアクセスする。
JARファイルをコマンドから実行したいとき: gralde build でJARファイルを作成してから、java -jar [jarファイル名]。アプリを終了するときは、Ctrl+C でアプリを停止する。
请参阅提供的网址。
-
- http://docs.spring.io/spring-boot/docs/current/reference/html/getting-started-installing-spring-boot.html#getting-started-gradle-installation
-
- http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-build-systems.html#using-boot-gradle
- http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-running-your-application.html#using-boot-running-with-the-gradle-plugin
另外,我也参考了《Spring Boot》的第一章节。