Skip to content

[Backport 4.0.x] Add mvnup SourceStrategy for migrating to <source> elements#12357

Open
gnodet wants to merge 4 commits into
maven-4.0.xfrom
backport/mvnup-source-strategy
Open

[Backport 4.0.x] Add mvnup SourceStrategy for migrating to <source> elements#12357
gnodet wants to merge 4 commits into
maven-4.0.xfrom
backport/mvnup-source-strategy

Conversation

@gnodet

@gnodet gnodet commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Summary

Backport of #12355 to maven-4.0.x.

  • Add SourceStrategy to mvnup that migrates legacy source configuration to Maven 4.1.0+ <source> elements
  • Handles compiler properties (maven.compiler.release, maven.compiler.source/target), compiler plugin configuration, source/test directories, and resources
  • Applicable when --model-version 4.1.0+ or --all is specified

Test plan

  • 26 unit tests covering all migration phases cherry-picked cleanly
  • Tests pass on maven-4.0.x

🤖 Generated with Claude Code

gnodet and others added 2 commits June 23, 2026 23:00
Adds a new mvnup upgrade strategy that migrates legacy source
configuration to Maven 4.1.0+ <source> elements. Handles four
migration phases:

- Compiler properties (maven.compiler.release, source/target)
  to <source><targetVersion>
- Compiler plugin configuration (<release>, <source>/<target>)
  to <source><targetVersion>
- Custom source/test directories to <source><directory>
- Resource/testResource sections to <source> with lang=resources

Applies when --model-version is 4.1.0+ or --all is set.
Runs at @priority(20), after all other strategies.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@gnodet gnodet added this to the 4.0.0-rc-6 milestone Jun 23, 2026
@gnodet gnodet added enhancement New feature or request mvn40 backport labels Jun 23, 2026
- Check groupId in findCompilerPlugin (not just artifactId)
- Clean up compiler plugin config when properties already provide
  targetVersion (previously left stale release/source/target elements)
- Clean up empty pluginManagement after plugin removal
- Clean up empty build element after all children removed
- Add test for pluginManagement compiler plugin migration

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
}
});
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It is a small detail, but the lambda below seems almost identical to the lambda above. Maybe it could be written only once in a separated function.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fully automatic review from Claude Code

Done — extracted a copyPatternList(source, target, containerName, elementName) static method that both includes and excludes now delegate to.

return true;
}

private void copyIncludesExcludes(Element source, Element target) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe the method could be static (I'm not sure, I didn't opened the file in an IDE for making sure that I didn't missed a field).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fully automatic review from Claude Code

Good catch — copyIncludesExcludes (and the new copyPatternList) are now both static.

@desruisseaux desruisseaux left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I did not verified in details, but it seems nice by a quick look and the purpose is nice too.

Address review feedback from desruisseaux: extract shared logic
from copyIncludesExcludes into a static copyPatternList method.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Backports a new mvnup upgrade strategy to migrate legacy compiler/source/resource configuration into Maven 4.1.0+ <build><sources><source> model, gated by --model-version 4.1.0+ or --all.

Changes:

  • Adds SourceStrategy (@Priority(20)) to extract target Java version from compiler properties / maven-compiler-plugin config and migrate source/test directories and (test)resources into <source> elements.
  • Cleans up migrated legacy elements/containers (properties, plugin config, sourceDirectory/testSourceDirectory, resources/testResources) and removes empty <build> when applicable.
  • Adds a comprehensive JUnit test suite covering applicability and all migration phases.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnup/goals/SourceStrategy.java Implements the new migration strategy that writes Maven 4.1+ <source> entries and removes legacy configuration.
impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvnup/goals/SourceStrategyTest.java Adds unit tests validating applicability and each migration phase (compiler props/plugin, directories, resources, version filtering, combined scenarios).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +145 to +151
if (targetVersion != null) {
Element sourcesElement = ensureSourcesElement(root);
Element sourceElement = DomUtils.insertNewElement("source", sourcesElement);
DomUtils.insertContentElement(sourceElement, "targetVersion", targetVersion);
context.detail("Set targetVersion: " + targetVersion);
hasChanges = true;
}
Comment on lines +137 to +143
String targetVersion = extractTargetVersionFromProperties(context, root);

if (targetVersion == null) {
targetVersion = extractTargetVersionFromCompilerPlugin(context, root);
} else {
cleanupCompilerPluginConfig(context, root);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport enhancement New feature or request mvn40

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants