ascheman commented on code in PR #11505:
URL: https://github.com/apache/maven/pull/11505#discussion_r2642169291
##########
impl/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java:
##########
@@ -1099,6 +1123,193 @@ public Set<Entry<K, V>> entrySet() {
}
}
+ /**
+ * Context object for resource handling configuration.
+ * Groups parameters shared between main and test resource handling to
reduce method parameter count.
+ */
+ private record ResourceHandlingContext(
+ MavenProject project,
+ Path baseDir,
+ Set<String> modules,
+ boolean modularProject,
+ ModelBuilderResult result) {}
+
+ /**
+ * Handles resource configuration for a given scope (main or test).
+ * This method applies the resource priority rules:
+ * <ol>
+ * <li>Modular project: use resources from {@code <sources>} if present,
otherwise inject defaults</li>
+ * <li>Classic project: use resources from {@code <sources>} if present,
otherwise use legacy resources</li>
+ * </ol>
+ *
+ * @param context the resource handling context containing project info
+ * @param resources the legacy resource list (from {@code <resources>} or
{@code <testResources>})
+ * @param hasResourcesInSources whether resources are configured via
{@code <sources>}
+ * @param scope the project scope (MAIN or TEST)
+ */
+ private void handleResourceConfiguration(
+ ResourceHandlingContext context,
+ List<Resource> resources,
+ boolean hasResourcesInSources,
+ ProjectScope scope) {
+
+ String scopeId = scope.id();
+ String scopeName = scope == ProjectScope.MAIN ? "Main" : "Test";
+ String legacyElement = scope == ProjectScope.MAIN ? "<resources>" :
"<testResources>";
+ String sourcesConfig = scope == ProjectScope.MAIN
+ ? "<source><lang>resources</lang></source>"
+ : "<source><lang>resources</lang><scope>test</scope></source>";
+
+ if (context.modularProject()) {
+ if (hasResourcesInSources) {
+ // Modular project with resources configured via <sources> -
already added above
+ if (hasExplicitLegacyResources(resources, context.baseDir(),
scopeId)) {
+ logger.warn(
+ "Legacy {} element is ignored because {} resources
are configured via {} in <sources>",
+ legacyElement,
+ scopeId,
+ sourcesConfig);
+ }
+ logger.debug(
+ "{} resources configured via <sources> element,
ignoring legacy {} element",
+ scopeName,
+ legacyElement);
+ } else {
+ // Modular project without resources in <sources> - inject
module-aware defaults
+ if (hasExplicitLegacyResources(resources, context.baseDir(),
scopeId)) {
+ String message = "Legacy " + legacyElement
+ + " element is ignored because modular sources are
configured. "
+ + "Use " + sourcesConfig + " in <sources> for
custom resource paths.";
+ logger.warn(message);
+ context.result()
+ .getProblemCollector()
+ .reportProblem(new
org.apache.maven.impl.model.DefaultModelProblem(
+ message,
+ Severity.WARNING,
+ Version.V41,
+ context.project().getModel().getDelegate(),
+ -1,
+ -1,
+ null));
+ }
+ logger.debug(
+ "Injecting module-aware {} resource roots for {}
modules",
+ scopeId,
+ context.modules().size());
+ for (String module : context.modules()) {
+ Path resourcePath = context.baseDir()
+ .resolve("src")
+ .resolve(module)
+ .resolve(scopeId)
+ .resolve("resources");
+ logger.debug(" - Adding {} resource root: {} (module:
{})", scopeId, resourcePath, module);
Review Comment:
After investigation, this was intentionally [kept as-is in the original
code](https://github.com/apache/maven/blob/master/impl/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java#L674).
Source roots are registered regardless of directory existence because:
1. Tests verify expected roots are registered
(testModularSourcesWithResourcesInSources expects 2 roots per module)
2. Directories may be created later during the build lifecycle
3. Filtering would change behavioral semantics, not just clean up code
The current behavior matches how Maven handles source directories generally.
--
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]