elharo commented on code in PR #639:
URL: https://github.com/apache/maven-war-plugin/pull/639#discussion_r3665548504


##########
src/main/java/org/apache/maven/plugins/war/packaging/OverlayPackagingTask.java:
##########
@@ -127,4 +137,96 @@ protected File getOverlayTempDirectory(WarPackagingContext 
context, Overlay over
         }
         return result;
     }
+
+    /**
+     * Identifies jars from the overlay's {@code WEB-INF/lib} whose artifactId 
matches a non-optional
+     * runtime-scope dependency resolved by the project. These jars are 
candidates for exclusion
+     * from the overlay copy so that only the project-resolved version ends up 
in {@code WEB-INF/lib}.
+     *
+     * <p>The overlay's cached unpack directory is <em>not</em> mutated; the 
returned set is used
+     * as additional excludes during the copy step.
+     *
+     * @param context the packaging context
+     * @param overlayDir the unpacked overlay directory
+     * @return set of paths (relative to the overlay dir) to exclude from the 
overlay copy
+     */
+    private Set<String> computeConflictingJars(WarPackagingContext context, 
File overlayDir) {
+        File libDir = new File(overlayDir, "WEB-INF/lib");
+        if (!libDir.isDirectory()) {
+            return Collections.emptySet();
+        }
+
+        ScopeArtifactFilter filter = new 
ScopeArtifactFilter(Artifact.SCOPE_RUNTIME);
+        Set<String> projectArtifactIds = new HashSet<>();
+        for (Artifact artifact : context.getProject().getArtifacts()) {
+            if (!artifact.isOptional() && filter.include(artifact) && 
"jar".equals(artifact.getType())) {
+                projectArtifactIds.add(artifact.getArtifactId());
+            }
+        }
+
+        if (projectArtifactIds.isEmpty()) {
+            return Collections.emptySet();
+        }
+
+        File[] overlayJars = libDir.listFiles((dir, name) -> 
name.endsWith(".jar"));
+        if (overlayJars == null) {
+            return Collections.emptySet();
+        }
+
+        Set<String> conflicting = new HashSet<>();
+        for (File overlayJar : overlayJars) {
+            String jarName = overlayJar.getName();
+            String artifactId = extractArtifactId(jarName);
+            if (artifactId != null && projectArtifactIds.contains(artifactId)) 
{
+                context.getLog()
+                        .debug("Excluding dependency [" + jarName + "] from 
overlay [" + overlay.getId()
+                                + "]; project runtime dependencies already 
include a version");
+                conflicting.add("WEB-INF/lib/" + jarName);
+            }
+        }

Review Comment:
   `overlay` is a field of `OverlayPackagingTask` (line 46: `private final 
Overlay overlay;`). Since `computeConflictingJars` is an instance method, 
`this.overlay` is accessible in scope. The code compiles and runs correctly.



##########
src/main/java/org/apache/maven/plugins/war/packaging/OverlayPackagingTask.java:
##########
@@ -127,4 +137,96 @@ protected File getOverlayTempDirectory(WarPackagingContext 
context, Overlay over
         }
         return result;
     }
+
+    /**
+     * Identifies jars from the overlay's {@code WEB-INF/lib} whose artifactId 
matches a non-optional
+     * runtime-scope dependency resolved by the project. These jars are 
candidates for exclusion
+     * from the overlay copy so that only the project-resolved version ends up 
in {@code WEB-INF/lib}.
+     *
+     * <p>The overlay's cached unpack directory is <em>not</em> mutated; the 
returned set is used
+     * as additional excludes during the copy step.
+     *
+     * @param context the packaging context
+     * @param overlayDir the unpacked overlay directory
+     * @return set of paths (relative to the overlay dir) to exclude from the 
overlay copy
+     */
+    private Set<String> computeConflictingJars(WarPackagingContext context, 
File overlayDir) {
+        File libDir = new File(overlayDir, "WEB-INF/lib");
+        if (!libDir.isDirectory()) {
+            return Collections.emptySet();
+        }
+
+        ScopeArtifactFilter filter = new 
ScopeArtifactFilter(Artifact.SCOPE_RUNTIME);
+        Set<String> projectArtifactIds = new HashSet<>();
+        for (Artifact artifact : context.getProject().getArtifacts()) {
+            if (!artifact.isOptional() && filter.include(artifact) && 
"jar".equals(artifact.getType())) {
+                projectArtifactIds.add(artifact.getArtifactId());
+            }
+        }

Review Comment:
   Addressed in commit fc54f70. Added `getJarGroupId()` which reads 
`META-INF/maven/**/pom.properties` from each overlay jar to obtain the groupId 
for precise `groupId:artifactId` matching. Falls back to artifactId-only 
matching when the metadata is absent.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to