[英]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
。 這樣,當您運行它時,默認情況下它將處於開發模式。 在這里,這意味着它將:
:frontent/src/js
並刷新頁面。 好吧,盡管開發人員是愛人,但我還需要進行編譯以進行生產。 上面提到的項目結構還顯示了:frontend
生成production-front.jar
。
這是帶有我的注釋的默認Java構建樹。
編輯我需要進行依賴,將production-front.jar
編譯為RELEASE.jar
並省略提及的附加資源。
請注意,我只需要ommit這些資源,在沒有任何其他main.resources.srcDirs
。
解決此問題的正確方法是什么(不執行例如從.jar中刪除開發資源,然后放入其他production-front.jar的任務)? 我不知道如何制作多個可以在這里工作的sourceSet或配置。
在過去一個星期對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.