在 Gradle 中创建具有附加编译依赖项的生产配置

我正在尝试为我的生产版本定义构建脚本。


下面是项目结构,所有项目都是java插件。


wrapper (parent)

|--frontend (child)

|  |--src

|  |  |--js (raw ES6 modules)

|  |  |--sass (raw)

|  |--build

|     |--lib

|     |  |--production-front.jar

|     |--dist

|        |--js (bundled)

|        |--css (compiled production)

|--backend (child) (spring boot)

   |--build

      |--lib

         |--RELEASE.jar

现在这里发生的是默认情况下 ( sourceSets.main.resources.srcDirs) ofbackend直接链接到:


生的 :frontent/src/js

生成 :frontent/build/dist/css.

这样,当您运行它时,默认情况下它将处于开发模式。这意味着它将:


使用生成的scss->css 文件(这是资源,例如,如果您运行后台 gulp-sass,每次更改 scss 时都会编译它,css 将更新和繁荣,快速开发周期)。

使用在浏览器中直接编译的原始JS(JSPM、SystemJS、Babel) - 所以你只需要编辑:frontent/src/js和刷新页面。

好的,虽然 dev 是爱,但我还需要编译以进行生产。上面提到的项目结构也显示了:frontend生成的位置production-front.jar。


这是带有我的笔记的默认 java 构建树。

http://img.mukewang.com/61b3029600012c7a12480326.jpg

编辑 我需要建立依赖项,该依赖项将编译production-front.jarRELEASE.jar并省略提到的附加资源。

请注意,我只需要忽略这些资源,而不是main.resources.srcDirs.

什么是解决这个问题的正确方法(一种不执行从 .jar 中删除开发资源然后放入其他 production-front.jar 的任务)?我无法理解如何制作在这里工作的多个 sourceSets 或配置。


宝慕林4294392
浏览 201回答 1
1回答

慕桂英4014372

在过去一周非常深入的 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.gradlewrapper.gradleVersion '5.0-milestone-1'allprojects {    apply plugin: 'eclipse'}包装器/前/build.gradleplugins {    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.gradlebuildscript {    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 11targetCompatibility = 10sourceSets {    // '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 :)。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java