Skip to content

Commit cf72357

Browse files
authored
feat!: enable productionMode with Gradle's bootJar (#22704)
Flow Gradle plugin enable productionMode automatically when run with bootJar or bootBuildImage task. -Pvaadin.productionMode=false overrides automation. RelatedTo: vaadin/base-starter-spring-gradle#293
1 parent 5a4ea11 commit cf72357

File tree

3 files changed

+66
-22
lines changed

3 files changed

+66
-22
lines changed

flow-plugins/flow-gradle-plugin/src/functionalTest/kotlin/com/vaadin/gradle/MiscSingleModuleTest.kt

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class MiscSingleModuleTest : AbstractGradleTest() {
5959
build.expectTaskNotRan("vaadinBuildFrontend")
6060

6161
val war: File = testProject.builtWar
62-
expectArchiveDoesntContainVaadinWebpackBundle(war, false)
62+
expectArchiveDoesntContainVaadinBundle(war, false)
6363
}
6464

6565
/**
@@ -160,7 +160,7 @@ class MiscSingleModuleTest : AbstractGradleTest() {
160160
expect(null) { build.task(":vaadinBuildFrontend") }
161161

162162
val jar: File = testProject.builtJar
163-
expectArchiveDoesntContainVaadinWebpackBundle(jar, false)
163+
expectArchiveDoesntContainVaadinBundle(jar, false)
164164
}
165165

166166
/**
@@ -230,6 +230,19 @@ class MiscSingleModuleTest : AbstractGradleTest() {
230230
doTestSpringProjectProductionMode();
231231
}
232232

233+
@Test
234+
fun testSpringProjectAutoProductionMode() {
235+
doTestSpringProjectAutoProductionMode();
236+
}
237+
238+
/**
239+
* Test Spring boot project build with 'bootJar' task (production), but forced in dev mode.
240+
*/
241+
@Test
242+
fun testSpringProjectForcedDevelopmentMode() {
243+
doTestSpringProjectForcedDevelopmentMode();
244+
}
245+
233246
@Ignore("Webpack uses gzip compression")
234247
@Test
235248
fun testSpringProjectProductionModeWebpack() {
@@ -238,6 +251,44 @@ class MiscSingleModuleTest : AbstractGradleTest() {
238251

239252
private fun doTestSpringProjectProductionMode(compressedExtension: String = "*.br") {
240253

254+
doTestSpringProject()
255+
256+
val build: BuildResult =
257+
testProject.build("-Pvaadin.productionMode", "build")
258+
build.expectTaskSucceded("vaadinPrepareFrontend")
259+
build.expectTaskSucceded("vaadinBuildFrontend")
260+
261+
val jar: File = testProject.builtJar
262+
expectArchiveContainsVaadinBundle(jar, true, compressedExtension)
263+
}
264+
265+
private fun doTestSpringProjectAutoProductionMode(compressedExtension: String = "*.br") {
266+
267+
doTestSpringProject()
268+
269+
val build: BuildResult =
270+
testProject.build("bootJar")
271+
build.expectTaskSucceded("vaadinPrepareFrontend")
272+
build.expectTaskSucceded("vaadinBuildFrontend")
273+
274+
val jar: File = testProject.builtJar
275+
expectArchiveContainsVaadinBundle(jar, true, compressedExtension)
276+
}
277+
278+
private fun doTestSpringProjectForcedDevelopmentMode(compressedExtension: String = "*.br") {
279+
280+
doTestSpringProject()
281+
282+
val build: BuildResult =
283+
testProject.build("-Pvaadin.productionMode=false", "bootJar")
284+
build.expectTaskSucceded("vaadinPrepareFrontend")
285+
build.expectTaskNotRan("vaadinBuildFrontend")
286+
287+
val jar: File = testProject.builtJar
288+
expectArchiveDoesntContainVaadinBundle(jar, false)
289+
}
290+
291+
private fun doTestSpringProject() {
241292
val springBootVersion = "3.3.4"
242293

243294
testProject.settingsFile.writeText(
@@ -251,7 +302,7 @@ class MiscSingleModuleTest : AbstractGradleTest() {
251302
"""
252303
)
253304
testProject.buildFile.writeText(
254-
"""
305+
"""
255306
plugins {
256307
id 'org.springframework.boot' version '$springBootVersion'
257308
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
@@ -298,7 +349,7 @@ class MiscSingleModuleTest : AbstractGradleTest() {
298349

299350
// need to create the Application.java file otherwise bootJar will fail
300351
testProject.newFile(
301-
"src/main/java/com/example/demo/DemoApplication.java", """
352+
"src/main/java/com/example/demo/DemoApplication.java", """
302353
package com.example.demo;
303354
304355
import org.springframework.boot.SpringApplication;
@@ -317,7 +368,7 @@ class MiscSingleModuleTest : AbstractGradleTest() {
317368

318369
// AppShell.java file creation
319370
testProject.newFile(
320-
"src/main/java/com/example/demo/AppShell.java", """
371+
"src/main/java/com/example/demo/AppShell.java", """
321372
package com.example.demo;
322373
323374
import com.vaadin.flow.component.page.AppShellConfigurator;
@@ -328,14 +379,6 @@ class MiscSingleModuleTest : AbstractGradleTest() {
328379
}
329380
""".trimIndent()
330381
)
331-
332-
val build: BuildResult =
333-
testProject.build("-Pvaadin.productionMode", "build")
334-
build.expectTaskSucceded("vaadinPrepareFrontend")
335-
build.expectTaskSucceded("vaadinBuildFrontend")
336-
337-
val jar: File = testProject.builtJar
338-
expectArchiveContainsVaadinBundle(jar, true, compressedExtension)
339382
}
340383

341384
/**

flow-plugins/flow-gradle-plugin/src/functionalTest/kotlin/com/vaadin/gradle/TestUtils.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,13 @@
1515
*/
1616
package com.vaadin.flow.gradle
1717

18-
import org.gradle.api.JavaVersion
1918
import org.gradle.testkit.runner.BuildResult
2019
import org.gradle.testkit.runner.BuildTask
2120
import org.gradle.testkit.runner.GradleRunner
2221
import org.gradle.testkit.runner.TaskOutcome
2322
import java.io.File
24-
import java.io.IOException
2523
import java.nio.file.FileSystems
2624
import java.nio.file.Files
27-
import java.nio.file.Path
2825
import java.nio.file.PathMatcher
2926
import java.util.zip.ZipInputStream
3027
import kotlin.test.expect
@@ -165,11 +162,11 @@ fun expectArchiveContainsVaadinBundle(
165162
}
166163

167164
/**
168-
* Asserts that given archive (jar/war) doesn't contain the Vaadin webpack bundle:
165+
* Asserts that given archive (jar/war) doesn't contain the Vaadin bundle:
169166
* the `META-INF/VAADIN/build/` directory.
170167
*/
171-
fun expectArchiveDoesntContainVaadinWebpackBundle(archive: File,
172-
isSpringBootJar: Boolean) {
168+
fun expectArchiveDoesntContainVaadinBundle(archive: File,
169+
isSpringBootJar: Boolean) {
173170
val isWar: Boolean = archive.name.endsWith(".war", true)
174171
val isStandaloneJar: Boolean = !isWar && !isSpringBootJar
175172
val resourcePackaging: String = when {

flow-plugins/flow-gradle-plugin/src/main/kotlin/com/vaadin/gradle/VaadinFlowPluginExtension.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ import org.gradle.internal.component.external.model.ModuleComponentArtifactIdent
3838

3939
public abstract class VaadinFlowPluginExtension @Inject constructor(private val project: Project) {
4040
/**
41-
* Whether we are running in productionMode or not. Defaults to false.
42-
* Responds to the `-Pvaadin.productionMode` property.
41+
* Whether we are running in productionMode or not. Defaults to true when
42+
* run with `bootJar` or `bootBuildImage` task, otherwise false. Responds to
43+
* the `-Pvaadin.productionMode` property.
4344
*/
4445
public abstract val productionMode: Property<Boolean>
4546

@@ -352,9 +353,12 @@ public class PluginEffectiveConfiguration(
352353
internal val projectName = project.name
353354

354355
public val productionMode: Provider<Boolean> = extension.productionMode
355-
.convention(false)
356+
.convention(project.tasks.names.any { task ->
357+
task.contains("bootJar") || task.contains("bootBuildImage")
358+
})
356359
.overrideWithSystemPropertyFlag(project, "vaadin.productionMode")
357360

361+
358362
public val sourceSetName: Property<String> = extension.sourceSetName
359363
.convention("main")
360364

0 commit comments

Comments
 (0)