Skip to content

Commit

Permalink
fix: cache ClassFinder for reuse in mojo execution (#20148)
Browse files Browse the repository at this point in the history
Creates ClassFinder once per mojo execution, preventing eccessive and
useless scans.

Fixes #19874
  • Loading branch information
mcollovati authored and vaadin-bot committed Oct 7, 2024
1 parent 8d0e36c commit 3dd17de
Showing 1 changed file with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import com.vaadin.flow.internal.StringUtil;
import com.vaadin.flow.plugin.base.BuildFrontendUtil;
import com.vaadin.flow.plugin.base.PluginAdapterBase;
Expand All @@ -37,11 +43,6 @@
import com.vaadin.flow.server.frontend.installer.NodeInstaller;
import com.vaadin.flow.server.frontend.installer.Platform;
import com.vaadin.flow.server.frontend.scanner.ClassFinder;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import static com.vaadin.flow.server.Constants.VAADIN_SERVLET_RESOURCES;
import static com.vaadin.flow.server.Constants.VAADIN_WEBAPP_RESOURCES;
Expand Down Expand Up @@ -241,6 +242,8 @@ public abstract class FlowModeAbstractMojo extends AbstractMojo
@Parameter(property = InitParameters.APPLICATION_IDENTIFIER)
private String applicationIdentifier;

private ClassFinder classFinder;

/**
* Generates a List of ClasspathElements (Run and CompileTime) from a
* MavenProject.
Expand Down Expand Up @@ -274,10 +277,8 @@ public static List<String> getClasspathElements(MavenProject project) {
* Target Maven project
* @return true if Hilla is available, false otherwise
*/
public static boolean isHillaAvailable(MavenProject mavenProject) {
List<String> classpathElements = FlowModeAbstractMojo
.getClasspathElements(mavenProject);
return BuildFrontendUtil.getClassFinder(classpathElements).getResource(
public boolean isHillaAvailable(MavenProject mavenProject) {
return getOrCreateClassFinder(mavenProject).getResource(
"com/vaadin/hilla/EndpointController.class") != null;
}

Expand All @@ -292,7 +293,7 @@ public static boolean isHillaAvailable(MavenProject mavenProject) {
* @return {@code true} if Hilla is available and Hilla views are used,
* {@code false} otherwise
*/
public static boolean isHillaUsed(MavenProject mavenProject,
public boolean isHillaUsed(MavenProject mavenProject,
File frontendDirectory) {
return isHillaAvailable(mavenProject)
&& FrontendUtils.isHillaViewsUsed(frontendDirectory);
Expand Down Expand Up @@ -325,11 +326,15 @@ public File generatedTsFolder() {

@Override
public ClassFinder getClassFinder() {
return getOrCreateClassFinder(project);
}

List<String> classpathElements = getClasspathElements(project);

return BuildFrontendUtil.getClassFinder(classpathElements);

private ClassFinder getOrCreateClassFinder(MavenProject project) {
if (classFinder == null) {
List<String> classpathElements = getClasspathElements(project);
classFinder = BuildFrontendUtil.getClassFinder(classpathElements);
}
return classFinder;
}

@Override
Expand Down

0 comments on commit 3dd17de

Please sign in to comment.