1 回答

TA贡献1871条经验 获得超13个赞
在过去一周非常深入的 Gradle 学习之后(创建这个主题是因为我快要死了)我终于找到了非常令人满意的解决方案。
我想分享我的目标和最终解决方案:
拥有尽可能小的 build.gradle
有发展的可能 build --continuous
使eclipse插件(可能还有其他 IDE)可以使用自己的构建系统完美地反映纯命令行 Gradle 开发。
这是一个多项目,其中一个是带有 DevTools 的 Spring Boot 应用程序。
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
正如我所描述的,目标是:
已经bootRun与运行原为js和源编译CSS,而且与被包含在每个资源backend的main。
已经bootJar编译到生产中,依赖于已编译front.jar而不是在开发中使用的所有内容(上一点)。
我使用了配置、sourceSets 和 bootRun 属性的组合(+ 很多时间)。
以下是文件(精简):
包装器/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
请注意,客户端的源文件夹称为src-client:这是在 eclipse 中制作“完美镜像”的关键。如果我们将它命名src为已经main在backendeclipse 中使用的名称,则会因名称冲突而呕吐(这可能可以通过配置 eclipse 插件来修复,但话又说回来 - 我们希望保持简单,没有 IDE 配置)。
作为最终结果,我们通过 gradle 的连续构建和 eclipse 中的完全镜像行为获得了非常好的命令行开发,默认其配置以相同的方式工作(但使用 eclipse 的构建器而不是 gradle 的构建器)。我们还在backend项目中获得了很好的链接文件夹到从front. 同样可能适用于 IDEA :)。
添加回答
举报