Spring Boot 中使用 spring-boot-devtools (使用 Gradle 作為構建工具)


Spring Boot 中使用 spring-boot-devtools (使用 Gradle 作為構建工具)

本文使用 Gradle 作為構建工具,關於 Gradle 構建工具,可以理解為是 Maven 的升級版,我個人認為比 Maven 好的地方是 Gradle 的 Groovy 的語法,比起 Maven 的 xml 語法,看起來要簡潔得多。

首先,我們引入阿里巴巴的 Maven 倉庫,加快構建下載的速度

allprojects {
    repositories {
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
    }
}

引入基本依賴

引入 Spring Boot 最最基礎的依賴:

compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.5.2.RELEASE'

引入 spring-boot-devtools 的依賴:

compile("org.springframework.boot:spring-boot-devtools")

所以最后看起來是這樣:

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.5.2.RELEASE'
    compile("org.springframework.boot:spring-boot-devtools")
}

加入倉庫(很重要,決定了 spring-boot-devtools 能否正確下載)

buildscript {
    ext {
        springBootVersion = '1.5.2.RELEASE'
    }
    repositories {
        // NOTE: You should declare only repositories that you need here
        mavenLocal()
        mavenCentral()
        maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
        maven { url "http://repo.spring.io/release" }
        maven { url "http://repo.spring.io/milestone" }
        maven { url "http://repo.spring.io/snapshot" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

加入插件(很重要,決定了 spring-boot-devtools 能否正確下載)

這里就一句話,這句話,我測試了很久才發現這句話必須加上去。

apply plugin: 'spring-boot'

這時,我們就可以刷新 Gradle,看到所有的依賴都正常下載。

到這里配置文件的配置就告一段落了,下面我們還要配置 IntelliJ IDEA。

配置 IntelliJ IDEA

勾選 Build project automatically 選項前的單選按鈕。

接下來的設置

1、mac 電腦按下 command + alt + shift + / 出現:

2、找到 “compiler.automake.allow.when.app.running” 這個選項,並且勾選:

此致,就大功告成了。

示例項目 GitHub 所在地址:https://github.com/weimingge14/SpringBootDevTool

參考資料:
1、Intellij IDEA 使用Spring-boot-devTools無效解決辦法


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM