Gradle子项目构建排序:先构建Angular再生成Spring Boot整合Jar包
Hey there! Let's fix this build order issue for you, and I'll even throw in a Kotlin DSL version since you're not a fan of Groovy's syntax. You're already close with your existing configs—here's how to tie everything together:
First: Groovy DSL Solution
Step 1: Confirm Subprojects Are Included
Make sure your root settings.gradle lists both subprojects:
include ':core', ':ui'
Step 2: Add Cross-Subproject Dependency
The key is to make the core subproject's build task depend on the ui subproject's build task. Add this line to your core/build.gradle:
// Ensure core builds only after ui is fully built build.dependsOn ':ui:build'
That's it! Now when you run gradle build from the root directory:
- Gradle will first execute the
uisubproject's build (including yournpm_installandbuildAngulartasks, which outputs Angular files to core's static directory) - Then it will run the
coresubproject's build, packaging the Spring Boot jar with the Angular assets included.
Bonus: Kotlin DSL Version (Since You Hate Groovy)
If you want to migrate everything to Kotlin DSL, here's the full setup:
Root settings.gradle.kts
include("core", "ui")
ui/build.gradle.kts
plugins { id("com.moowork.node") version "1.2.0" } buildscript { repositories { // Replace with your actual globalRepositories (e.g., mavenCentral()) mavenCentral() } dependencies { classpath("com.moowork.gradle:gradle-node-plugin:1.2.0") } } node { version = "8.9.1" npmVersion = "5.6.0" download = true } // Register the buildAngular task val buildAngular by tasks.registering(NpmTask::class) { args = listOf("run", "build") } // Chain dependencies: buildAngular needs npm_install first, build needs buildAngular buildAngular.get().dependsOn(tasks.npm_install) tasks.build.dependsOn(buildAngular)
core/build.gradle.kts
plugins { java id("org.springframework.boot") version "2.0.0.M7" id("io.spring.dependency-management") version "1.0.6.RELEASE" // Compatible with Spring Boot 2.0.0.M7 } group = "xxx.xxxxxx" version = "0.0.1-SNAPSHOT" java { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 } buildscript { repositories { mavenCentral() maven { url = uri("https://repo.spring.io/milestone") } // Required for Spring Boot milestone versions } val springBootVersion = "2.0.0.M7" dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion") } } tasks.jar { archiveBaseName.set("spring-boot-angular") archiveVersion.set("1.0.0") } // Critical: Make core's build depend on ui's build tasks.build.dependsOn(project(":ui").tasks.build)
Note for Kotlin DSL:
Since you're using Spring Boot 2.0.0.M7 (a milestone release), we added the Spring milestone repository to ensure Gradle can resolve all dependencies correctly.
Now running gradle build from the root will behave exactly as you want: build frontend first, then backend, resulting in a single Spring Boot jar with your Angular app included.
内容的提问来源于stack exchange,提问作者ielkhalloufi

