You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by "Carsten Ziegeler (Jira)" <ji...@apache.org> on 2021/01/11 06:35:00 UTC

[jira] [Commented] (SLING-10052) Cache artifact resolution per project

    [ https://issues.apache.org/jira/browse/SLING-10052?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17262417#comment-17262417 ] 

Carsten Ziegeler commented on SLING-10052:
------------------------------------------

Potential patch
{noformat}
diff --git a/src/main/java/org/apache/sling/feature/maven/ProjectHelper.java b/src/main/java/org/apache/sling/feature/maven/ProjectHelper.java
index b710190..ed98bc2 100644
--- a/src/main/java/org/apache/sling/feature/maven/ProjectHelper.java
+++ b/src/main/java/org/apache/sling/feature/maven/ProjectHelper.java
@@ -34,6 +34,7 @@ import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
 import java.util.TreeMap;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.stream.Collectors;

 import org.apache.maven.artifact.Artifact;
@@ -73,6 +74,9 @@ public abstract class ProjectHelper {
     /** Default metadata */
     private static final String METADATA_KEY = Feature.class.getName() + "/metadata";

+    /** Artifact cache */
+    private static final String ARTIFACT_CACHE = Artifact.class.getName() + "/cache";
+
     private static void store(final MavenProject project, final String key, final Map<String, Feature> features) {
         if ( features != null && !features.isEmpty()) {
             project.setContextValue(key, features.size());
@@ -317,37 +321,47 @@ public abstract class ProjectHelper {
             final ArtifactHandlerManager artifactHandlerManager,
             final ArtifactResolver resolver,
             final ArtifactId id) {
-        Artifact result = findArtifact(id, project.getAttachedArtifacts());
+        @SuppressWarnings("unchecked")
+        Map<String, Artifact> cache = (Map<String, Artifact>) project.getContextValue(ARTIFACT_CACHE);
+        if ( cache == null ) {
+            cache = new ConcurrentHashMap<>();
+            project.setContextValue(ARTIFACT_CACHE, cache);
+        }
+        Artifact result = cache.get(id.toMvnId());
         if ( result == null ) {
-            result = findArtifact(id, project.getDependencyArtifacts());
+            result = findArtifact(id, project.getAttachedArtifacts());
             if ( result == null ) {
-                if ( isLocalProjectArtifact(project, id)) {
-                    for(final Map.Entry<String, Feature> entry : getFeatures(project).entrySet()) {
-                        if ( entry.getValue().getId().equals(id)) {
-                            final Artifact artifact = new DefaultArtifact(id.getGroupId(), id.getArtifactId(), id.getVersion(), Artifact.SCOPE_PROVIDED, id.getType(), id.getClassifier(), null);
-                            artifact.setFile(createTmpFeatureFile(project, entry.getValue()));
-
-                            result = artifact;
-                            break;
+                result = findArtifact(id, project.getDependencyArtifacts());
+                if ( result == null ) {
+                    if ( isLocalProjectArtifact(project, id)) {
+                        for(final Map.Entry<String, Feature> entry : getFeatures(project).entrySet()) {
+                            if ( entry.getValue().getId().equals(id)) {
+                                final Artifact artifact = new DefaultArtifact(id.getGroupId(), id.getArtifactId(), id.getVersion(), Artifact.SCOPE_PROVIDED, id.getType(), id.getClassifier(), null);
+                                artifact.setFile(createTmpFeatureFile(project, entry.getValue()));
+
+                                result = artifact;
+                                break;
+                            }
                         }
                     }
-                }
-                if ( result == null ) {
-                    final Artifact prjArtifact = new DefaultArtifact(id.getGroupId(),
-                            id.getArtifactId(),
-                            VersionRange.createFromVersion(id.getVersion()),
-                            Artifact.SCOPE_PROVIDED,
-                            id.getType(),
-                            id.getClassifier(),
-                            artifactHandlerManager.getArtifactHandler(id.getType()));
-                    try {
-                        resolver.resolve(prjArtifact, project.getRemoteArtifactRepositories(), session.getLocalRepository());
-                    } catch (final ArtifactResolutionException | ArtifactNotFoundException e) {
-                        throw new RuntimeException("Unable to get artifact for " + id.toMvnId(), e);
+                    if ( result == null ) {
+                        final Artifact prjArtifact = new DefaultArtifact(id.getGroupId(),
+                                id.getArtifactId(),
+                                VersionRange.createFromVersion(id.getVersion()),
+                                Artifact.SCOPE_PROVIDED,
+                                id.getType(),
+                                id.getClassifier(),
+                                artifactHandlerManager.getArtifactHandler(id.getType()));
+                        try {
+                            resolver.resolve(prjArtifact, project.getRemoteArtifactRepositories(), session.getLocalRepository());
+                        } catch (final ArtifactResolutionException | ArtifactNotFoundException e) {
+                            throw new RuntimeException("Unable to get artifact for " + id.toMvnId(), e);
+                        }
+                        result = prjArtifact;
                     }
-                    result = prjArtifact;
                 }
             }
+            cache.put(id.toMvnId(), result);
         }

         return result;
{noformat}

> Cache artifact resolution per project
> -------------------------------------
>
>                 Key: SLING-10052
>                 URL: https://issues.apache.org/jira/browse/SLING-10052
>             Project: Sling
>          Issue Type: Improvement
>          Components: Feature Model, Maven Plugins and Archetypes
>            Reporter: Carsten Ziegeler
>            Priority: Major
>             Fix For: slingfeature-maven-plugin 1.4.22
>
>
> ProjectHelper.getOrResolveArtifact is potentially a slow operation. The build can be speed up by caching the resolution - if this method is called frequently during a build



--
This message was sent by Atlassian Jira
(v8.3.4#803005)