From 5f9d61fb76124fcb4ab74a1852fd58b21a9ea4c9 Mon Sep 17 00:00:00 2001 From: Marco Collovati Date: Sun, 10 Nov 2024 17:16:29 +0100 Subject: [PATCH] fix: ignore spring related classes in native build (#1063) Quarkus Spring-Data extension does not support JPA specification. This change removes from the native build Hilla classes referencing Spring Data classes to prevent failures during native image creation. --- .../QuarkusHillaNativeProcessor.java | 32 +++++++++++++++++-- commons/hilla-shaded-deps/pom.xml | 2 ++ commons/runtime/pom.xml | 14 ++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/commons/deployment/src/main/java/com/github/mcollovati/quarkus/hilla/deployment/QuarkusHillaNativeProcessor.java b/commons/deployment/src/main/java/com/github/mcollovati/quarkus/hilla/deployment/QuarkusHillaNativeProcessor.java index 86722fe8..ed221d24 100644 --- a/commons/deployment/src/main/java/com/github/mcollovati/quarkus/hilla/deployment/QuarkusHillaNativeProcessor.java +++ b/commons/deployment/src/main/java/com/github/mcollovati/quarkus/hilla/deployment/QuarkusHillaNativeProcessor.java @@ -47,6 +47,7 @@ import com.vaadin.hilla.push.PushEndpoint; import io.quarkus.arc.deployment.ExcludedTypeBuildItem; import io.quarkus.bootstrap.classloading.QuarkusClassLoader; +import io.quarkus.deployment.Capabilities; import io.quarkus.deployment.annotations.BuildProducer; import io.quarkus.deployment.annotations.BuildStep; import io.quarkus.deployment.annotations.ExecutionTime; @@ -55,12 +56,14 @@ import io.quarkus.deployment.builditem.BytecodeTransformerBuildItem; import io.quarkus.deployment.builditem.CombinedIndexBuildItem; import io.quarkus.deployment.builditem.GeneratedNativeImageClassBuildItem; +import io.quarkus.deployment.builditem.RemovedResourceBuildItem; import io.quarkus.deployment.builditem.nativeimage.NativeImageResourcePatternsBuildItem; import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem; import io.quarkus.deployment.builditem.nativeimage.RuntimeInitializedPackageBuildItem; import io.quarkus.deployment.pkg.NativeConfig; import io.quarkus.gizmo.ClassCreator; import io.quarkus.gizmo.MethodCreator; +import io.quarkus.maven.dependency.ArtifactKey; import io.quarkus.undertow.deployment.ServletDeploymentManagerBuildItem; import io.quarkus.vertx.http.deployment.DefaultRouteBuildItem; import org.atmosphere.cache.UUIDBroadcasterCache; @@ -123,6 +126,24 @@ void preventHillaSpringBeansDetection(BuildProducer produ producer.produce(new ExcludedTypeBuildItem(ServletDeployer.class.getName())); } + /* + * Quarkus Spring-Data extension does not currently support JpaSpecificationExecutor. + * Hilla classes based on that API must be removed from the native build + * to prevent NoClassDefFound errors + * https://quarkus.io/guides/spring-data-jpa#what-is-currently-unsupported + */ + @BuildStep(onlyIf = IsNativeBuild.class) + void removeHillaSpringBasedClasses(Capabilities capabilities, BuildProducer producer) { + producer.produce(new RemovedResourceBuildItem( + ArtifactKey.of("com.vaadin", "hilla-endpoint", null, "jar"), + Set.of( + "com/vaadin/hilla/crud/CrudConfiguration.class", + "com/vaadin/hilla/crud/CrudRepositoryService.class", + "com/vaadin/hilla/crud/JpaFilterConverter.class", + "com/vaadin/hilla/crud/ListRepositoryService.class", + "com/vaadin/hilla/crud/PropertyStringFilterSpecification.class"))); + } + /* * If license-checker is not present at runtime, create stub Dau integration * classes that thrown an exception if invoked. This will happen only if the @@ -188,6 +209,7 @@ void generateDummyDauClassesIfLicenseCheckerIsNotPresent( @BuildStep void hillaNativeSupport( + Capabilities capabilities, CombinedIndexBuildItem combinedIndex, BuildProducer nativeImageResource, BuildProducer reflectiveClass) { @@ -205,11 +227,15 @@ void hillaNativeSupport( classes.add(index.getClassByName(PushEndpoint.class)); classes.addAll(getJsonClasses(index)); - classes.add(index.getClassByName("org.springframework.data.repository.Repository")); - classes.add(index.getClassByName("org.springframework.data.repository.CrudRepository")); - classes.add(index.getClassByName("org.springframework.data.domain.Pageable")); + if (capabilities.isPresent(QuarkusHillaExtensionProcessor.SPRING_DATA_SUPPORT)) { + classes.add(index.getClassByName("org.springframework.data.repository.Repository")); + classes.add(index.getClassByName("org.springframework.data.repository.CrudRepository")); + classes.add(index.getClassByName("org.springframework.data.domain.Pageable")); + classes.add(index.getClassByName("org.springframework.data.domain.Specification")); + } reflectiveClass.produce(ReflectiveClassBuildItem.builder(classes.stream() + .filter(Objects::nonNull) .map(classInfo -> classInfo.name().toString()) .toArray(String[]::new)) .constructors() diff --git a/commons/hilla-shaded-deps/pom.xml b/commons/hilla-shaded-deps/pom.xml index fb44e998..32119488 100644 --- a/commons/hilla-shaded-deps/pom.xml +++ b/commons/hilla-shaded-deps/pom.xml @@ -135,6 +135,8 @@ org/springframework/data/domain/Sort$Order.class org/springframework/data/domain/Sort$NullHandling.class org/springframework/data/domain/Unpaged.class + org/springframework/data/repository/CrudRepository.class + org/springframework/data/repository/Repository.class org/springframework/data/util/Streamable.class org/springframework/data/util/StreamUtils.class org/springframework/data/util/LazyStreamable.class diff --git a/commons/runtime/pom.xml b/commons/runtime/pom.xml index 529e8f0d..e478571c 100644 --- a/commons/runtime/pom.xml +++ b/commons/runtime/pom.xml @@ -71,6 +71,20 @@ com.github.mcollovati hilla-shaded-deps ${project.version} + + + org.springframework + * + + + org.springframework.security + * + + + org.springframework.data + * + + org.graalvm.sdk