Skip to content

Conversation

@ascheman
Copy link
Contributor

Summary

  • Automatically inject module-aware resource roots for projects using modular <sources> layout
  • Resources are picked up from src/<module>/main/resources and src/<module>/test/resources
  • Issue warnings when explicit legacy <resources> configuration is ignored in favor of modular defaults

Problem

Projects using Maven 4.x modular sources (<source><module>org.foo.bar</module></source>) had to manually configure maven-resources-plugin for each module. Resources from the modular layout paths were not automatically discovered.

Solution

When a project defines at least one module in <sources>:

  1. Modular resource roots are automatically injected for each module
  2. Legacy <resources> from Super POM defaults are silently replaced
  3. Explicit legacy <resources> configuration triggers a ModelProblem warning

Test plan

  • testModularSourcesInjectResourceRoots - verifies resource injection for multi-module projects
  • testModularSourcesWithExplicitResourcesIssuesWarning - verifies warnings are issued when legacy resources are ignored

@ascheman
Copy link
Contributor Author

Hi @desruisseaux,

I'd be happy to get some feedback from you (cf. my mail to the dev mailing list as of today), before making this an official PR.
It affects the handling of resources as we recently as issue on my Java Modules examples.

Thanks
Gerd

@ascheman ascheman force-pushed the feature/maven-4-resource-handling branch from 3e2d01f to bef10a1 Compare December 3, 2025 17:51
@ascheman ascheman force-pushed the feature/maven-4-resource-handling branch from bef10a1 to f747693 Compare December 13, 2025 09:17
ascheman added a commit to support-and-care/maven that referenced this pull request Dec 13, 2025
- Rename 'ctx' to 'context' in DefaultProjectBuilder
- Use short form 'ModelProblem' instead of FQCN in test

Addresses review comments from elharo on PR apache#11505.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
elharo
elharo previously requested changes Dec 13, 2025
ascheman added a commit to support-and-care/maven that referenced this pull request Dec 14, 2025
Use {@code <element>} instead of &lt;element&gt; escapes in javadoc
for better readability in source code (per Martin's suggestion).

Addresses review comment from desruisseaux on PR apache#11505.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
ascheman added a commit to support-and-care/maven that referenced this pull request Dec 14, 2025
Avoid abbreviations in new code per project conventions.

Addresses review comment from elharo on PR apache#11505.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
ascheman added a commit to support-and-care/maven that referenced this pull request Dec 14, 2025
Avoid abbreviations in new code per project conventions.

Addresses review comment from elharo on PR apache#11505.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@ascheman ascheman force-pushed the feature/maven-4-resource-handling branch from bdb4c3f to ee7f1d3 Compare December 14, 2025 09:57
@elharo elharo dismissed their stale review December 14, 2025 12:05

addressed

@ascheman
Copy link
Contributor Author

Could someone please add the backport-to-4.0.x label as it would be great to have this change one of the next RCs?

Copy link
Contributor

@gnodet gnodet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should reject any project that mixes <resource> and <source> elements, I don't see that as a valid use case.

@gnodet
Copy link
Contributor

gnodet commented Dec 16, 2025

I think we should reject any project that mixes <resource> and <source> elements, I don't see that as a valid use case.

I think this should even be done in org.apache.maven.impl.model.DefaultModelValidator.

@ascheman
Copy link
Contributor Author

I think we should reject any project that mixes <resource> and <source> elements, I don't see that as a valid use case.

I think this should even be done in org.apache.maven.impl.model.DefaultModelValidator.

Thanks for the feedback, @gnodet.
Personally, I am with you, cf. my lengthy discussion about the topic in Confluence. I was proposing two approaches (warnings/lenient vs. errors/strict).
How could we get a stronger vote about it (with more opinions than just yours and mine)?

Having said that, this PR is just about the first part of the problem (make Maven Core handle resources at all). Perhaps we can focus here on that feature?

@desruisseaux
Copy link
Contributor

desruisseaux commented Dec 16, 2025

I would also suggest to reject any project that mixes <resource> and <source> elements. Since <source> is new in Maven 4, we are not yet breaking any existing project. If this policy is too strict, we can relax later. The opposite is much more difficult to do.

@ascheman ascheman requested a review from gnodet December 16, 2025 10:12
ascheman added a commit to support-and-care/java9-jigsaw-examples that referenced this pull request Dec 16, 2025
Once the PR is merged (and a new version of Maven is published)
we should be able to perform a simplified build wrt Maven resources.
@ascheman ascheman force-pushed the feature/maven-4-resource-handling branch from 3b2af3a to d2ce972 Compare December 19, 2025 08:08
ascheman added a commit to support-and-care/maven that referenced this pull request Dec 19, 2025
- Rename 'ctx' to 'context' in DefaultProjectBuilder
- Use short form 'ModelProblem' instead of FQCN in test

Addresses review comments from elharo on PR apache#11505.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
ascheman added a commit to support-and-care/maven that referenced this pull request Dec 23, 2025
- Change debug logging to trace level for reduced verbosity
- Add periods to end of all log/warning messages
- Use else blocks to avoid redundant logging after warnings
- Make hasExplicitLegacyResources and createModularResourceRoot static
- Remove low-level debug log from hasExplicitLegacyResources
- Simplify loop logging by using context.modules() directly

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
ascheman and others added 10 commits December 24, 2025 14:56
Maven 4.x introduces a unified <sources> element that supports modular
project layouts (src/<module>/<scope>/<lang>). However, resource handling
did not follow the modular layout - resources were always loaded from
the legacy <resources> element which defaults to src/main/resources.

This change implements automatic module-aware resource injection:

- Detect modular projects (projects with at least one module in sources)
- For modular projects without resource configuration in <sources>,
  automatically inject resource roots following the modular layout:
  src/<module>/main/resources and src/<module>/test/resources
- Resources configured via <sources> take priority over legacy <resources>
- Issue warnings (as ModelProblem) when explicit legacy resources are ignored

Example: A project with modular sources for org.foo.moduleA will now
automatically pick up resources from:
- src/org.foo.moduleA/main/resources
- src/org.foo.moduleA/test/resources

This eliminates the need for explicit maven-resources-plugin executions
when using modular project layouts.
- Rename hasOnlySuperPomDefaults to hasExplicitLegacyResources
  with inverted logic to avoid double negations in predicates
- Remove redundant isEmpty() checks now handled by the method
- Make extractModules method static

Note: Extracting a shared method for main/test resource handling
was not possible due to checkstyle ParameterNumber limit (max 7).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Refactor duplicate resource handling code into a shared method:
- Add ResourceHandlingContext record to group shared parameters
- Add handleResourceConfiguration method (4 parameters vs 9)
- Remove unused outputDirectory param from createModularResourceRoot
- Replace ~120 lines of duplicate code with 7 lines

This addresses PR review comment apache#1 about code duplication between
main and test resource handling blocks.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
- Rename 'ctx' to 'context' in DefaultProjectBuilder
- Use short form 'ModelProblem' instead of FQCN in test

Addresses review comments from elharo on PR apache#11505.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Use {@code <element>} instead of &lt;element&gt; escapes in javadoc
for better readability in source code (per Martin's suggestion).

Addresses review comment from desruisseaux on PR apache#11505.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Avoid abbreviations in new code per project conventions.

Addresses review comment from elharo on PR apache#11505.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
- Change debug logging to trace level for reduced verbosity
- Add periods to end of all log/warning messages
- Use else blocks to avoid redundant logging after warnings
- Make hasExplicitLegacyResources and createModularResourceRoot static
- Remove low-level debug log from hasExplicitLegacyResources
- Simplify loop logging by using context.modules() directly

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Move resource handling logic to dedicated package-private class:
- Create ResourceHandlingContext with project-level state
- Method handleResourceConfiguration(scope, hasResourcesInSources)
  derives resources from scope, takes flag as parameter
- Reduces DefaultProjectBuilder by 179 lines

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Per PR apache#11505 review feedback from desruisseaux: the logs in
handleResourceConfiguration are the result of more elaborated
analysis and should remain at debug level, not trace.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@ascheman ascheman force-pushed the feature/maven-4-resource-handling branch from dcc621c to dc90ada Compare December 24, 2025 14:16
@desruisseaux
Copy link
Contributor

Thanks! I will wait a little bit in case there is some comment, and if none I propose to merge this weekend.

@desruisseaux desruisseaux merged commit 74ef127 into apache:master Dec 29, 2025
22 checks passed
@github-actions github-actions bot added this to the 4.1.0 milestone Dec 29, 2025
@github-actions
Copy link

@desruisseaux Please assign appropriate label to PR according to the type of change.

@desruisseaux desruisseaux added the enhancement New feature or request label Dec 29, 2025
@ascheman
Copy link
Contributor Author

Thanks a lot for merging, @desruisseaux.
I still hope for the backport-to-4.0.x label to ensure the change becomes part of the next Maven 4.0.0 RC.

@desruisseaux
Copy link
Contributor

Yes I thought about that. But as you mentioned on the slack channel, the work done in this pull request could be generalized. I thought that what this PR does for resources, the same thing could be done for tests. I'm thinking to refactor the ResourceHandler class in something that does not handle resources in a special way, but instead does the same thing in a uniform way for all combinations of <lang> and <scope>. If agreed, I would propose to merge on 4.0.x the generalized version.

@ascheman
Copy link
Contributor Author

Yes I thought about that. But as you mentioned on the slack channel, the work done in this pull request could be generalized.

Yes, I have a Phase 2 implementation in my mind which would be more general.

Nevertheless, the feature would already be helpful (and I'd be eager to get it in the next Release Candidate for Maven 4.0).

I thought that what this PR does for resources, the same thing could be done for tests. I'm thinking to refactor the ResourceHandler class in something that does not handle resources in a special way, but instead does the same thing in a uniform way for all combinations of <lang> and <scope>. If agreed, I would propose to merge on 4.0.x the generalized version.

Sounds good, perhaps we can join forces in the next weeks to get a combined implementation?

@ascheman ascheman deleted the feature/maven-4-resource-handling branch December 30, 2025 11:28
@desruisseaux
Copy link
Contributor

Maybe you can go ahead with phase 2 when you have time. Looking on the wiki page, I suggest the following changes:

  • Instead of record ModuleConfig(boolean hasMain, boolean hasTest, boolean hasMainResources, boolean hasTestResources), handle any combination of <lang> and <scope> with something like Map<Something, Boolean>. The mapping to legacy <resources>, <testResources>, etc. would still special cases, but the rest (e.g., generate automatically a Source object) can be handled by a universal code.
  • Remove rule R3, i.e., accept duplicated source for scope='X', lang='Y', module='Z'. The compiler plugin 4 already accepts that, the need for multi-source directories in Maven 3 was strong enough to justify an external plugin, and Maven 3 already accepts that for resources if I remember right.

@ascheman
Copy link
Contributor Author

I have raised a new issue (#11612) for further discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants