Skip to content

Commit 90e7fa9

Browse files
vaadin-botmc1arkemcollovati
authored
fix: Allow Gradle builds to use custom sourcesets (#15739) (CP: 23.3) (#15753)
* fix: Allow Gradle builds to use custom sourcesets (#15739) When a sourceset other than `main` is used for holding classes using Vaadin components, or for providing a resolution scope for Vaadin dependencies, the `prepareVaadinFrontend` task does not correctly extract the correct Node libraries required by those classes, and the `buildVaadinFrontend` task subsequently fails to complete due to Vite requiring those Node dependencies to have been extracted into the project so failing to find all of its front-end dependencies. The `VaadinFlowPluginExtension` is being altered to include a `sourceSetName` field, that defaults to `main` for backwards compatibility but allows users to specify a custom sourceset to use for the prepare and build tasks. As the use of a custom sourceset generally implies that different Gradle tasks will be executed for operating on those sourcesets, and a different dependency scope generated for the custom sourceset, fields have also been added to the extension that allow users to override the values the plugin uses, whilst the default values follow the Gradle conventions for both `main` and non-`main` sourcesets. Fixes #15738 * downgrade spring boot version Co-authored-by: Michael Clarke <mc1arke@users.noreply.github.com> Co-authored-by: Marco Collovati <marco@vaadin.com>
1 parent 676feda commit 90e7fa9

6 files changed

Lines changed: 157 additions & 31 deletions

File tree

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

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2000-2022 Vaadin Ltd
2+
* Copyright 2000-2023 Vaadin Ltd
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -525,4 +525,97 @@ class MiscSingleModuleTest : AbstractGradleTest() {
525525
classpath.map { it.removeSuffix("-SNAPSHOT.jar").dropLastWhile { it != '-' } }
526526
}
527527
}
528+
529+
@Test
530+
fun testUsingNonMainSourceSet() {
531+
testProject.settingsFile.writeText(
532+
"""
533+
pluginManagement {
534+
repositories {
535+
gradlePluginPortal()
536+
}
537+
}
538+
"""
539+
)
540+
testProject.buildFile.writeText(
541+
"""
542+
plugins {
543+
id 'java'
544+
id 'org.springframework.boot' version '2.7.4'
545+
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
546+
id("com.vaadin")
547+
}
548+
549+
repositories {
550+
mavenLocal()
551+
mavenCentral()
552+
maven { url 'https://maven.vaadin.com/vaadin-prereleases' }
553+
}
554+
555+
sourceSets {
556+
ui {
557+
java
558+
}
559+
main {
560+
java {
561+
compileClasspath += ui.output
562+
runtimeClasspath += ui.output + ui.runtimeClasspath
563+
}
564+
}
565+
}
566+
567+
vaadin {
568+
productionMode = true
569+
sourceSetName = 'ui'
570+
}
571+
572+
dependencies {
573+
uiImplementation('com.vaadin:flow:$flowVersion')
574+
implementation('org.springframework.boot:spring-boot-starter-web')
575+
}
576+
577+
jar {
578+
enabled = false
579+
}
580+
"""
581+
)
582+
583+
testProject.newFile(
584+
"src/main/java/com/example/demo/DemoApplication.java", """
585+
package com.example.demo;
586+
587+
import org.springframework.boot.SpringApplication;
588+
import org.springframework.boot.autoconfigure.SpringBootApplication;
589+
590+
@SpringBootApplication
591+
public class DemoApplication {
592+
593+
public static void main(String[] args) {
594+
SpringApplication.run(DemoApplication.class, args);
595+
}
596+
597+
}
598+
""".trimIndent()
599+
)
600+
601+
testProject.newFile(
602+
"src/ui/java/com/example/demo/AppShell.java", """
603+
package com.example.demo;
604+
605+
import com.vaadin.flow.component.page.AppShellConfigurator;
606+
import com.vaadin.flow.server.PWA;
607+
608+
@PWA(name = "Demo application", shortName = "Demo")
609+
public class AppShell implements AppShellConfigurator {
610+
}
611+
""".trimIndent()
612+
)
613+
614+
val build: BuildResult = testProject.build("build")
615+
build.expectTaskSucceded("vaadinPrepareFrontend")
616+
build.expectTaskSucceded("vaadinBuildFrontend")
617+
618+
val jar: File = testProject.builtJar
619+
expectArchiveContainsVaadinBundle(jar, true)
620+
}
528621
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2000-2022 Vaadin Ltd
2+
* Copyright 2000-2023 Vaadin Ltd
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,10 +45,10 @@ internal class GradlePluginAdapter(val project: Project, private val isBeforePro
4545
override fun generatedTsFolder(): File = extension.generatedTsFolder
4646

4747
override fun getClassFinder(): ClassFinder {
48-
val runtimeClasspath: Configuration? = project.configurations.findByName("runtimeClasspath")
49-
val runtimeClasspathJars: List<File> = if (runtimeClasspath != null) {
48+
val dependencyConfiguration: Configuration? = project.configurations.findByName(extension.dependencyScope!!)
49+
val dependencyConfigurationJars: List<File> = if (dependencyConfiguration != null) {
5050
var artifacts: List<ResolvedArtifact> =
51-
runtimeClasspath.resolvedConfiguration.resolvedArtifacts.toList()
51+
dependencyConfiguration.resolvedConfiguration.resolvedArtifacts.toList()
5252
val extension = VaadinFlowPluginExtension.get(project)
5353
val artifactFilter = extension.classpathFilter.toPredicate()
5454
artifacts = artifacts.filter { artifactFilter.test(it.moduleVersion.id.module) }
@@ -57,11 +57,11 @@ internal class GradlePluginAdapter(val project: Project, private val isBeforePro
5757

5858
// we need to also analyze the project's classes
5959
val sourceSet: SourceSetContainer = project.properties["sourceSets"] as SourceSetContainer
60-
val classesDirs: List<File> = sourceSet.getByName("main").output.classesDirs
60+
val classesDirs: List<File> = sourceSet.getByName(extension.sourceSetName).output.classesDirs
6161
.toList()
6262
.filter { it.exists() }
6363

64-
val resourcesDir: List<File> = listOfNotNull(sourceSet.getByName("main").output.resourcesDir)
64+
val resourcesDir: List<File> = listOfNotNull(sourceSet.getByName(extension.sourceSetName).output.resourcesDir)
6565
.filter { it.exists() }
6666

6767
// for Spring Boot project there is no "providedCompile" scope: the WAR plugin brings that in.
@@ -71,7 +71,7 @@ internal class GradlePluginAdapter(val project: Project, private val isBeforePro
7171
?.toList()
7272
?: listOf()
7373

74-
val apis: Set<File> = (runtimeClasspathJars + classesDirs + resourcesDir + servletJar).toSet()
74+
val apis: Set<File> = (dependencyConfigurationJars + classesDirs + resourcesDir + servletJar).toSet()
7575

7676
// eagerly check that all the files/folders exist, to avoid spamming the console later on
7777
// see https://github.com/vaadin/vaadin-gradle-plugin/issues/38 for more details
@@ -94,7 +94,7 @@ internal class GradlePluginAdapter(val project: Project, private val isBeforePro
9494
}
9595

9696
override fun getJarFiles(): MutableSet<File> {
97-
val jarFiles: Set<File> = project.configurations.runtimeClasspath.jars.toSet()
97+
val jarFiles: Set<File> = project.configurations.getByName(extension.dependencyScope!!).jars.toSet()
9898
return jarFiles.toMutableSet()
9999
}
100100

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

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2000-2022 Vaadin Ltd
2+
* Copyright 2000-2023 Vaadin Ltd
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@ package com.vaadin.gradle
1818
import com.vaadin.flow.server.Constants
1919
import com.vaadin.flow.server.InitParameters
2020
import com.vaadin.flow.server.frontend.FrontendTools
21-
import com.vaadin.flow.server.frontend.FrontendUtils
2221
import com.vaadin.flow.server.frontend.installer.NodeInstaller
2322
import groovy.lang.Closure
2423
import groovy.lang.DelegatesTo
@@ -197,6 +196,27 @@ public open class VaadinFlowPluginExtension(project: Project) {
197196

198197
public var classpathFilter: ClasspathFilter = ClasspathFilter()
199198

199+
/**
200+
* The name of the SourceSet to scan for Vaadin components - i.e. the classes that are annoated with
201+
* Vaadin annotations.
202+
*/
203+
public var sourceSetName : String = "main"
204+
205+
/**
206+
* The Gradle scope the Vaadin dependencies have been added to. Defaults to 'runtimeClasspath' if
207+
* no sourceSetName has been specified, or '<code>sourceSetName</code>RuntimeClasspath' if a non-main sourceset
208+
* has been set.
209+
*/
210+
public var dependencyScope : String? = null
211+
212+
/**
213+
* The Gradle task that the `vaadinPrepareFrontend` task must run before. The target task should run before
214+
* or be the task that copies the files from the resources directories of the specified SourceSet to the relevant
215+
* output directory for that SourceSet. Defaults to 'processResources' if no sourceSetName has been specified, or
216+
* 'process<code>SourceSetName</code>Resources' if a non-main sourceset has been specified.
217+
*/
218+
public var processResourcesTaskName : String? = null
219+
200220
public fun filterClasspath(@DelegatesTo(value = ClasspathFilter::class, strategy = Closure.DELEGATE_FIRST) block: Closure<*>? = null): ClasspathFilter {
201221
if (block != null) {
202222
block.delegate = classpathFilter
@@ -246,6 +266,24 @@ public open class VaadinFlowPluginExtension(project: Project) {
246266
if (useGlobalPnpmProperty != null) {
247267
useGlobalPnpm = useGlobalPnpmProperty
248268
}
269+
270+
// calculate processResourcesTaskName if not set by user
271+
if (processResourcesTaskName == null) {
272+
processResourcesTaskName = if (sourceSetName == "main") {
273+
"processResources"
274+
} else {
275+
"process${sourceSetName.replaceFirstChar(Char::titlecase)}Resources"
276+
}
277+
}
278+
279+
// calculate dependencyScope if not set by user
280+
if (dependencyScope == null) {
281+
dependencyScope = if (sourceSetName == "main") {
282+
"runtimeClasspath"
283+
} else {
284+
sourceSetName + "RuntimeClasspath"
285+
}
286+
}
249287
}
250288

251289
override fun toString(): String = "VaadinFlowPluginExtension(" +
@@ -271,13 +309,16 @@ public open class VaadinFlowPluginExtension(project: Project) {
271309
"generatedTsFolder=$generatedTsFolder, " +
272310
"nodeVersion=$nodeVersion, " +
273311
"nodeDownloadRoot=$nodeDownloadRoot, " +
274-
"nodeAutoUpdate=$nodeAutoUpdate" +
275-
"resourceOutputDirectory=$resourceOutputDirectory" +
276-
"postinstallPackages=$postinstallPackages" +
312+
"nodeAutoUpdate=$nodeAutoUpdate, " +
313+
"resourceOutputDirectory=$resourceOutputDirectory, " +
314+
"postinstallPackages=$postinstallPackages, " +
315+
"sourceSetName=$sourceSetName, " +
316+
"dependencyScope=$dependencyScope, " +
317+
"processResourcesTaskName=$processResourcesTaskName" +
277318
")"
278319
}
279320

280321
internal val Project.buildResourcesDir: File get() {
281322
val sourceSets: SourceSetContainer = project.convention.getPlugin(JavaPluginConvention::class.java).sourceSets
282-
return sourceSets.getByName("main").output.resourcesDir!!
323+
return sourceSets.getByName(extensions.getByType(VaadinFlowPluginExtension::class.java).sourceSetName).output.resourcesDir!!
283324
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2000-2022 Vaadin Ltd
2+
* Copyright 2000-2023 Vaadin Ltd
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,10 +48,10 @@ public class VaadinPlugin : Plugin<Project> {
4848

4949
// add a new source-set folder for generated stuff, by default `vaadin-generated`
5050
val sourceSets: SourceSetContainer = it.properties["sourceSets"] as SourceSetContainer
51-
sourceSets.getByName("main").resources.srcDirs(extension.resourceOutputDirectory)
51+
sourceSets.getByName(extension.sourceSetName).resources.srcDirs(extension.resourceOutputDirectory)
5252

5353
// auto-activate tasks: https://github.com/vaadin/vaadin-gradle-plugin/issues/48
54-
project.tasks.getByPath("processResources").dependsOn("vaadinPrepareFrontend")
54+
project.tasks.getByPath(extension.processResourcesTaskName!!).dependsOn("vaadinPrepareFrontend")
5555
if (extension.productionMode) {
5656
// this will also catch the War task since it extends from Jar
5757
project.tasks.withType(Jar::class.java) { task: Jar ->

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2000-2022 Vaadin Ltd
2+
* Copyright 2000-2023 Vaadin Ltd
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,8 +18,6 @@ package com.vaadin.gradle
1818
import com.vaadin.flow.plugin.base.BuildFrontendUtil
1919
import com.vaadin.flow.server.frontend.FrontendTools
2020
import org.gradle.api.DefaultTask
21-
import org.gradle.api.Task
22-
import org.gradle.api.artifacts.ProjectDependency
2321
import org.gradle.api.tasks.TaskAction
2422

2523
/**
@@ -33,17 +31,18 @@ public open class VaadinPrepareFrontendTask : DefaultTask() {
3331
group = "Vaadin"
3432
description = "checks that node and npm tools are installed, copies frontend resources available inside `.jar` dependencies to `node_modules`, and creates or updates `package.json` and `webpack.config.json` files."
3533

34+
val extension: VaadinFlowPluginExtension = VaadinFlowPluginExtension.get(project)
3635
// Maven's task run in the LifecyclePhase.PROCESS_RESOURCES phase
3736

3837
// the processResources copies stuff from build/vaadin-generated
3938
// (which is populated by this task) and therefore must run after this task.
40-
project.tasks.getByName("processResources").mustRunAfter("vaadinPrepareFrontend")
39+
project.tasks.getByName(extension.processResourcesTaskName!!).mustRunAfter("vaadinPrepareFrontend")
4140

4241
// make sure all dependent projects have finished building their jars, otherwise
4342
// the Vaadin classpath scanning will not work properly. See
4443
// https://github.com/vaadin/vaadin-gradle-plugin/issues/38
4544
// for more details.
46-
dependsOn(project.configurations.runtimeClasspath.jars)
45+
dependsOn(project.configurations.getByName(extension.dependencyScope!!).jars)
4746
}
4847

4948
@TaskAction

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2000-2022 Vaadin Ltd
2+
* Copyright 2000-2023 Vaadin Ltd
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@ package com.vaadin.gradle
1717

1818

1919
import org.gradle.api.artifacts.Configuration
20-
import org.gradle.api.artifacts.ConfigurationContainer
2120
import org.gradle.api.file.FileCollection
2221
import com.vaadin.flow.function.SerializableSupplier
2322
import com.vaadin.flow.server.frontend.FrontendTools
@@ -74,12 +73,6 @@ internal fun VaadinFlowPluginExtension.createFrontendTools(): FrontendTools {
7473
return FrontendTools(settings)
7574
}
7675

77-
/**
78-
* Returns the "runtimeClasspath" file collection.
79-
*/
80-
internal val ConfigurationContainer.runtimeClasspath: Configuration
81-
get() = getByName("runtimeClasspath")
82-
8376
/**
8477
* Returns only jar files from given file collection.
8578
*/

0 commit comments

Comments
 (0)