Skip to content

Commit

Permalink
fix: include filesystem dependencies in gradle plugin class finder (#…
Browse files Browse the repository at this point in the history
…19287) (CP: 24.3) (#19302)

* fix: include filesystem dependencies in gradle plugin class finder (#19287)

Gradle plugin builds a class finder upon the JAR files obtained through
the project dependencies resolved artifacts, that however does not include
filesystem dependencies.
This change adds filesystem dependencis to the list of project JAR files
provided to the class finder.

Fixes #19024

* fix java imports

---------

Co-authored-by: Marco Collovati <marco@vaadin.com>
  • Loading branch information
vaadin-bot and mcollovati committed May 3, 2024
1 parent cc1e1be commit 668e0c8
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ import java.io.File
import kotlin.test.assertContains
import kotlin.test.expect
import com.vaadin.flow.server.InitParameters
import com.vaadin.flow.server.frontend.FrontendUtils
import elemental.json.JsonObject
import elemental.json.impl.JsonUtil
import org.gradle.api.JavaVersion
import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.TaskOutcome
import org.junit.Before
import org.junit.Test
import java.nio.file.Files
import java.nio.file.StandardCopyOption

/**
* The most basic tests. If these fail, the plugin is completely broken and all
Expand Down Expand Up @@ -329,6 +332,76 @@ class VaadinSmokeTest : AbstractGradleTest() {

}

/**
* Tests that build works with resources from classpath, not only from
* frontend directory
*
* https://github.com/vaadin/flow/issues/14420
*/
@Test
fun vaadinBuildFrontendShouldScanFilesystemDependencies() {
testProject.buildFile.writeText(
"""
plugins {
id 'war'
id 'com.vaadin'
}
repositories {
mavenLocal()
mavenCentral()
maven { url = 'https://maven.vaadin.com/vaadin-prereleases' }
}
dependencies {
implementation("com.vaadin:flow:$flowVersion")
implementation(files('libs/addon.jar'))
}
"""
)

testProject.newFile(
"src/main/java/org/vaadin/example/MainView.java", """
package org.vaadin.example;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.router.Route;
import com.vaadin.example.Addon;
@Route("")
public class MainView extends Div {
public MainView() {
add(new Addon());
}
}
""".trimIndent()
)

testProject.newFolder("libs")
val addonJar: File = testProject.newFile("libs/addon.jar")
Files.copy(
File(javaClass.classLoader.getResource("addon.jar").path).toPath(),
addonJar.toPath(), StandardCopyOption.REPLACE_EXISTING
)

val result: BuildResult = testProject.build("-Pvaadin.productionMode", "build")
result.expectTaskSucceded("vaadinPrepareFrontend")
result.expectTaskSucceded("vaadinBuildFrontend")

val addonFile =
File(testProject.dir, FrontendUtils.DEFAULT_PROJECT_FRONTEND_GENERATED_DIR + "jar-resources/my-addon.js")
expect(true, addonFile.toString()) { addonFile.exists() }

val importsFile = File(
testProject.dir,
FrontendUtils.DEFAULT_PROJECT_FRONTEND_GENERATED_DIR + "flow/" + FrontendUtils.IMPORTS_NAME
)
expect(true, importsFile.toString()) { importsFile.exists() }
expect(true, "Addon javascript should be found and imported") {
importsFile.readText().contains("import 'Frontend/generated/jar-resources/my-addon.js'")
}
}

@Test
fun pluginShouldFailWithUnsupportedGradleVersion() {

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,16 @@ internal class GradlePluginAdapter(
val dependencyConfigurationJars: List<File> = if (dependencyConfiguration != null) {
var artifacts: List<ResolvedArtifact> =
dependencyConfiguration.resolvedConfiguration.resolvedArtifacts.toList()

// Detect local filesystem dependencies that are not resolved as artifacts
// They will be added to the filtered artifacts list
val filesystemDependencies =
dependencyConfiguration.resolvedConfiguration.files.minus(artifacts.map { it.file }.toSet())

val artifactFilter = config.classpathFilter.toPredicate()
artifacts = artifacts.filter { artifactFilter.test(it.moduleVersion.id.module) }
artifacts.map { it.file }

artifacts.map { it.file }.plus(filesystemDependencies)
} else listOf()

// we need to also analyze the project's classes
Expand Down

0 comments on commit 668e0c8

Please sign in to comment.