1 回答

TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超13個(gè)贊
在過去一周非常深入的 Gradle 學(xué)習(xí)之后(創(chuàng)建這個(gè)主題是因?yàn)槲铱煲懒耍┪医K于找到了非常令人滿意的解決方案。
我想分享我的目標(biāo)和最終解決方案:
擁有盡可能小的 build.gradle
有發(fā)展的可能 build --continuous
使eclipse插件(可能還有其他 IDE)可以使用自己的構(gòu)建系統(tǒng)完美地反映純命令行 Gradle 開發(fā)。
這是一個(gè)多項(xiàng)目,其中一個(gè)是帶有 DevTools 的 Spring Boot 應(yīng)用程序。
wrapper (parent)
|--frontend (child)
| |--src-client
| | |--static
| | | |--img (images)
| | | \--js (raw ES6 modules)
| | \--sass (raw, note not in static folder)
| \--build
| |--lib
| | \--front.jar
| |--dist
| | |--js (bundled, minimized)
| | \--css (compiled production, minimized)
| \--dev
| \--css (compiled dev, compact readable format)
\--backend (child) (spring boot)
|--src
| |--main/java
| |--main/resources
| \--test/java
\--build
\--lib
\--application.jar
正如我所描述的,目標(biāo)是:
已經(jīng)bootRun與運(yùn)行原為js和源編譯CSS,而且與被包含在每個(gè)資源backend的main。
已經(jīng)bootJar編譯到生產(chǎn)中,依賴于已編譯front.jar而不是在開發(fā)中使用的所有內(nèi)容(上一點(diǎn))。
我使用了配置、sourceSets 和 bootRun 屬性的組合(+ 很多時(shí)間)。
以下是文件(精簡):
包裝器/build.gradle
wrapper.gradleVersion '5.0-milestone-1'
allprojects {
apply plugin: 'eclipse'
}
包裝器/前/build.gradle
plugins {
id 'java' // possibly use java-base or just custom zip task, since client doesn't actually compile java
}
jar {
dependsOn buildProduction // task that compiles my stuff into build/dist
baseName 'front'
classifier 'SNAPSHOT-' + new Date().format('yyyyMMddHHmmss')
from(buildDir.absolutePath + '/dist') {
into 'static'
}
}
// Note there is a lot of other tasks here that actually compile my stuff, like gulp-sass and JSPM bundling with babel transpiler.
包裝器/后端/build.gradle
buildscript {
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
}
dependencies {
classpath 'org.springframework.boot:spring-boot-gradle-plugin:2.1.0.RC1'
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
sourceCompatibility = 10 // eclipse has (or had) problems with Java 11
targetCompatibility = 10
sourceSets {
// 'java' plugin adds default main sourceSet
dev {
resources.srcDirs = [
project(':front').projectDir.absolutePath + '/src-client',
project(':front').buildDir.absolutePath + '/dev'
]
}
}
bootJar {
baseName 'application'
classifier 'SNAPSHOT-' + new Date().format('yyyyMMddHHmmssSSS')
// I used bootArchives since it was already there and my stuff fits description, you can also define your own configuration and extend runtime one.
classpath configurations.bootArchives
}
bootRun {
sourceResources sourceSets.dev // I make bootRun (dev) use dev sourceSet
}
dependencies {
runtime 'org.springframework.boot:spring-boot-devtools'
// Since bootArchives configuration is used only by bootJar (not bootRun), this will be only in final fat .jar
bootArchives project(':front')
...
}
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
}
一些有幫助的鏈接:http : //mrhaki.blogspot.com/2014/09/gradle-goodness-adding-dependencies.html
請(qǐng)注意,客戶端的源文件夾稱為src-client:這是在 eclipse 中制作“完美鏡像”的關(guān)鍵。如果我們將它命名src為已經(jīng)main在backendeclipse 中使用的名稱,則會(huì)因名稱沖突而嘔吐(這可能可以通過配置 eclipse 插件來修復(fù),但話又說回來 - 我們希望保持簡單,沒有 IDE 配置)。
作為最終結(jié)果,我們通過 gradle 的連續(xù)構(gòu)建和 eclipse 中的完全鏡像行為獲得了非常好的命令行開發(fā),默認(rèn)其配置以相同的方式工作(但使用 eclipse 的構(gòu)建器而不是 gradle 的構(gòu)建器)。我們還在backend項(xiàng)目中獲得了很好的鏈接文件夾到從front. 同樣可能適用于 IDEA :)。
添加回答
舉報(bào)