簡體   English   中英

在Gradle中創建具有附加編譯依賴性的生產配置

[英]Create production configuration with additional compile dependency in 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

現在,這里發生的是backend默認情況下( sourceSets.main.resources.srcDirs )直接鏈接到:

  • 原始的 :frontent/src/js
  • 生成 :frontent/build/dist/css

這樣,當您運行它時,默認情況下它將處於開發模式。 在這里,這意味着它將:

  • 使用生成的 scss-> css文件(這是資源,因此,例如,如果您運行后台gulp-sass每次更改scss都會對其進行編譯,則css會更新並迅速發展,開發周期很快)。
  • 使用直接在瀏覽器(JSPM,SystemJS,Babel)中編譯的原始 JS-因此,您只需要編輯:frontent/src/js並刷新頁面。

好吧,盡管開發人員是愛人,但我還需要進行編譯以進行生產。 上面提到的項目結構還顯示了:frontend生成production-front.jar

這是帶有我的注釋的默認Java構建樹。

在此處輸入圖片說明

編輯我需要進行依賴,將production-front.jar編譯為RELEASE.jar並省略提及的附加資源。

請注意,我只需要ommit這些資源,在沒有任何其他main.resources.srcDirs

解決此問題的正確方法是什么(不執行例如從.jar中刪除開發資源,然后放入其他production-front.jar的任務)? 我不知道如何制作多個可以在這里工作的sourceSet或配置。

在過去一個星期對Gradle進行了非常深入的學習之后(創建這個主題是因為我快要死了),我終於找到了非常令人滿意的解決方案。

我想分享一下我的目標和最終解決方案:

  • 具有最小的build.gradle
  • 有可能隨着build --continuous而發展- 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而不是dev中使用的所有內容(上一點),編譯到生產環境中。

我已經使用了配置,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 ,它將在backend eclipse中已由main使用,它將以名稱clash嘔吐(這可以通過配置eclipse插件來解決,但是再說一次-我們希望保持其簡單性,而不使用IDE配置)。

最終結果是,通過gradle的連續構建,我們得到了非常不錯的命令行開發,並且在eclipse中完全鏡像了行為,默認情況下,其配置以相同的方式工作(但使用eclipse的builder而不是gradle的builder)。 我們還可以在backend項目中獲得鏈接到front使用的源的漂亮文件夾。 同樣可能適用於IDEA :)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM