Copilot commented on code in PR #639:
URL: https://github.com/apache/maven-war-plugin/pull/639#discussion_r3665270403
##########
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:
Conflict matching is done on `artifactId` only, which can incorrectly
exclude an overlay jar when two different dependencies share the same
artifactId across different groupIds (leading to missing/incorrect jars in the
final WAR). A more reliable approach is to read
`META-INF/maven/**/pom.properties` from the overlay jar to obtain
groupId/artifactId (and optionally version), then compare against the project’s
resolved artifacts by groupId+artifactId.
##########
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 referenced in the log message but is not in scope in
`computeConflictingJars(...)`, which will not compile. Pass the `Overlay` (or
overlay id) into `computeConflictingJars(...)` and use that parameter in the
log message, or remove the overlay id from the message.
--
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]